修复预览闪退的问题
This commit is contained in:
parent
b37a1efb54
commit
f0440075dd
@ -8,6 +8,9 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
@ -74,6 +77,10 @@ public class MediaObject implements Serializable {
|
|||||||
* 获取所有分块
|
* 获取所有分块
|
||||||
*/
|
*/
|
||||||
private LinkedList<MediaPart> mMediaList = new LinkedList<MediaPart>();
|
private LinkedList<MediaPart> mMediaList = new LinkedList<MediaPart>();
|
||||||
|
|
||||||
|
private Lock lock = new ReentrantLock();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 主题
|
* 主题
|
||||||
*/
|
*/
|
||||||
@ -214,6 +221,17 @@ public class MediaObject implements Serializable {
|
|||||||
return duration;
|
return duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void eachPart(Consumer<MediaPart> consumer){
|
||||||
|
if(mMediaList == null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
synchronized (mMediaList){
|
||||||
|
for (MediaPart part : mMediaList) {
|
||||||
|
consumer.accept(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除分块
|
* 删除分块
|
||||||
*/
|
*/
|
||||||
|
@ -236,6 +236,13 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
* 开始录制
|
* 开始录制
|
||||||
*/
|
*/
|
||||||
private void startRecord() {
|
private void startRecord() {
|
||||||
|
// 删除临时目录
|
||||||
|
|
||||||
|
File file = new File(JianXiCamera.getVideoCachePath());
|
||||||
|
if(file.exists() && file.isDirectory()){
|
||||||
|
FileUtils.deleteDir(file);
|
||||||
|
}
|
||||||
|
file.mkdirs();
|
||||||
if (mMediaRecorder != null) {
|
if (mMediaRecorder != null) {
|
||||||
MediaObject.MediaPart part = mMediaRecorder.startRecord();
|
MediaObject.MediaPart part = mMediaRecorder.startRecord();
|
||||||
if (part == null) {
|
if (part == null) {
|
||||||
@ -274,6 +281,11 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setPreviewUI() throws IOException {
|
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();
|
mMediaRecorder.stopPreview();
|
||||||
mCameraSwitch.setVisibility(View.INVISIBLE);
|
mCameraSwitch.setVisibility(View.INVISIBLE);
|
||||||
mRecordLed.setVisibility(View.INVISIBLE);
|
mRecordLed.setVisibility(View.INVISIBLE);
|
||||||
@ -294,7 +306,6 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
mPlayer.setDisplay(mSurfaceView.getHolder());
|
mPlayer.setDisplay(mSurfaceView.getHolder());
|
||||||
mPlayer.reset();
|
mPlayer.reset();
|
||||||
}
|
}
|
||||||
File file = new File(mMediaObject.getOutputTempTranscodingVideoPath());
|
|
||||||
Log.i("player - file",mMediaObject.getOutputTempTranscodingVideoPath());
|
Log.i("player - file",mMediaObject.getOutputTempTranscodingVideoPath());
|
||||||
Log.i("player - size",""+file.length());
|
Log.i("player - size",""+file.length());
|
||||||
mPlayer.setDataSource(mMediaObject.getOutputTempTranscodingVideoPath());
|
mPlayer.setDataSource(mMediaObject.getOutputTempTranscodingVideoPath());
|
||||||
@ -323,9 +334,11 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
part.remove = true;
|
part.remove = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(mPlayer != null && mPlayer.isPlaying()){
|
||||||
mPlayer.reset();
|
mPlayer.reset();
|
||||||
mMediaRecorder.startPreview();
|
mMediaRecorder.startPreview();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void done(){
|
private void done(){
|
||||||
// 复制文件到文件目录
|
// 复制文件到文件目录
|
||||||
@ -338,10 +351,11 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
boolean success = file.renameTo(out);
|
boolean success = file.renameTo(out);
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
if(success){
|
if(success){
|
||||||
File temp = new File( mMediaObject.getOutputDirectory());
|
File temp = new File(JianXiCamera.getVideoCachePath());
|
||||||
if(temp.exists()){
|
if(temp.exists() && temp.isDirectory()){
|
||||||
temp.delete();
|
FileUtils.deleteDir(temp);
|
||||||
}
|
}
|
||||||
|
temp.mkdirs();
|
||||||
bundle.putString(MediaRecorderActivity.OUTPUT_DIRECTORY, dir.getAbsolutePath());
|
bundle.putString(MediaRecorderActivity.OUTPUT_DIRECTORY, dir.getAbsolutePath());
|
||||||
bundle.putString(MediaRecorderActivity.VIDEO_URI, out.getAbsolutePath());
|
bundle.putString(MediaRecorderActivity.VIDEO_URI, out.getAbsolutePath());
|
||||||
}else {
|
}else {
|
||||||
@ -424,6 +438,11 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
public void onEncodeComplete() {
|
public void onEncodeComplete() {
|
||||||
this.completed = true;
|
this.completed = true;
|
||||||
Log.d("onEncodeComplete");
|
Log.d("onEncodeComplete");
|
||||||
|
try {
|
||||||
|
setPreviewUI();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -495,11 +514,7 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
Log.d("onRecordFinishedListener:拍摄完成");
|
Log.d("onRecordFinishedListener:拍摄完成");
|
||||||
startState = false;
|
startState = false;
|
||||||
stopRecord();
|
stopRecord();
|
||||||
try {
|
this.mRecordController.setVisibility(View.INVISIBLE);
|
||||||
setPreviewUI();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -142,8 +142,6 @@ public abstract class MediaRecorderBase implements Callback, PreviewCallback, IM
|
|||||||
*/
|
*/
|
||||||
protected MediaObject mMediaObject;
|
protected MediaObject mMediaObject;
|
||||||
|
|
||||||
protected Lock lock = new ReentrantLock();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转码监听器
|
* 转码监听器
|
||||||
*/
|
*/
|
||||||
@ -499,10 +497,8 @@ public abstract class MediaRecorderBase implements Callback, PreviewCallback, IM
|
|||||||
*/
|
*/
|
||||||
private void stopAllRecord() {
|
private void stopAllRecord() {
|
||||||
mRecording = false;
|
mRecording = false;
|
||||||
if(this.lock.tryLock()){
|
|
||||||
try {
|
|
||||||
if (mMediaObject != null && mMediaObject.getMedaParts() != null) {
|
if (mMediaObject != null && mMediaObject.getMedaParts() != null) {
|
||||||
for (MediaObject.MediaPart part : mMediaObject.getMedaParts()) {
|
mMediaObject.eachPart(part->{
|
||||||
if (part != null && part.recording) {
|
if (part != null && part.recording) {
|
||||||
part.recording = false;
|
part.recording = false;
|
||||||
part.endTime = System.currentTimeMillis();
|
part.endTime = System.currentTimeMillis();
|
||||||
@ -515,11 +511,7 @@ public abstract class MediaRecorderBase implements Callback, PreviewCallback, IM
|
|||||||
mMediaObject.removePart(part, true);
|
mMediaObject.removePart(part, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}finally {
|
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user