我正在尝试使用mediaRecorder
捕获视频。
这是我的代码:
public class MainActivity extends Activity {
MediaRecorder recorder;
File outfile = null;
static final String TAG = "MediaRecording";
Button startButton,stopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.button1);
stopButton = (Button) findViewById(R.id.button2);
}
public void startRecording(View view) throws IOException {
startButton.setEnabled(false);
stopButton.setEnabled(true);
//Creating file
File dir = Environment.getExternalStorageDirectory();
try {
outfile = File.createTempFile("video", ".mp4", dir);
} catch (IOException e) {
Log.e(TAG, "external storage access error");
return;
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setOutputFile(outfile.getAbsolutePath());
recorder.prepare();
recorder.start();
}
public void stopRecording(View view) {
startButton.setEnabled(true);
stopButton.setEnabled(false);
//stopping recorder
recorder.stop();
recorder.release();
}
}
然而,当我点击startrecording按钮时,我在第recorder.start()
行
引起:java.lang.RuntimeException:start failed。
我的清单中有以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.CAMERA"/>
编辑:根据要求,这里是完整的logcat。
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: FATAL EXCEPTION: main
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: Process: com.example.android.update, PID: 19656
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.view.View$1.onClick(View.java:4096)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.view.View.performClick(View.java:4856)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19956)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.os.Looper.loop(Looper.java:211)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5389)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.view.View$1.onClick(View.java:4091)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.view.View.performClick(View.java:4856)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19956)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.os.Looper.loop(Looper.java:211)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5389)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: Caused by: java.lang.RuntimeException: start failed.
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.media.MediaRecorder.start(Native Method)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at com.example.android.update.MainActivity.startRecording(MainActivity.java:74)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.view.View$1.onClick(View.java:4091)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.view.View.performClick(View.java:4856)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19956)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.os.Looper.loop(Looper.java:211)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5389)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
11-25 13:08:08.701 19656-19656/com.example.android.update E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)