MediaRecorder和VideoSource.SURFACE,停止失败:-1007(严重的Android错误) [英] MediaRecorder and VideoSource.SURFACE, stop failed: -1007 (a serious Android bug)
问题描述
我正尝试在不使用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);
我只是写出什么问题:
下一个代码仅在某些设备上有效,并且在最近的设备重新启动后暂时不可用,或者在某些设备上暂时不可用
如果它不起作用,则MediaRecorder.stop()
方法失败并显示下一个错误
If it doesn't work ok MediaRecorder.stop()
method fails with the next error
E/MediaRecorder:停止失败:-1007 W/System.err:
E/MediaRecorder: stop failed: -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
经过测试的设备:
适用于Lenovo P2,小米Mi A1
works on Lenovo P2, Xiaomi Mi A1
不适用于小米Redmi 5,索尼Xperia,小米Redmi 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 #1 seems 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);
在使用
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.VORBIS);
检查 Android MediaRecorder使用MP4/H264且分辨率大于720p时,停止时会崩溃
因此当您使用MediaRecorder
和MediaProjection
记录/捕获设备屏幕时也会发生(因为它也使用Surface
...)
check Android MediaRecorder crashes on stop when using MP4/H264 and a resolution bigger than 720p
so it also happens when you use MediaRecorder
and MediaProjection
to record/capture device Screen (because it also uses Surface
...)
更新2 是的,似乎vp8编解码器工作正常,但是webm容器的一个问题-没有声音!
UPDATE 2 yes seems vp8 codec works fine, but one problem for webm container - NO AUDIO!
笨拙的Android设备仅不支持VORBIS/OGG音频编码...
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
或MediaRecorder
时千万不要使用lockCanvas
或lockHardwareCanvas
,这是越野车..
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/86 https://issuetracker.google.com/issues/111433520
这篇关于MediaRecorder和VideoSource.SURFACE,停止失败:-1007(严重的Android错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!