我正在使用Live555开发Android流媒体应用程序。因此,我决定分别录制音频和视频来处理流。但不幸的是,当我尝试开始录制时,我遇到了RuntimeException。
private void startRecording() throws IOException {
if (videoRecorder == null) {
videoRecorder = new MediaRecorder();
videoRecorder.setPreviewDisplay(preview.getHolder().getSurface());
videoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
videoRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
videoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
videoRecorder.setVideoSize(320,24);
videoRecorder.setVideoFrameRate(25);
videoRecorder.setOutputFile(video.getFD());
}
if (audioRecorder == null) {
audioRecorder = new MediaRecorder();
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
audioRecorder.setOutputFile(audio.getFD());
}
if (!isStreaming) {
videoRecorder.prepare();
videoRecorder.start(); // RuntimeException
audioRecorder.prepare();
audioRecorder.start();
isStreaming = true;
} else {
stopRecording();
isStreaming = false;
}
Thread t = new Thread(new LiveStreamer(this, video, audio));
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}
我在互联网上搜索了一段时间,但没有一个成功的解决方案对我有用。这是logcat:
01-02 16:17:09.355: E/MediaRecorder(11256): start failed: -19
01-02 16:17:09.355: D/AndroidRuntime(11256): Shutting down VM
01-02 16:17:09.355: W/dalvikvm(11256): threadid=1: thread exiting with uncaught exception (group=0x41884da0)
01-02 16:17:09.355: E/AndroidRuntime(11256): FATAL EXCEPTION: main
01-02 16:17:09.355: E/AndroidRuntime(11256): Process: de.douglasmedia.LiveCam, PID: 11256
01-02 16:17:09.355: E/AndroidRuntime(11256): java.lang.RuntimeException: start failed.
01-02 16:17:09.355: E/AndroidRuntime(11256): at android.media.MediaRecorder.start(Native Method)
01-02 16:17:09.355: E/AndroidRuntime(11256): at de.douglasmedia.LiveCam.MainActivity.startRecording(MainActivity.java:139)
01-02 16:17:09.355: E/AndroidRuntime(11256): at de.douglasmedia.LiveCam.MainActivity.access$0(MainActivity.java:116)
01-02 16:17:09.355: E/AndroidRuntime(11256): at de.douglasmedia.LiveCam.MainActivity$1.onClick(MainActivity.java:66)
01-02 16:17:09.355: E/AndroidRuntime(11256): at android.view.View.performClick(View.java:4637)
01-02 16:17:09.355: E/AndroidRuntime(11256): at android.view.View$PerformClick.run(View.java:19422)
01-02 16:17:09.355: E/AndroidRuntime(11256): at android.os.Handler.handleCallback(Handler.java:733)
01-02 16:17:09.355: E/AndroidRuntime(11256): at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 16:17:09.355: E/AndroidRuntime(11256): at android.os.Looper.loop(Looper.java:136)
01-02 16:17:09.355: E/AndroidRuntime(11256): at android.app.ActivityThread.main(ActivityThread.java:5586)
01-02 16:17:09.355: E/AndroidRuntime(11256): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 16:17:09.355: E/AndroidRuntime(11256): at java.lang.reflect.Method.invoke(Method.java:515)
01-02 16:17:09.355: E/AndroidRuntime(11256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
01-02 16:17:09.355: E/AndroidRuntime(11256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
01-02 16:17:09.355: E/AndroidRuntime(11256): at dalvik.system.NativeStart.main(Native Method)
这是我的AndroidManifest的权限部分:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camra" />
某处,我已经读过问题是视频大小。所以,我尝试了允许的视频大小,但得到了同样的错误。有人知道如何解决这个问题吗?谢谢你的帮助!
答案 0 :(得分:1)
未设置音频编码器,需要添加以下行。
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
使用两个MediaRecorder的目的是什么?也许最好将一个MediaRecorder用于音频和视频。
以下代码部分在MotoG设备上运行正常:
private void startRecording() throws IOException {
String storage = Environment.getExternalStorageDirectory().getAbsolutePath();
if (mVideoRecorder == null) {
mVideoRecorder = new MediaRecorder();
mVideoRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
mVideoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mVideoRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mVideoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mVideoRecorder.setVideoSize(320,240);
mVideoRecorder.setVideoFrameRate(30);
FileOutputStream fileOutputStream = new FileOutputStream(new File(
storage + "/video.mp4"));
FileDescriptor fd = fileOutputStream.getFD();
mVideoRecorder.setOutputFile(fd);
}
if (mAudioRecorder == null) {
mAudioRecorder = new MediaRecorder();
mAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
FileOutputStream fileOutputStream = new FileOutputStream(new File(
storage + "/audio.mp4"));
FileDescriptor fd = fileOutputStream.getFD();
mAudioRecorder.setOutputFile(fd);
}
mVideoRecorder.prepare();
mVideoRecorder.start(); // RuntimeException
mAudioRecorder.prepare();
mAudioRecorder.start();
}