为了兼容使用,在之前这个基础上进行一下修改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),