diff --git a/src/android/MediaObject.java b/src/android/MediaObject.java index aff3477..054aea0 100644 --- a/src/android/MediaObject.java +++ b/src/android/MediaObject.java @@ -8,6 +8,9 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.Serializable; import java.util.LinkedList; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Consumer; @SuppressWarnings("serial") @@ -74,6 +77,10 @@ public class MediaObject implements Serializable { * 获取所有分块 */ private LinkedList mMediaList = new LinkedList(); + + private Lock lock = new ReentrantLock(); + + /** * 主题 */ @@ -214,6 +221,17 @@ public class MediaObject implements Serializable { return duration; } + public void eachPart(Consumer consumer){ + if(mMediaList == null){ + return; + } + synchronized (mMediaList){ + for (MediaPart part : mMediaList) { + consumer.accept(part); + } + } + } + /** * 删除分块 */ diff --git a/src/android/MediaRecorderActivity.java b/src/android/MediaRecorderActivity.java index a335893..5d6fb03 100644 --- a/src/android/MediaRecorderActivity.java +++ b/src/android/MediaRecorderActivity.java @@ -236,6 +236,13 @@ public class MediaRecorderActivity extends Activity implements * 开始录制 */ private void startRecord() { + // 删除临时目录 + + File file = new File(JianXiCamera.getVideoCachePath()); + if(file.exists() && file.isDirectory()){ + FileUtils.deleteDir(file); + } + file.mkdirs(); if (mMediaRecorder != null) { MediaObject.MediaPart part = mMediaRecorder.startRecord(); if (part == null) { @@ -274,6 +281,11 @@ public class MediaRecorderActivity extends Activity implements } private void setPreviewUI() throws IOException { + File file = new File(mMediaObject.getOutputTempTranscodingVideoPath()); + if(!file.exists() || file.length() == 0){ + Toast.makeText(this,"录制失败",3000).show(); + return; + } mMediaRecorder.stopPreview(); mCameraSwitch.setVisibility(View.INVISIBLE); mRecordLed.setVisibility(View.INVISIBLE); @@ -294,7 +306,6 @@ public class MediaRecorderActivity extends Activity implements mPlayer.setDisplay(mSurfaceView.getHolder()); mPlayer.reset(); } - File file = new File(mMediaObject.getOutputTempTranscodingVideoPath()); Log.i("player - file",mMediaObject.getOutputTempTranscodingVideoPath()); Log.i("player - size",""+file.length()); mPlayer.setDataSource(mMediaObject.getOutputTempTranscodingVideoPath()); @@ -323,8 +334,10 @@ public class MediaRecorderActivity extends Activity implements part.remove = true; } } - mPlayer.reset(); - mMediaRecorder.startPreview(); + if(mPlayer != null && mPlayer.isPlaying()){ + mPlayer.reset(); + mMediaRecorder.startPreview(); + } } private void done(){ @@ -338,10 +351,11 @@ public class MediaRecorderActivity extends Activity implements boolean success = file.renameTo(out); Bundle bundle = new Bundle(); if(success){ - File temp = new File( mMediaObject.getOutputDirectory()); - if(temp.exists()){ - temp.delete(); + File temp = new File(JianXiCamera.getVideoCachePath()); + if(temp.exists() && temp.isDirectory()){ + FileUtils.deleteDir(temp); } + temp.mkdirs(); bundle.putString(MediaRecorderActivity.OUTPUT_DIRECTORY, dir.getAbsolutePath()); bundle.putString(MediaRecorderActivity.VIDEO_URI, out.getAbsolutePath()); }else { @@ -424,6 +438,11 @@ public class MediaRecorderActivity extends Activity implements public void onEncodeComplete() { this.completed = true; Log.d("onEncodeComplete"); + try { + setPreviewUI(); + } catch (IOException e) { + e.printStackTrace(); + } } /** @@ -495,11 +514,7 @@ public class MediaRecorderActivity extends Activity implements Log.d("onRecordFinishedListener:拍摄完成"); startState = false; stopRecord(); - try { - setPreviewUI(); - } catch (IOException e) { - e.printStackTrace(); - } + this.mRecordController.setVisibility(View.INVISIBLE); } @Override diff --git a/src/android/MediaRecorderBase.java b/src/android/MediaRecorderBase.java index 87cf8d0..b56d901 100644 --- a/src/android/MediaRecorderBase.java +++ b/src/android/MediaRecorderBase.java @@ -142,8 +142,6 @@ public abstract class MediaRecorderBase implements Callback, PreviewCallback, IM */ protected MediaObject mMediaObject; - protected Lock lock = new ReentrantLock(); - /** * 转码监听器 */ @@ -498,29 +496,23 @@ public abstract class MediaRecorderBase implements Callback, PreviewCallback, IM * 停止所有块的写入 */ private void stopAllRecord() { - mRecording = false; - if(this.lock.tryLock()){ - try { - if (mMediaObject != null && mMediaObject.getMedaParts() != null) { - for (MediaObject.MediaPart part : mMediaObject.getMedaParts()) { - if (part != null && part.recording) { - part.recording = false; - part.endTime = System.currentTimeMillis(); - part.duration = (int) (part.endTime - part.startTime); - part.cutStartTime = 0; - part.cutEndTime = part.duration; - // 检测视频大小是否大于0,否则丢弃(注意有音频没视频的情况下音频也会丢弃) - File videoFile = new File(part.mediaPath); - if (videoFile != null && videoFile.length() < 1) { - mMediaObject.removePart(part, true); - } - } - } + mRecording = false; + if (mMediaObject != null && mMediaObject.getMedaParts() != null) { + mMediaObject.eachPart(part->{ + if (part != null && part.recording) { + part.recording = false; + part.endTime = System.currentTimeMillis(); + part.duration = (int) (part.endTime - part.startTime); + part.cutStartTime = 0; + part.cutEndTime = part.duration; + // 检测视频大小是否大于0,否则丢弃(注意有音频没视频的情况下音频也会丢弃) + File videoFile = new File(part.mediaPath); + if (videoFile != null && videoFile.length() < 1) { + mMediaObject.removePart(part, true); } - }finally { - lock.unlock(); } - } + }); + } } /**