暂停/恢复媒体codeC [英] Pause/Resume MediaCodec

查看:2199
本文介绍了暂停/恢复媒体codeC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现暂停/恢复功能为我的应用程序,用记录媒体codeC显示捕获。
我试着做男人coder.stop(),然后男人coder.start(),而不调用男人coder.release(),但没有奏效。再次调用时,男人coder.start()我得到IllegalStateException异常。
现在我实现了一个解决办法,我合并的视频撕成小块捕获完成后,但它需要一个很长的时间来融合。
任何人都可以帮我吗?也许有人已经实现了这个事情吗?

Initialazation:

 媒体codeC男人codeR;
    男人codeR =媒体codec.createEn coderByType(preferences.MIME_TYPE);
    男人coder.configure(mFormat,NULL,NULL,
            媒体codec.CONFIGURE_FLAG_EN code);
    mInputSurface =新InputSurface(MEN coder.createInputSurface()
            mSavedEglContext);
    男人coder.start();
    尝试{
        串FILEID =将String.valueOf(System.currentTimeMillis的());
        mMuxer =新MediaMuxer(dir.getPath()+/视频
                + FILEID +.MP4
                MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        videoParts.add(FILEID);
    }赶上(IOException异常IOE){
        抛出新的RuntimeException(MediaMuxer创建失败,IOE);
    }
    isRecording = TRUE;

暂停:

 公共无效pauseRecord pressed(){
    如果(isRecording){
        isRecording = FALSE;
        drainEn codeR(假);        如果(MEN codeR!= NULL){
            男人coder.stop();
        }
    }
}

取消暂停:

 公共无效resumeRecord pressed(){
    男人coder.start();
    isRecording = TRUE;
}

例外:

  15 01-09:34:27.980:E / AndroidRuntime(21467):致命异常:主要
01-09 15:34:27.980:E / AndroidRuntime(21467):工艺:com.example.poc,PID:21467
01-09 15:34:27.980:E / AndroidRuntime(21467):java.lang.IllegalStateException:启动失败
01-09 15:34:27.980:E / AndroidRuntime(21467):在android.media.Media codec.start(本机方法)
01-09 15:34:27.980:E / AndroidRuntime(21467):在com.example.poc.MyRenderer.resumeRecord pressed(MyRenderer.java:501)
01-09 15:34:27.980:E / AndroidRuntime(21467):在com.example.poc.MyGLSurfaceView.resumeRecord pressed(MyGLSurfaceView.java:243)
01-09 15:34:27.980:E / AndroidRuntime(21467):在com.example.poc.MainActivity.onClick(MainActivity.java:775)
01-09 15:34:27.980:E / AndroidRuntime(21467):在android.view.View.performClick(View.java:4438)
01-09 15:34:27.980:E / AndroidRuntime(21467):在android.view.View $ PerformClick.run(View.java:18422)
01-09 15:34:27.980:E / AndroidRuntime(21467):在android.os.Handler.handleCallback(Handler.java:733)
01-09 15:34:27.980:E / AndroidRuntime(21467):在android.os.Handler.dispatchMessage(Handler.java:95)
01-09 15:34:27.980:E / AndroidRuntime(21467):在android.os.Looper.loop(Looper.java:136)
01-09 15:34:27.980:E / AndroidRuntime(21467):在android.app.ActivityThread.main(ActivityThread.java:5017)
01-09 15:34:27.980:E / AndroidRuntime(21467):在java.lang.reflect.Method.invokeNative(本机方法)
01-09 15:34:27.980:E / AndroidRuntime(21467):在java.lang.reflect.Method.invoke(Method.java:515)
01-09 15:34:27.980:E / AndroidRuntime(21467):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
01-09 15:34:27.980:E / AndroidRuntime(21467):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-09 15:34:27.980:E / AndroidRuntime(21467):在dalvik.system.NativeStart.main(本机方法)

MediaFormat:

  mFormat = createMediaFormat();    私有静态MediaFormat createMediaFormat(){
    MediaFormat格式= MediaFormat.createVideoFormat(
            preferences.MIME_TYPE,mScreenWidth,mScreenHeight);
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
            媒体codecInfo codecCapabilities.COLOR_FormatSurface)。
    format.setInteger(MediaFormat.KEY_BIT_RATE,preferences.BIT_RATE);
    format.setInteger(MediaFormat.KEY_FRAME_RATE,preferences.FRAME_RATE);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL,
            preferences.IFRAME_INTERVAL);
    返回格式;
}


解决方案

媒体codeC 停止时丢弃它的配置,所以你需要调用配置()一次。我不知道为什么你想要做虽然重启一下 - 你可以把它激活,无需喂养它的数据

例如,请参阅 CameraCaptureActivity Grafika ,其中叶恩codeR整个活动将重新启动活着。如果你不希望在重启过程中视频暂停,你需要跟踪被编码多久停顿了一下,然后调整时间戳被送入复用器。

I'm trying to implement pause/resume feature for my app that records display capture using MediaCodec. I've tried doing mEncoder.stop() and then mEncoder.start() without calling mEncoder.release() but that didn't work. I get IllegalStateException when calling mEncoder.start() again. Right now I implemented a workaround, I'm merging peaces of video after the capture is complete, but it takes a really long time to merge. Can anyone help me with that? Maybe someone has already implemented this thing?

Initialazation:

    MediaCodec mEncoder;
    mEncoder = MediaCodec.createEncoderByType(Preferences.MIME_TYPE);
    mEncoder.configure(mFormat, null, null,
            MediaCodec.CONFIGURE_FLAG_ENCODE);
    mInputSurface = new InputSurface(mEncoder.createInputSurface(),
            mSavedEglContext);
    mEncoder.start();
    try {
        String fileId = String.valueOf(System.currentTimeMillis());
        mMuxer = new MediaMuxer(dir.getPath() + "/Video"
                + fileId + ".mp4",
                MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        videoParts.add(fileId);
    } catch (IOException ioe) {
        throw new RuntimeException("MediaMuxer creation failed", ioe);
    }
    isRecording = true;

Pause:

    public void pauseRecordPressed() {
    if (isRecording){
        isRecording = false;
        drainEncoder(false);

        if (mEncoder != null) {
            mEncoder.stop();
        }
    }
}

Unpause:

    public void resumeRecordPressed() {
    mEncoder.start();
    isRecording = true;
}

Exception:

01-09 15:34:27.980: E/AndroidRuntime(21467): FATAL EXCEPTION: main
01-09 15:34:27.980: E/AndroidRuntime(21467): Process: com.example.poc, PID: 21467
01-09 15:34:27.980: E/AndroidRuntime(21467): java.lang.IllegalStateException: start failed
01-09 15:34:27.980: E/AndroidRuntime(21467):    at android.media.MediaCodec.start(Native Method)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at com.example.poc.MyRenderer.resumeRecordPressed(MyRenderer.java:501)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at com.example.poc.MyGLSurfaceView.resumeRecordPressed(MyGLSurfaceView.java:243)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at com.example.poc.MainActivity.onClick(MainActivity.java:775)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at android.view.View.performClick(View.java:4438)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at android.view.View$PerformClick.run(View.java:18422)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at android.os.Handler.handleCallback(Handler.java:733)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at android.os.Looper.loop(Looper.java:136)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at android.app.ActivityThread.main(ActivityThread.java:5017)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at java.lang.reflect.Method.invokeNative(Native Method)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at java.lang.reflect.Method.invoke(Method.java:515)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-09 15:34:27.980: E/AndroidRuntime(21467):    at dalvik.system.NativeStart.main(Native Method)

MediaFormat:

mFormat = createMediaFormat();

    private static MediaFormat createMediaFormat() {
    MediaFormat format = MediaFormat.createVideoFormat(
            Preferences.MIME_TYPE, mScreenWidth, mScreenHeight);
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
            MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
    format.setInteger(MediaFormat.KEY_BIT_RATE, Preferences.BIT_RATE);
    format.setInteger(MediaFormat.KEY_FRAME_RATE, Preferences.FRAME_RATE);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL,
            Preferences.IFRAME_INTERVAL);
    return format;
}

解决方案

The MediaCodec discards its configuration when stopped, so you will need to call configure() again. I'm not sure why you're trying to do restart it though -- you can just leave it active, without feeding it data.

For example, see the CameraCaptureActivity in Grafika, which leaves the encoder alive across activity restarts. If you don't want to have a pause in the video during the restart, you'll need to keep track of how long encoding was paused, and then adjust the timestamps being fed into the muxer.

这篇关于暂停/恢复媒体codeC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆