如何使用camera2 API从android中的摄像头生成最近拍摄的视频的缩略图?
如何使用camera2 API从android中的摄像头生成最近拍摄的视频的缩略图?,android,thumbnails,android-gallery,android-camera2,Android,Thumbnails,Android Gallery,Android Camera2,MainActivity.java(这是所有功能的主类),需要在摄像头应用程序的一角生成视频缩略图&单击缩略图时,它将播放视频 我尝试过“回收者”视图,但它不起作用。有人能帮我吗 import android.Manifest; import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.content.pm.PackageMa
MainActivity.java
(这是所有功能的主类),需要在摄像头应用程序的一角生成视频缩略图&单击缩略图时,它将播放视频
我尝试过“回收者”视图,但它不起作用。有人能帮我吗
import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.MediaRecorder;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Size;
import android.util.SparseIntArray;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CAMERA_PERMISSION_RESULT = 0;
public static final int REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT = 1;
private TextureView mTextureView;
private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
//Toast.makeText(getApplicationContext(),"TextureView is Available",Toast.LENGTH_SHORT).show();
setupCamera(width, height);
connectCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
private CameraDevice mCameraDevice;
private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
mCameraDevice = camera;
if(mIsRecording){
try {
createVideoFileName();
} catch (IOException e) {
e.printStackTrace();
}
startRecord();
mMediaRecorder.start();
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.setVisibility(View.VISIBLE);
mChronometer.start();
}else{
startPreview();
}
//Toast.makeText(getApplicationContext(),"Camera connected",Toast.LENGTH_SHORT).show();
}
@Override
public void onDisconnected(CameraDevice camera) {
camera.close();
mCameraDevice = null;
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(mVideoFileName ,MediaStore.Images.Thumbnails.MINI_KIND);
ImageView myImage = (ImageView) findViewById(R.id.thumbnail);
myImage.setImageBitmap(thumb);
}
@Override
public void onError(CameraDevice camera, int i) {
}
};
private HandlerThread mBackgroundHandlerThread;
private Handler mBackgroundHandler;
private String mCameraId;
private Size mPreviewSize;
private Size mVideoSize;
private MediaRecorder mMediaRecorder;
private Chronometer mChronometer;
private int mTotalRotation;
private CaptureRequest.Builder mCaptureRequestBuilder;
private ImageButton mRecordImageButton;
private boolean mIsRecording = false;
private File mVideoFolder;
private String mVideoFileName;
//private static File mRawVideoFileName;
private static SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 0);
ORIENTATIONS.append(Surface.ROTATION_90, 90);
ORIENTATIONS.append(Surface.ROTATION_180, 180);
ORIENTATIONS.append(Surface.ROTATION_270, 270);
}
private static class CompareSizeByArea implements Comparator<Size> {
@Override
public int compare(Size lhs, Size rhs) {
return Long.signum((long) lhs.getWidth() * lhs.getHeight() / (long) rhs.getWidth() * rhs.getHeight());
}
}
// public static boolean contains(int[] modes, int mode){
// if (modes == null){
// return false;
// }
// for(int i : modes){
// if(i == mode){
// return true;
// }
// }
// return false;
// }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createVideoFolder();
mMediaRecorder = new MediaRecorder();
mChronometer = (Chronometer) findViewById(R.id.chronometer);
mTextureView = (TextureView) findViewById(R.id.textureView);
final Bitmap thumb = ThumbnailUtils.createVideoThumbnail(mVideoFileName, MediaStore.Images.Thumbnails.MINI_KIND);
mRecordImageButton = (ImageButton) findViewById(R.id.videoButton);
mRecordImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mIsRecording) {
mChronometer.stop();
mChronometer.setVisibility(View.INVISIBLE);
mIsRecording = false;
mRecordImageButton.setImageResource(R.mipmap.start_recording);
//Toast.makeText(getApplicationContext(),"Started",Toast.LENGTH_SHORT).show();
mMediaRecorder.stop();
mMediaRecorder.reset();
Intent mediaStoreUpdateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaStoreUpdateIntent.setData(Uri.fromFile(new File(mVideoFileName)));
sendBroadcast(mediaStoreUpdateIntent);
startPreview();
} else {
checkWriteStoragePermission();
//checkWriteStoragePermission();
}
}
});
}
@Override
protected void onResume() {
super.onResume();
startBackgroundThread();
if (mTextureView.isAvailable()) {
setupCamera(mTextureView.getWidth(), mTextureView.getHeight());
connectCamera();
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permission, grantResults);
if (requestCode == REQUEST_CAMERA_PERMISSION_RESULT) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Application will not run without camera service", Toast.LENGTH_SHORT).show();
}
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Application will not have audio on record ", Toast.LENGTH_SHORT).show();
}
}
if (requestCode== REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT){
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
mIsRecording=true;
mRecordImageButton.setImageResource(R.mipmap.ic_launcher);
try {
createVideoFileName();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this,"Permission Successfully Granted",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"App needs to save video to run",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onPause() {
closeCamera();
stopBackgroundThread();
super.onPause();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
View decorView = getWindow().getDecorView();
if (hasFocus) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
}
private void setupCamera(int width, int height) {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
for (String cameraId : cameraManager.getCameraIdList()) {
CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
if (cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) {
continue;
}
StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
int deviceOrientation = getWindowManager().getDefaultDisplay().getRotation();
mTotalRotation = sensorToDeviceRotation(cameraCharacteristics, deviceOrientation);
boolean swapRotation = mTotalRotation == 90 || mTotalRotation == 270;
int rotateWidth = width;
int rotateHeight = height;
if (swapRotation) {
rotateWidth = height;
rotateHeight = width;
}
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotateWidth, rotateHeight);
mVideoSize = chooseOptimalSize(map.getOutputSizes(MediaRecorder.class), rotateWidth, rotateHeight);
mCameraId = cameraId;
return;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void connectCamera() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
Toast.makeText(this, "Video app required access to camera", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{android.Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO}, REQUEST_CAMERA_PERMISSION_RESULT);
}
} else {
cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void startRecord(){
try {
setupMediaRecorder();
SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(),mPreviewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
Surface recordSurface = mMediaRecorder.getSurface();
mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
mCaptureRequestBuilder.addTarget(previewSurface);
mCaptureRequestBuilder.addTarget(recordSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface,recordSurface),new CameraCaptureSession.StateCallback(){
@Override
public void onConfigured(CameraCaptureSession session) {
try {
session.setRepeatingRequest(mCaptureRequestBuilder.build(),null,null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
},null);
}catch (Exception e)
{
e.printStackTrace();
}
}
private void startPreview() {
SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
try {
mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mCaptureRequestBuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
session.setRepeatingRequest(mCaptureRequestBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
Toast.makeText(getApplicationContext(), "Unable to setup Camera Preview", Toast.LENGTH_SHORT).show();
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void closeCamera() {
if (mCameraDevice != null) {
mCameraDevice.close();
mCameraDevice = null;
}
}
private void startBackgroundThread() {
mBackgroundHandlerThread = new HandlerThread("AuthorTV");
mBackgroundHandlerThread.start();
mBackgroundHandler = new Handler(mBackgroundHandlerThread.getLooper());
}
private void stopBackgroundThread() {
mBackgroundHandlerThread.quitSafely();
try {
mBackgroundHandlerThread.join();
mBackgroundHandlerThread = null;
mBackgroundHandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static int sensorToDeviceRotation(CameraCharacteristics cameraCharacteristics, int deviceOrientation) {
int sensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
deviceOrientation = ORIENTATIONS.get(deviceOrientation);
return (sensorOrientation + deviceOrientation + 360) % 360;
}
private static Size chooseOptimalSize(Size[] choices, int width, int height) {
List<Size> bigEnough = new ArrayList<Size>();
for (Size option : choices) {
if (option.getHeight() == option.getWidth() * height / width && option.getWidth() >= width && option.getHeight() >= height) {
bigEnough.add(option);
}
}
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizeByArea());
} else {
return choices[0];
}
}
private void createVideoFolder() {
File movieFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
mVideoFolder = new File(movieFile, "AuthorTVCam");
if (!mVideoFolder.exists()){
mVideoFolder.mkdirs();
}
}
private File createVideoFileName() throws IOException {
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String prepend = "VIDEO_" + timestamp + "_";
File videoFile = File.createTempFile(prepend, ".mp4", mVideoFolder);
mVideoFileName = videoFile.getAbsolutePath();
return videoFile;
}
// private File createRawVideoFileName() throws IOException {
// String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
// String prepend = "RAW_" + timestamp + "_";
// File rawvideoFile = File.createTempFile(prepend, ".jpeg", mRawVideoFolder);
// mVideoFileName = rawvideoFile.getAbsolutePath();
// return rawvideoFile;
// }
private void checkWriteStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
mIsRecording = true;
mRecordImageButton.setImageResource(R.mipmap.stop_recording);
try {
createVideoFileName();
} catch (IOException e) {
e.printStackTrace();
}
startRecord();
mMediaRecorder.start();
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.setVisibility(View.VISIBLE);
mChronometer.start();
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(this, "App needs to be able to save videos", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT);
}
} else {
mIsRecording = true;
mRecordImageButton.setImageResource(R.mipmap.stop_recording);
try {
createVideoFileName();
} catch (IOException e) {
e.printStackTrace();
}
startRecord();
mMediaRecorder.start();
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.setVisibility(View.VISIBLE);
mChronometer.start();
}
}
private void setupMediaRecorder()throws IOException{
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(mVideoFileName);
mMediaRecorder.setVideoEncodingBitRate(1000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(),mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setOrientationHint(mTotalRotation);
mMediaRecorder.prepare();
}
}
导入android.Manifest;
导入android.annotation.TargetApi;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.graphics.Bitmap;
导入android.graphics.SurfaceTexture;
导入android.hardware.camera2.CameraAccessException;
导入android.hardware.camera2.CameraCaptureSession;
导入android.hardware.camera2.CameraCharacteristics;
导入android.hardware.camera2.CameraDevice;
导入android.hardware.camera2.CameraManager;
导入android.hardware.camera2.CaptureRequest;
导入android.hardware.camera2.params.StreamConfigurationMap;
导入android.media.MediaRecorder;
导入android.media.ThumbnailUtils;
导入android.net.Uri;
导入android.os.Build;
导入android.os.Bundle;
导入android.os.Environment;
导入android.os.Handler;
导入android.os.HandlerThread;
导入android.os.SystemClock;
导入android.provider.MediaStore;
导入android.support.v4.content.ContextCompat;
导入android.support.v7.app.AppActivity;
导入android.util.Size;
导入android.util.SparseIntArray;
导入android.view.Surface;
导入android.view.TextureView;
导入android.view.view;
导入android.widget.Chronometer;
导入android.widget.ImageButton;
导入android.widget.ImageView;
导入android.widget.Toast;
导入java.io.File;
导入java.io.IOException;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.Date;
导入java.util.List;
@TargetApi(Build.VERSION\u code.LOLLIPOP)
公共类MainActivity扩展了AppCompatActivity{
公共静态最终int请求\摄像机\权限\结果=0;
公共静态最终整数请求\写入\外部\存储\权限\结果=1;
私有纹理视图mTextureView;
私有TextureView.SurfaceTextRelistener mSurfaceTextureListener=新的TextureView.SurfaceTextRelistener(){
@凌驾
SurfaceTexture上的公共空心可用(SurfaceTexture SurfaceTexture,int-width,int-height){
//Toast.makeText(getApplicationContext(),“TextureView可用”,Toast.LENGTH_SHORT.show();
设置摄像机(宽度、高度);
连接摄像头();
}
@凌驾
SurfaceTextureSizeChanged上的公共空心(SurfaceTexture SurfaceTexture,int-width,int-height){
}
@凌驾
公共布尔onSurfaceTextureDestroyed(SurfaceTexture SurfaceTexture){
返回false;
}
@凌驾
已更新SurfaceTexture上的公共空间(SurfaceTexture SurfaceTexture){
}
};
私人摄像设备;
private CameraDevice.StateCallback mCameraDeviceStateCallback=新建CameraDevice.StateCallback(){
@凌驾
打开公共空间(摄像头副摄像头){
mCameraDevice=摄像机;
如果(错误记录){
试一试{
createVideoFileName();
}捕获(IOE异常){
e、 printStackTrace();
}
startRecord();
mMediaRecorder.start();
mchrometer.setBase(SystemClock.elapsedRealtime());
mchrometer.setVisibility(视图.可见);
mchrometer.start();
}否则{
startPreview();
}
//Toast.makeText(getApplicationContext(),“摄像头已连接”,Toast.LENGTH\u SHORT.show();
}
@凌驾
已断开连接的公共空间(摄像头副摄像头){
摄像机关闭();
mCameraDevice=null;
位图thumb=ThumbnailUtils.createVideoThumbnail(mVideoFileName,MediaStore.Images.Thumbnails.MINI_-KIND);
ImageView myImage=(ImageView)findViewById(R.id.缩略图);
设置图像位图(拇指);
}
@凌驾
公共无效onError(摄像头设备摄像头,int i){
}
};
私有HandlerThread mBackgroundHandlerThread;
私人处理程序mBackgroundHandler;
私人字符串mCameraId;
私人规模的mPreviewSize;
私人规模;
专用媒体记录器;
私人天文钟;
私人国际旋转;
私有CaptureRequest.Builder mCaptureRequestBuilder;
私有图像按钮mRecordImageButton;
私有布尔错误记录=假;
私人文件夹;
私有字符串文件名;
//私有静态文件mRawVideoFileName;
专用静态SparseIntArray方向=新SparseIntArray();
静止的{
方向。附加(Surface.ROTATION_0,0);
方向。附加(Surface.ROTATION_90,90);
方向。附加(Surface.ROTATION_180,180);
方向。附加(Surface.ROTATION_270,270);
}
私有静态类CompareSizeByArea实现Comparator{
@凌驾
公共整数比较(大小lhs,大小rhs){
返回Long.signum((Long)lhs.getWidth()*lhs.getHeight()/(Long)rhs.getWidth()*rhs.getHeight());
}
}
//公共静态布尔包含(int[]模式,int模式){
//如果(模式==null){
//返回false;
// }
//用于(int i:模式){
//if(i==模式){
//返回true;
// }
// }
//返回false;
// }
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createVideoFolder();
mMediaRecorder=新的MediaRecorder();
妇幼保健院
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="70dp"
android:paddingBottom="50dp"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/thumbnail"
android:layout_alignLeft="@+id/chronometer"
android:layout_alignStart="@+id/chronometer" />
<TextureView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textureView"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/videoButton"
android:src="@mipmap/start_recording"
android:contentDescription="@string/video_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="39dp" />
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chronometer"
android:visibility="invisible"
android:textColor="#ff0000"
android:textSize="25dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,MediaStore.Images.Thumbnails.MINI_KIND);
private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
mCameraDevice = camera;
if(mIsRecording){
try {
createVideoFileName();
} catch (IOException e) {
e.printStackTrace();
}
startRecord();
mMediaRecorder.start();
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.setVisibility(View.VISIBLE);
mChronometer.start();
}else{
startPreview();
}
//Toast.makeText(getApplicationContext(),"Camera connected",Toast.LENGTH_SHORT).show();
}
@Override
public void onDisconnected(CameraDevice camera) {
camera.close();
mCameraDevice = null;
//here your camera disconnected, so, you can do your other UI stuffs here.
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(mVideoFileName ,MediaStore.Images.Thumbnails.MINI_KIND)
}
@Override
public void onError(CameraDevice camera, int i) {
}
};
Glide.with(activity)
.load(yourVideoUri)
.asBitmap()
.override(THUMBNAIL_DEFAULT_RESOLUTION, THUMBNAIL_DEFAULT_RESOLUTION)
.into(yourImageView);
@Override public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
videoContainer.start();
videoContainer.seekTo(VIDEO_START_VALUE);
}
@Override public void onCompletion(MediaPlayer mp) {
//Manage onCompletion callback if necessary
}