我正在开发一个有关使用SurfaceView录制视频的移动应用程序。我想使用MediaRecorder录制多个视频,但是我的应用程序在录制第二个视频时崩溃。我可以成功录制第一个视频。我搜索了一些解决方案,但没有任何帮助。谢谢,这是我的代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
// we shall take the video in landscape orientation
context=this ;
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
FloatingActionButton fab = findViewById(R.id.fab);
mMediaRecorder=new MediaRecorder();
//mToggleButton = (ToggleButton) findViewById(R.id.toggleRecordingButton);
fab.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent Event) {
if (Event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(context, "Recording started!", Toast.LENGTH_SHORT).show();
if (mMediaRecorder!=null) {
mMediaRecorder=null;
try {
initRecorder(mHolder.getSurface());
} catch (IOException e) {
e.printStackTrace();
}
Log.d("s2", "dkhalt start2");
try {
//mMediaRecorder.setOutputFile(myfile);
record_audio();
Log.d("started_2", "started_2");
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}else if (Event.getAction() == MotionEvent.ACTION_UP) {
mMediaRecorder.stop();
mMediaRecorder.reset();
// shutdown();
Toast.makeText(video.this, "Recording finished", Toast.LENGTH_LONG).show();
Toast.makeText(context, "Recorded.!", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
private void initRecorder(Surface surface) throws IOException {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyVideos/";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
String myfile = path + "houssam" + i + ".mp4";
if (checkPermissionREAD_EXTERNAL_STORAGE(context)) {
if (mCamera == null) {
mCamera = Camera.open();
mCamera.unlock();
}
mMediaRecorder=new MediaRecorder();
mMediaRecorder.setPreviewDisplay(surface);
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
//mMediaRecorder.setVideoSize(640, 480);
mMediaRecorder.setOutputFile(myfile);
mMediaRecorder.setMaxDuration(3000); // 20 seconds
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mInitSuccesful = true;
i++;
}
else
{ ActivityCompat.requestPermissions(video.this, new String[]
{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO,CAMERA}, RequestPermissionCode) ;
}
}
public void record_audio(){
if (checkPermissionREAD_EXTERNAL_STORAGE(context)) {
mMediaRecorder.setOnInfoListener(this);
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else { ActivityCompat.requestPermissions(video.this, new String[]
{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO,CAMERA}, RequestPermissionCode) ;}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (!mInitSuccesful)
initRecorder(mHolder.getSurface());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
shutdown();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
private void shutdown() {
// Release MediaRecorder and especially the Camera as it's a shared
// object that can be used by other applications
if(null !=mMediaRecorder && mCamera!=null) {
try {
mMediaRecorder.reset();
mMediaRecorder.release();
mCamera.release();
// once the objects have been released they can't be reused
mMediaRecorder = null;
mCamera = null;
}
catch (RuntimeException e) {e.getMessage();}
}
}
@Override
public void onInfo(MediaRecorder mediaRecorder, int i, int i1) {
if (i == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
mMediaRecorder.stop();
Log.v("VIDEOCAPTURE","Maximum Duration Reached");
Toast.makeText(context, "Maximum Duration Reached",
Toast.LENGTH_SHORT).show();
}
}
08-03 16:06:51.138 30868-30868/com.example.android.roomwordssample
E/MediaRecorder: start failed: -19
08-03 16:06:51.138 30868-30868/com.example.android.roomwordssample
E/InputEventReceiver: Exception dispatching input event.
08-03 16:06:51.138 30868-30868/com.example.android.roomwordssample
E/MessageQueue-JNI: Exception in MessageQueue callback:
handleReceiveCallback
08-03 16:06:51.168 30868-30868/com.example.android.roomwordssample
E/MessageQueue-JNI: java.lang.RuntimeException: start failed.
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:946)
08-03 17:04:11.778 15665-15665/com.example.android.roomwordssample E/MediaRecorder: start failed: -19
08-03 17:04:11.798 15665-15665/com.example.android.roomwordssample E/InputEventReceiver: Exception dispatching input event.
08-03 17:04:11.798 15665-15665/com.example.android.roomwordssample E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
08-03 17:04:11.828 15665-15665/com.example.android.roomwordssample E/MessageQueue-JNI: java.lang.RuntimeException: start failed.
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:946)
at com.example.lab.Activities.video.record_audio(video.java:197)
at com.example.lab.Activities.video$1.onTouch(video.java:101)
at android.view.View.dispatchTouchEvent(View.java:9987)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2839)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2456)
```