基于录制视频的开源项目,https://github.com/jmolsmobile/LandscapeVideoCamera,修改
上面的开源项目实现了使用MediaRecorder和Camera录制视频,如今需要在录制视频的时候增加暂停的功能。
关键思路:因为MediaRecorder只有start(),stop(),prepare(),release()等方法,并没有暂停,所以得在最终生成的文件想办法,
找到一个解决办法就是每一次暂停都会调用stop(),然后释放资源,当用户点了继续的时候重新录制一个新的视频,最后将所有的文件合并为一个文件即可(删除掉刚刚保存过的若干个临时文件)。
1.在RecordingButtonInterface.java中增加三个方法暂停和继续以及选择本地文件。
在VideoCaptureActivity中实现接口方法,对呀的回调中写逻辑,
关于选择本地文件,需要注意在android 4.4(API 19)以下和以上可能会出现不同的情况,需要分别判断。
如下
@Nullable
privateString getvideopath(Intent data) {
if(data ==null) {
return null;
}
Uri mUri = data.getData();
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
if(DocumentsContract.isDocumentUri(this, mUri)) {
if(isMediaDocument(mUri)) {
finalString docId = DocumentsContract.getDocumentId(mUri);
finalString[] split = docId.split(":");
finalString type = split[0];
Uri contentUri =null;
if("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}else if("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}else if("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
finalString selection ="_id=?";
finalString[] selectionArgs =newString[]{
split[1]
};
returngetDataColumn(this, contentUri, selection, selectionArgs);
}else if(isExternalStorageDocument(mUri)) {
finalString docId = DocumentsContract.getDocumentId(mUri);
finalString[] split = docId.split(":");
finalString type = split[0];
if("primary".equalsIgnoreCase(type)) {
returnEnvironment.getExternalStorageDirectory() +"/"+ split[1];
}
}
}
// MediaStore (and general)
else if("content".equalsIgnoreCase(mUri.getScheme())) {
returngetDataColumn(this, mUri,null,null);
}
// File
else if("file".equalsIgnoreCase(mUri.getScheme())) {
returnmUri.getPath();
}
}
// MediaStore (and general)
else if("content".equalsIgnoreCase(mUri.getScheme())) {
returngetDataColumn(this, mUri,null,null);
}
// File
else if("file".equalsIgnoreCase(mUri.getScheme())) {
returnmUri.getPath();
}
return null;
}
privateString getDataColumn(Context mContext, Uri mContentUri, String mSelection, String[] mSelectionArgs) {
Cursor cursor =null;
finalString column ="_data";
finalString[] projection = {
column
};
try{
cursor = mContext.getContentResolver().query(mContentUri, projection, mSelection, mSelectionArgs,
null);
if(cursor !=null&& cursor.moveToFirst()) {
final intindex = cursor.getColumnIndexOrThrow(column);
returncursor.getString(index);
}
}finally{
if(cursor !=null)
cursor.close();
}
return null;
}
private booleanisMediaDocument(Uri mUri) {
return"com.android.providers.media.documents".equals(mUri.getAuthority());
}
private booleanisExternalStorageDocument(Uri mUri) {
return"com.android.externalstorage.documents".equals(mUri.getAuthority());
}
2.关于暂停视频录制
停止上一个视频的录制并且释放资源
//录制视频暂停的处理
@Override
public voidonRecordPause() {
mVideoRecorder.stopRecording(null);
mVideoCaptureView.updateUIRecordingPause(getVideoThumbnail());
releaseAllResources();
}
点击继续的时候
重新定义需要保存文件
VideoFile mVideoFilex =newVideoFile(mVideoFile.getFullPath().replace(".mp4",cout+".mp4"));
mVideoFiles.add(mVideoFilex.getFullPath());
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
mVideoRecorder=newVideoRecorder(this,mCaptureConfiguration, mVideoFilex,newCameraWrapper(newNativeCamera(), display.getRotation()),
mVideoCaptureView.getPreviewSurfaceHolder());
重新开始录制,
完成后调用视频合成方法
视频合成:需要aspectjrt-1.6.1.jar,isoparser-1.0.5.jar
调用
VideoUtils.appendVideo(this,mVideoFile.getFullPath(),mVideoFiles);
内部实现
public static voidappendVideo(Context context,String saveVideoPath,List videos)throwsIOException{
Movie[] inMovies =newMovie[videos.size()];
intindex =0;
for(String video:videos)
{
inMovies[index] = MovieCreator.build(video);
index++;
}
List videoTracks =newLinkedList();
List audioTracks =newLinkedList();
for(Movie m : inMovies) {
for(Track t : m.getTracks()) {
if(t.getHandler().equals("soun")) {
audioTracks.add(t);
}
if(t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Movie result =newMovie();
if(audioTracks.size() >0) {
result.addTrack(newAppendTrack(audioTracks.toArray(newTrack[audioTracks.size()])));
}
if(videoTracks.size() >0) {
result.addTrack(newAppendTrack(videoTracks.toArray(newTrack[videoTracks.size()])));
}
Container out =newDefaultMp4Builder().build(result);
FileChannel fc =newRandomAccessFile(String.format(saveVideoPath),"rw").getChannel();
out.writeContainer(fc);
fc.close();
}
最后不要忘记删除已经录制的文件