Android MediaMuxer使用遇到的问题

1.IllegalStateException问题
MediaMuxer的使用必须严格遵循如下顺序:
addTrack->start->writeSampleData->stop->release
如果有顺序冲突或者多次调用start、stop、release,会导致IllegalStateException
在使用MediaMuxer时,经常遇到的场景是在两个不同的Thread中分别执行音视频的录制+编码,所以需要等待两个线程都执行完addTrack之后,再start

2.stop fail的问题
addTrack的时候,传入的MediaFormat需要从对应的MediaCodec实例调用MediaCodec.getOutputFormat()得到,否则stop的时候会发生Exception
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这里是一个简单的Android MediaMuxer使用示例: ```java public class MediaMuxerActivity extends Activity { private static final String TAG = "MediaMuxerActivity"; private static final String VIDEO_MIME_TYPE = "video/avc"; private static final int VIDEO_FRAME_RATE = 30; private static final int VIDEO_I_FRAME_INTERVAL = 5; private static final int VIDEO_WIDTH = 720; private static final int VIDEO_HEIGHT = 1280; private MediaMuxer mMediaMuxer; private int mVideoTrackIndex; private long mLastVideoPresentationTimeUs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_media_muxer); setUpMediaMuxer(); } private void setUpMediaMuxer() { try { // Create a MediaMuxer. This will create a new file with the specified name. mMediaMuxer = new MediaMuxer(getFilesDir() + "/output.mp4", MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); // Add the video track to the MediaMuxer. MediaFormat videoFormat = MediaFormat.createVideoFormat(VIDEO_MIME_TYPE, VIDEO_WIDTH, VIDEO_HEIGHT); videoFormat.setInteger(MediaFormat.KEY_BIT_RATE, 2000000); videoFormat.setInteger(MediaFormat.KEY_FRAME_RATE, VIDEO_FRAME_RATE); videoFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, VIDEO_I_FRAME_INTERVAL); mVideoTrackIndex = mMediaMuxer.addTrack(videoFormat); // Start the MediaMuxer. mMediaMuxer.start(); } catch (IOException e) { Log.e(TAG, "setUpMediaMuxer failed", e); } } public void writeVideoFrame(byte[] data) { if (mMediaMuxer == null || mVideoTrackIndex < 0) { return; } // Create a ByteBuffer from the video data. ByteBuffer videoBuffer = ByteBuffer.wrap(data); // Calculate the presentation time of the video frame. long videoPresentationTimeUs = (System.nanoTime() - mLastVideoPresentationTimeUs) / 1000; mLastVideoPresentationTimeUs = System.nanoTime(); // Write the video data to the MediaMuxer. MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo(); videoBufferInfo.offset = 0; videoBufferInfo.size = data.length; videoBufferInfo.presentationTimeUs = videoPresentationTimeUs; videoBufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME; mMediaMuxer.writeSampleData(mVideoTrackIndex, videoBuffer, videoBufferInfo); } @Override protected void onDestroy() { super.onDestroy(); // Stop and release the MediaMuxer. if (mMediaMuxer != null) { mMediaMuxer.stop(); mMediaMuxer.release(); mMediaMuxer = null; } } } ``` 这个示例演示了如何使用MediaMuxerAndroid中编写视频文件。在 `setUpMediaMuxer()` 方法中,我们创建了一个新的 `MediaMuxer` 对象,并为视频轨道添加了一个 `MediaFormat` 对象。然后,在 `writeVideoFrame()` 方法中,我们将视频数据写入 `MediaMuxer`。最后,在 `onDestroy()` 方法中,我们停止并释放了 `MediaMuxer`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值