我正在使用这个库从 USB 摄像头录制视频。我使用的代码如下:
try {
private MediaMuxerWrapper mMuxer;
mMuxer = new MediaMuxerWrapper("FolderName", ".mp4");
new MediaVideoEncoder(mMuxer, mMediaEncoderListener);
mMuxer.prepare();
mMuxer.startRecording();
} catch (final IOException e) {
}
private final MediaEncoder.MediaEncoderListener mMediaEncoderListener = new MediaEncoder.MediaEncoderListener() {
@Override
public void onPrepared(final MediaEncoder encoder) {
if (encoder instanceof MediaVideoEncoder)
try {
mWeakCameraView.get().setVideoEncoder(encoder);
} catch (final Exception e) {
Log.e(TAG, "onPrepared:", e);
}
if (encoder instanceof MediaSurfaceEncoder)
try {
mWeakCameraView.get().setVideoEncoder(encoder);
mUVCCamera.startCapture(((MediaSurfaceEncoder)encoder).getInputSurface());
} catch (final Exception e) {
Log.e(TAG, "onPrepared:", e);
}
}
@Override
public void onStopped(final MediaEncoder encoder) {
if ((encoder instanceof MediaVideoEncoder)
|| (encoder instanceof MediaSurfaceEncoder))
try {
final MainActivity parent = mWeakParent.get();
mWeakCameraView.get().setVideoEncoder(null);
mUVCCamera.stopCapture();
} catch (final Exception e) {
Log.e(TAG, "onPrepared:", e);
}
}
};
视频录制大部分时间都有效,但有时会引发异常。以下是 logcat 的输出:
D/MPEG4Writer(20994): Setting Video track to done
E/MPEG4Writer(20994): The number of recorded samples is 0
W/MPEG4Writer(20994): 0-duration samples found: 1
W/MPEG4Writer(20994): 0-duration samples found: 1
I/MPEG4Writer(20994): Received total/0-length (0/0) buffers and encoded 0 frames. - video
I/MPEG4Writer(20994): Received total/0-length (528/0) buffers and encoded 528 frames. - audio
I/MPEG4Writer(20994): Audio track drift time: 0 us
D/MPEG4Writer(20994): Setting Audio track to done
D/MPEG4Writer(20994): Stopping Video track
D/MPEG4Writer(20994): Stopping Video track source
D/MPEG4Writer(20994): Video track stopped
D/MPEG4Writer(20994): Stopping Audio track
D/MPEG4Writer(20994): Stopping Audio track source
D/MPEG4Writer(20994): Audio track stopped
D/MPEG4Writer(20994): Duration from tracks range is [0, 12084319] us
D/MPEG4Writer(20994): Stopping writer thread
D/MPEG4Writer(20994): 0 chunks are written in the last batch
D/MPEG4Writer(20994): Writer thread stopped
E/MediaEncoder(20994): failed stopping muxer
E/MediaEncoder(20994): java.lang.IllegalStateException: Failed to stop the muxer
E/MediaEncoder(20994): at android.media.MediaMuxer.nativeStop(Native Method)
E/MediaEncoder(20994): at android.media.MediaMuxer.stop(MediaMuxer.java:226)
E/MediaEncoder(20994): at com.serenegiant.encoder.MediaMuxerWrapper.stop(MediaMuxerWrapper.java:149)
E/MediaEncoder(20994): at com.serenegiant.encoder.MediaEncoder.release(MediaEncoder.java:234)
E/MediaEncoder(20994): at com.serenegiant.encoder.MediaAudioEncoder.release(MediaAudioEncoder.java:98)
E/MediaEncoder(20994): at com.serenegiant.encoder.MediaEncoder.run(MediaEncoder.java:154)
E/MediaEncoder(20994): at java.lang.Thread.run(Thread.java:841)
D/MediaEncoder(20994): Encoder thread exiting
有任何想法吗?