mediaRecorder.start()报错
Camara打开预览图像正常,一点击开始录像就报错弹出,代码如下,老师帮看看:
public class RecordActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
private TextureView mTextureView;
private String mCameraId;
private CameraDevice mCameraDevice;
private CaptureRequest.Builder mPreviewBuilder;
private CaptureRequest mCaptureRequest;
private CameraCaptureSession mPreviewSession;
private CameraCharacteristics characteristics;
private String videoSavePath;//视频保存路径
private HandlerThread mCameraThread;
private Handler mCameraHandler;
private boolean isStop = false;//是否停止过了MediaRecorder
private MediaRecorder mMediaRecorder;
private Button mStart;
private static final String TAG = "RecordActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
initViews();
}
@Override
protected void onResume() {
super.onResume();
initTextureView();
}
private void initViews() {
mTextureView = findViewById(R.id.textureV);
mStart =findViewById(R.id.start_btn);
mStart.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
prepareMediaRecorder();
mMediaRecorder.start();
}
});
}
private void initTextureView() {
mCameraThread = new HandlerThread("CameraThread");
mCameraThread.start();
mCameraHandler = new Handler(mCameraThread.getLooper());
mTextureView.setSurfaceTextureListener(this);
}
public static void start(Context context) {
Intent intent = new Intent(context, RecordActivity.class);
context.startActivity(intent);
}
/**
* ************************************TextureView回调******************************************
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
setupCamera(width, height);//配置相机参数
openCamera(mCameraId);//打开相机
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
/**
* ******************************************************************************
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setupCamera(int width, int height) {
//获取摄像头的管理者CameraManager
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
mCameraId = manager.getCameraIdList()[0];
characteristics = manager.getCameraCharacteristics(mCameraId);
//MediaRecorder用于录像所需
mMediaRecorder = new MediaRecorder();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ******************************openCamera(打开Camera)*****************************************
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void openCamera(String CameraId) {
//获取摄像头的管理者CameraManager
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
//检查权限
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, "权限。。。。。。。。。。 " );
return;
}
manager.openCamera(CameraId, mStateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onOpened(CameraDevice camera) {
mCameraDevice = camera;
startPreview();
}
@Override
public void onDisconnected(CameraDevice cameraDevice) {
cameraDevice.close();
mCameraDevice = null;
}
@Override
public void onError(CameraDevice cameraDevice, int error) {
cameraDevice.close();
mCameraDevice = null;
}
};
/**
* ******************************Camera2成功打开,开始预览(startPreview)*************************
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void startPreview() {
if (null == mCameraDevice || !mTextureView.isAvailable()) {
return;
}
SurfaceTexture mSurfaceTexture = mTextureView.getSurfaceTexture();//获取TextureView的SurfaceTexture,作为预览输出载体
if (mSurfaceTexture == null) {
return;
}
try {
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);//创建CaptureRequestBuilder,TEMPLATE_PREVIEW比表示预览请求
Surface mSurface = new Surface(mSurfaceTexture);//获取Surface显示预览数据
mPreviewBuilder.addTarget(mSurface);//设置Surface作为预览数据的显示界面
//默认预览不开启闪光灯
mPreviewBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
mCameraDevice.createCaptureSession(Arrays.asList(mSurface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
//创建捕获请求
mCaptureRequest = mPreviewBuilder.build();
mPreviewSession = session;
mPreviewSession.setRepeatingRequest(mCaptureRequest, null, mCameraHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
}, null);
} catch (Exception e) {
e.printStackTrace();
Log.e("dasdadasd", "捕获的异常" + e.toString());
}
}
/**
* **********************************配置录像******************************
*
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void prepareMediaRecorder() {
if (null == mCameraDevice || !mTextureView.isAvailable() ) {
return;
}
try {
closePreviewSession();
setUpMediaRecorder();
SurfaceTexture texture = mTextureView.getSurfaceTexture();
assert texture != null;
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
List<Surface> surfaces = new ArrayList<>();
// Set up Surface for the camera preview
Surface previewSurface = new Surface(texture);
surfaces.add(previewSurface);
mPreviewBuilder.addTarget(previewSurface);
// Set up Surface for the MediaRecorder
Surface recorderSurface = mMediaRecorder.getSurface();
surfaces.add(recorderSurface);
mPreviewBuilder.addTarget(recorderSurface);
mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
//创建捕获请求
mCaptureRequest = mPreviewBuilder.build();
mPreviewSession = session;
//设置反复捕获数据的请求,这样预览界面就会一直有数据显示
mPreviewSession.setRepeatingRequest(mCaptureRequest, null, mCameraHandler);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "捕获的异常2" + e.toString());
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
}, null);
} catch (Exception e) {
e.printStackTrace();
}
}
private void setUpMediaRecorder() {
try {
mMediaRecorder.reset();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoEncodingBitRate(2500000);
mMediaRecorder.setVideoFrameRate(20);
mMediaRecorder.setVideoSize(mTextureView.getWidth(), mTextureView.getHeight());
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
videoSavePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CameraV2/" + "VIDEO_" + timeStamp + ".mp4";
mMediaRecorder.setOutputFile(videoSavePath);
mMediaRecorder.setOrientationHint(90);
mMediaRecorder.prepare();
Log.e(TAG, "配置录像完成......................." );
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "配置录像错误"+e.toString() );
}
}
//清除预览Session
private void closePreviewSession() {
if (mPreviewSession != null) {
mPreviewSession.close();
mPreviewSession = null;
}
}
public void onFinishCapture() {
try {
if (mPreviewSession != null) {
mPreviewSession.close();
mPreviewSession = null;
}
if (mCameraDevice != null) {
mCameraDevice.close();
mCameraDevice = null;
}
if (mMediaRecorder != null) {
mMediaRecorder.release();
mMediaRecorder = null;
}
if (mCameraHandler != null) {
mCameraHandler.removeCallbacksAndMessages(null);
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.toString() + "onFinish2()");
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
try{
if (!isStop){
mMediaRecorder.stop();
mMediaRecorder.release();
}
isStop=true;
} catch(Exception stopException){
Log.e(TAG, stopException.toString()+" ------------" );
}
}
@Override
protected void onPause() {
super.onPause();
try{
if (!isStop){
mMediaRecorder.stop();
mMediaRecorder.release();
}
isStop=true;
} catch(Exception stopException){
Log.e(TAG, stopException.toString()+" ------------" );
}
onFinishCapture();
}
}
正在回答 回答被采纳积分+1
有点懵了懵了
03-19 15:23:51.356 332-332/? E/CameraSource: Camera connection could not be established.
03-19 15:23:51.356 332-332/? E/StagefrightRecorder: [setupMPEG4orWEBMRecording] setupMediaSource Fail err=-19
03-19 15:23:51.356 5929-5929/? E/MediaRecorder: start failed: -19
03-19 15:23:51.357 5929-5929/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yk17.administrator.recorderdemo, PID: 5929
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.yk17.administrator.recorderdemo.RecordActivity$1.onClick(RecordActivity.java:81)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20300)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5682)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
- 参与学习 人
- 解答问题 241 个
本专题是动画、游戏、音视频等实际应用开发必备技能。课程深入到Android事件总线,利用主流框架实现消息推送、自定义事件统计分析等功能。最后带你开发一款利用传感器实现计步、距离、热量等功能的App。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星