MediaRecorder.stop() stop failed: -1007

二次信任 提交于 2019-12-17 15:47:32

问题


I am recording video with MediaRecorder. My code works fine on 2.3.3 but fails on 4.0.3.

The issue is following: the code mediaRecorder.stop() throws the RuntimeExeption

java.lang.RuntimeException: stop failed.
    at android.media.MediaRecorder.stop(Native Method)

with LogCat message

04-05 15:10:51.815: E/MediaRecorder(15709): stop failed: -1007

UPDATE

I've found, that MediaPlayer reports an error (via MediaPlayer.OnErrorListener) almost immediately after the start. Error code is 100 (media server died), extra -1007.

UPDATE 2 Code to prepare the MediaRecorder

            c = Camera.open();

    ...

    // Step 1: Unlock and set camera to MediaRecorder
    camera.unlock();
    mediaRecorder.setCamera(camera);

    // Step 2: Set sources
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    CamcorderProfile profile = CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH);

    // manual set up!

    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
    mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
    mediaRecorder.setVideoSize(profile.videoFrameWidth,
            profile.videoFrameHeight);

    mediaRecorder.setAudioChannels(profile.audioChannels);
    mediaRecorder.setAudioEncodingBitRate(profile.audioBitRate);
    mediaRecorder.setAudioSamplingRate(profile.audioSampleRate);

    mediaRecorder.setAudioEncoder(profile.audioCodec);
    //mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    mediaRecorder.setVideoEncoder(profile.videoCodec);

    // mediaRecorder.setProfile(profile);

    // Step 4: Set output file
    mediaRecorder.setOutputFile("somefile.mp4");

    // Step 5: Set the preview output
    mediaRecorder.setPreviewDisplay(preview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        mediaRecorder.prepare();
    } catch ...
    { release mediaRecorder}

then I simplyCall mediaRecorder.start() please note, that I need video to be encoded into mp4 format. This code works on Samsng Galaxy GIO (android 2.3.3) and fails as described on Acer E305 (android 4.0.2)

Any ideas? Thanks.


回答1:


Solved it at last. The issue was setting the preview size before setting the actual preview for the camera. The preview size MUST be equal to the selected video size.

CamcorderProfile profile = [get required profile];

Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(profile.videoFrameWidth,profile.videoFrameHeight);
mCamera.setParameters(parameters);

mCamera.setPreviewDisplay([surface holder]);
mCamera.startPreview();

...

//configure MediaRecorder and call MediaRecorder.start()



回答2:


Quoting the documentation of "stop" method in MediaRecorder.java in 4.0.3:

Stops recording. Call this after start(). Once recording is stopped, you will have to configure it again as if it has just been constructed. Note that a RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called. This happens if stop() is called immediately after start(). The failure lets the application take action accordingly to clean up the output file (delete the output file, for instance), since the output file is not properly constructed when this happens.

And the fact that MediaPlayer is reporting this "media server died" is due to the same reason. Can you post the rest of your code to see if there's any misconception that may cause this issue?




回答3:


I experienced the same issue on Samsung J4+, Android 9 Pie.

Fixed it by running mediaRecorder.start() and mediaRecorder.stop() in a Handler:

private val START = 0;
private val STOP  = 1;

inner class CameraHandler(looper: Looper?): Handler(looper) {

  override fun handleMessage(msg: Message?) {
    super.handleMessage(msg)
      try {
        when (msg?.what) {
          START -> mediaRecorder?.start()
          STOP  -> mediaRecorder?.stop()
        }
      } catch (e: Exception) {
        Log.d("debug", e.message)
      }
  }
}

declare the Handler:

private lateinit var mCameraHandler: Handler

initialize in OnCreate with a HandlerThread Looper:

val handlerThread: HandlerThread = HandlerThread("Camera Handler Thread")
handlerThread.start()
mCameraHandler = CameraHandler(handlerThread.looper)

when record or stop button is clicked call:

mCameraHandler.sendEmptyMessage(START)
mCameraHandler.sendEmptyMessage(STOP)

link to my messy code xD




回答4:


I had -1007 error on some devices, mostly with android 9 and finally i solved this problem. The reason was that OMX.google.h264.encoder supports only video sizes evenly dividable by 16. I used displayMetrics.widthPixels and displayMetrics.heightPixels for video size and it is not meet requirements on all devices.

Hope this helps someone!




回答5:


So I found this error being reported for me on the Android emulator for API 18 (after recording was working fine on later versions).

What I found is that if I had called Camera.startPreview() before initialising and starting starting my MediaRecorder instance, I'd get the stop failed: -1007 log when calling MediaRecorder.stop, but if I called Camera.stopPreview() before initialising my MediaRecorder the video would record fine.

I hope this helps



来源:https://stackoverflow.com/questions/15833694/mediarecorder-stop-stop-failed-1007

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!