我试图在一段时间后停止录制并且不知道如何调用停止录制的方法(新到android)
这是我正在尝试的... (两种方法都在Xml中的onClick中调用)
public void startRecording(View v) {
Log.d(TAG, "startRecording()");
// we need to unlock the camera so that mediaRecorder can use it
// this.camera.unlock(); // unnecessary in API >= 14
// now we can initialize the media recorder and set it up with our
// camera
flipCamera.setVisibility(View.GONE);
captureImage.setVisibility(View.GONE);
String deviceMan = android.os.Build.MANUFACTURER;
this.mediaRecorder = new MediaRecorder();
this.mediaRecorder.setCamera(this.camera);
camera.unlock();
this.mediaRecorder.setCamera(camera);
this.mediaRecorder.setOrientationHint(90);
this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
this.mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
this.mediaRecorder.setProfile(camcorderProfile_HQ);
this.mediaRecorder.setOutputFile(this.initFile().getAbsolutePath());
this.mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
this.mediaRecorder.setPreviewDisplay(this.cameraPreview.getHolder().getSurface());
try {
this.mediaRecorder.prepare();
// start the actual recording
// throws IllegalStateException if not prepared
this.mediaRecorder.start();
Toast.makeText(this, R.string.recording, Toast.LENGTH_SHORT).show();
// enable the stop button by indicating that we are recording
this.toggleButtons(true);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
stopRecording();// how to call stop here so that i can stop after 60 seconds
}
}, 60000);
} catch (Exception e) {
Log.wtf(TAG, "Failed to prepare MediaRecorder", e);
Toast.makeText(this, R.string.cannot_record, Toast.LENGTH_SHORT).show();
this.releaseMediaRecorder();
}
}
我在按钮点击时调用停止方法但是我还想添加60秒延迟,以便媒体记录器停止。
public void stopRecording(View v) {
Log.d(TAG, "stopRecording()");
assert this.mediaRecorder != null;
try {
this.mediaRecorder.stop();
Toast.makeText(this, R.string.saved, Toast.LENGTH_SHORT).show();
// we are no longer recording
flipCamera.setVisibility(View.VISIBLE);
captureImage.setVisibility(View.VISIBLE);
this.toggleButtons(false);
} catch (RuntimeException e) {
// the recording did not succeed
Log.w(TAG, "Failed to record", e);
if (this.file != null && this.file.exists() && this.file.delete()) {
Log.d(TAG, "Deleted " + this.file.getAbsolutePath());
}
return;
} finally {
this.releaseMediaRecorder();
}
if (this.file == null || !this.file.exists()) {
Log.w(TAG, "File does not exist after stop: " + this.file.getAbsolutePath());
} else {
Log.d(TAG, "Going to display the video: " + this.file.getAbsolutePath());
Intent intent = new Intent(this, VideoPlaybackActivity.class);
intent.setData(Uri.fromFile(file));
System.out.println("########Uri is: " + Uri.fromFile(file));
intent.putExtra("fromClass", "");
intent.putExtra("file_name", videoName);
intent.putExtra("data_type", "video");
intent.putExtra("expiry_time", "");
super.startActivity(intent);
finish();
}
}
错误日志 -
05-15 13:51:34.050: W/SurfaceView(4877): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false
05-15 13:51:34.410: E/MediaPlayer(4877): error (1, -2147483648)
05-15 13:51:34.420: E/MediaPlayer(4877): Error (1,-2147483648)
05-15 13:51:34.420: D/VideoView(4877): Error: 1,-2147483648
05-15 13:51:37.400: D/Camera_Activity(4877): stopRecording()
05-15 13:51:37.400: W/Camera_Activity(4877): Failed to record
05-15 13:51:37.400: W/Camera_Activity(4877): java.lang.NullPointerException
05-15 13:51:37.400: W/Camera_Activity(4877): at com.ss.chat.Camera_Activity.stoprecording(Camera_Activity.java:350)
05-15 13:51:37.400: W/Camera_Activity(4877): at com.ss.chat.Camera_Activity$6.run(Camera_Activity.java:297)
05-15 13:51:37.400: W/Camera_Activity(4877): at android.os.Handler.handleCallback(Handler.java:605)
05-15 13:51:37.400: W/Camera_Activity(4877): at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 13:51:37.400: W/Camera_Activity(4877): at android.os.Looper.loop(Looper.java:137)
05-15 13:51:37.400: W/Camera_Activity(4877): at android.app.ActivityThread.main(ActivityThread.java:4507)
05-15 13:51:37.400: W/Camera_Activity(4877): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 13:51:37.400: W/Camera_Activity(4877): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 13:51:37.400: W/Camera_Activity(4877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
05-15 13:51:37.400: W/Camera_Activity(4877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
05-15 13:51:37.400: W/Camera_Activity(4877): at dalvik.system.NativeStart.main(Native Method)
05-15 13:51:37.410: D/Camera_Activity(4877): Deleted /mnt/sdcard/Movies/MyVideo/VID_20140515_135037_13.mp4
答案 0 :(得分:1)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
stopRecording(v);// how to call stop here so that i can stop after 60 seconds
}
}, 60000);