gpt4 book ai didi

Android MediaRecorder 启动失败-19

转载 作者:行者123 更新时间:2023-11-30 02:27:28 25 4
gpt4 key购买 nike

我正在使用 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();
}

我在 Internet 上搜索了一段时间,但没有一个成功的解决方案适合我。这是日志:

    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" />

在某处,我读到问题是视频大小。所以,我尝试使用允许的视频尺寸,但得到了同样的错误。有人知道如何解决这个问题吗?感谢您的帮助!

最佳答案

  1. 您的视频尺寸不正确,应该是 320x240 而不是 320x24。打字错误?
  2. 音频编码器未设置,您需要添加以下行。

    audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

  3. 使用两个 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();
}

关于Android MediaRecorder 启动失败-19,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27744425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com