为了兼容使用,在之前这个基础上进行一下修改android 录像本地网络传输保存成mp4文件
后面录像要加水印,使用mediarecorder不好实现,同时mediarecorder录像保存的文件相对来说会稍微大一点.改用medicodec+mediamuxer方式。
系统修改
系统版本android7.1,低版本的MediaMuxer.java还没有FileDescriptor接口增加这个接口
1.frameworks/base/media/java/android/media/MediaMuxer.java
public MediaMuxer(@NonNull String path, @Format int format) throws IOException {
if (path == null) {
throw new IllegalArgumentException("path must not be null");
}
if (format != OutputFormat.MUXER_OUTPUT_MPEG_4 &&
format != OutputFormat.MUXER_OUTPUT_WEBM) {
throw new IllegalArgumentException("format is invalid");
}
// Use RandomAccessFile so we can open the file with RW access;
// RW access allows the native writer to memory map the output file.
RandomAccessFile file = null;
try {
file = new RandomAccessFile(path, "rws");
FileDescriptor fd = file.getFD();
mNativeObject = nativeSetup(fd, format);
mState = MUXER_STATE_INITIALIZED;
mCloseGuard.open("release");
} finally {
if (file != null) {
file.close();
}
}
SystemProperties.set("ease.mp4seek.mode","false");//add by hclydao
}
//add by hclydao
public MediaMuxer(@NonNull FileDescriptor fd, @Format int format) throws IOException {
if (format != OutputFormat.MUXER_OUTPUT_MPEG_4 &&
format != OutputFormat.MUXER_OUTPUT_WEBM) {
throw new IllegalArgumentException("format is invalid");
}
// Use RandomAccessFile so we can open the file with RW access;
// RW access allows the native writer to memory map the output file.
mNativeObject = nativeSetup(fd, format);
mState = MUXER_STATE_INITIALIZED;
mCloseGuard.open("release");
SystemProperties.set("ease.mp4seek.mode","true");
}
增加public MediaMuxer(@NonNull FileDescriptor fd, @Format int format) 接口,然后设置了一个"ease.mp4seek.mode",这个后面用来判断是写文件,还是通过网络传输
2.frameworks/av/include/media/stagefright/MPEG4Writer.h
void myseek(int fd, off64_t seek, int whence);
bool myseekMode;
增加seek操作,网络传输无法像文件那种进行回写,所以处理一下文件seek的操作,增加myseek接口
3. frameworks/av/media/libstagefright/MPEG4Writer.cpp
MPEG4Writer::MPEG4Writer(int fd)
: mFd(dup(fd)),
mInitCheck(mFd < 0? NO_INIT: OK),
mIsRealTimeRecording(true),
mUse4ByteNalLe

这篇博客介绍了在Android系统上优化录像保存为MP4文件的方法,特别是针对网络传输的场景。作者指出,使用MediaRecorder可能会导致文件较大且不便于加水印,因此转向MediaCodec+MediaMuxer方案。系统层面,针对Android 7.1及更低版本,增加了MediaMuxer的FileDescriptor接口和seek操作。在应用操作中,通过LocalServer获取FileDescriptor初始化MediaMuxer,并在录像完成后使用fsync强制同步,解决Android 10中的文件延时问题。此外,还讨论了双路摄像头录像时可能出现的文件只有几帧图像的异常情况。
最低0.47元/天 解锁文章
1133

被折叠的 条评论
为什么被折叠?



