本文介绍了MediaRecorder 和 VideoSource.SURFACE,停止失败:-1007(一个严重的 Android 错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试不使用 Camera 实例而是使用 Surface 视频源来录制 MediaRecorder(是的,这是可能的,但事实证明它不是那么完美) - mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);

I'm trying to record MediaRecorder without using Camera instance but using Surface video source (yes it's possible, but it turned out that it's not that perfect) - mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);

我只是写出了什么问题:

I just write what the issue:

下一个代码仅适用于某些设备,并且在最近的设备重新启动后暂时适用于某些设备或根本不起作用

如果它不起作用 MediaRecorder.stop() 方法失败并出现下一个错误

If it doesn't work ok MediaRecorder.stop() method fails with the next error

E/MediaRecorder:停止失败:-1007 W/System.err:

java.lang.RuntimeException:停止失败.在

java.lang.RuntimeException: stop failed. at

android.media.MediaRecorder.stop(原生方法)

android.media.MediaRecorder.stop(Native Method)

录音机mp4文件太小(千字节),无法播放

recorder mp4 file is too small size (kilobytes) and it can't be played

经过测试的设备:

适用于联想 P2、小米 A1

works on Lenovo P2, Xiaomi Mi A1

不适用于小米红米 5、索尼 Xperia、小米红米 4 Prime

doesn't work on Xiaomi Redmi 5, Sony Xperia, Xiaomi Redmi 4 Prime

您也可以阅读我代码中的注释以更好地理解问题

Also you can read comments in my code to understand the issue better

new Thread(() -> {

    MediaRecorder mediaRecorder = new MediaRecorder();

    File file = new File(Environment.getExternalStorageDirectory()
            + File.separator + "test_media_recorder_surface_source.mp4");
    if (file.exists()) {
        file.delete();
    }

    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setOutputFile(file.getAbsolutePath());
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mediaRecorder.setVideoSize(1280, 720);
    mediaRecorder.setCaptureRate(24);

    try {
        mediaRecorder.prepare();

        int sleepTime = 1000 / 24;

        Surface surface = mediaRecorder.getSurface();

        mediaRecorder.start();

        // record something (we can also record frames here from onPreviewFrame byte arrays)
        // e.g. convert raw frame byte[] to Bitmap using mb OpenCV and then draw bitmap on canvas
        // using canvas.drawBitmap(...)
        // here we record just blue background...
        for (int i = 0; i < 120; i++) { // 5 seconds, 24 fps
            Canvas canvas = surface.lockCanvas(null);
            canvas.drawColor(Color.BLUE);
            surface.unlockCanvasAndPost(canvas);
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // on many devices stop fails with RuntimeException -1007 error code
        // I guess it works ok 100% only for modern powerful devices...
        mediaRecorder.stop();
        // E/MediaRecorder: stop failed: -1007
        // W/System.err: java.lang.RuntimeException: stop failed.
        // at android.media.MediaRecorder.stop(Native Method)

        // recorder.reset();
        mediaRecorder.release();
        // I get file with very small size (kilobytes) and it can't be played

        // ######## RESULTS ######

        // WORKS OK ON:
        // - Lenovo P2 (Android 7)
        // - Xiaomi Mi A1 (Android 8)

        // DOESN'T WORK ON (stop fails with -1007, small video file and can't be played):
        // - Xiaomi Redmi 5 (Android 7)
        // - Sony Xperia (I don't remember the exact model and Android OS)
        // - Xiaomi Redmi 4 Prime (Android 6) *

        // * p.s. on Xiaomi Redmi 4 Prime it works some time after rebooting the device
        // if I leave this smartphone for a while and try again it will fail again
        // until I reboot the device...

    } catch (Exception e) {
        e.printStackTrace();
    }
}).start();

更新 #1似乎有些进展可能是什么问题 - 代码问题(mp4/h264)

UPDATE #1seems some progress what could be the issue - codes issue (mp4/h264)

使用WEBM/VP8效果更好,现在可以播放视频,但是fps有问题,在比例中显示1000

it works better with WEBM/VP8, videos can be played now, but something wrong with fps, it shows 1000 in proporties

mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.WEBM);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.VP8);

MediaRecord 在使用时也不记录音频

also MediaRecord doesn't record audio when using

mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.VORBIS);

检查 Android MediaRecorder使用 MP4/H264 且分辨率大于 720p 时停止崩溃所以当你使用 MediaRecorderMediaProjection 来记录/捕获设备屏幕时也会发生这种情况(因为它也使用 Surface...)

check Android MediaRecorder crashes on stop when using MP4/H264 and a resolution bigger than 720pso it also happens when you use MediaRecorder and MediaProjection to record/capture device Screen (because it also uses Surface...)

更新 2是的,vp8 编解码器似乎工作正常,但是 webm 容器存在一个问题 - 没有音频!

UPDATE 2yes seems vp8 codec works fine, but one problem for webm container - NO AUDIO!

有问题的 Android 不支持 VORBIS/OGG 音频编码... https://developer.android.com/guide/topics/media/media-formats#audio-formats

buggy Android just doesn't support VORBIS/OGG audio encoding... https://developer.android.com/guide/topics/media/media-formats#audio-formats

推荐答案

我猜没有解决方案

所以答案是:MediaRecorder/Android 有问题,或者移动公司在开发设备时并不关心所有 Android 功能

so the answer: MediaRecorder/Android is buggy or Mobile companies didn't care of all Android features while developing their devices

更新

MediaCodec 画布也有问题

mSurface = mMediaCodec.createInputSurface();
mSurface.lockHardwareCanvas()

它适用于更多具有 MediaCodec 的设备,但仍有一些设备可能无法使用此方法正确录制视频

It works on much more devices with MediaCodec but still some devices may fail to record video correctly using this method

所以最终答案:在使用 MediaCodec 时,永远不要使用 lockCanvaslockHardwareCanvas>MediaRecorder,有问题..

So final answer: don't ever use lockCanvas or lockHardwareCanvas when working with MediaCodec or MediaRecorder, it's buggy..

唯一的方法——OpenGl ES

The only way - OpenGl ES

关于问题的其他链接:

https://github.com/googlesamples/android-Camera2Video/issues/86https://issuetracker.google.com/issues/111433520

这篇关于MediaRecorder 和 VideoSource.SURFACE,停止失败:-1007(一个严重的 Android 错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 17:31