From 1cdc139d0c2066bbfb39a5520249b7ae089ebfa4 Mon Sep 17 00:00:00 2001 From: zher52 Date: Thu, 28 Oct 2021 12:43:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=88=E4=BA=8E=E5=AE=8C=E6=88=90=E5=95=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/android/CircleButtonView.java | 1 - src/android/MediaRecorderActivity.java | 98 ++++++++++++++++++++++---- src/android/MediaRecorderBase.java | 6 +- src/android/MediaRecorderNative.java | 9 ++- 4 files changed, 98 insertions(+), 16 deletions(-) diff --git a/src/android/CircleButtonView.java b/src/android/CircleButtonView.java index 0d31a65..bbf9d67 100644 --- a/src/android/CircleButtonView.java +++ b/src/android/CircleButtonView.java @@ -256,7 +256,6 @@ public class CircleButtonView extends View{ mProgressAni.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { - Log.d("getAnimatedValue===>"+animation.getAnimatedValue()); mCurrentProgress= getDuration ((float)animation.getAnimatedValue()); invalidate(); } diff --git a/src/android/MediaRecorderActivity.java b/src/android/MediaRecorderActivity.java index ca27805..a335893 100644 --- a/src/android/MediaRecorderActivity.java +++ b/src/android/MediaRecorderActivity.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; +import android.media.MediaPlayer; import android.os.Bundle; import android.view.MotionEvent; import android.view.SurfaceView; @@ -21,6 +22,7 @@ import com.mabeijianxi.smallvideorecord2.model.MediaObject; import com.mabeijianxi.smallvideorecord2.model.MediaRecorderConfig; import java.io.File; +import java.io.IOException; import java.net.URI; /** @@ -28,7 +30,13 @@ import java.net.URI; */ public class MediaRecorderActivity extends Activity implements MediaRecorderBase.OnErrorListener, OnClickListener, MediaRecorderBase.OnPreparedListener, - MediaRecorderBase.OnEncodeListener, CircleButtonView.OnLongClickListener { + MediaRecorderBase.OnEncodeListener, CircleButtonView.OnLongClickListener, + MediaPlayer.OnPreparedListener, + MediaPlayer.OnCompletionListener, + MediaPlayer.OnErrorListener, + MediaPlayer.OnInfoListener, + MediaPlayer.OnSeekCompleteListener, + MediaPlayer.OnVideoSizeChangedListener{ private int RECORD_TIME_MIN = (int) (1.5f * 1000); /** @@ -63,6 +71,7 @@ public class MediaRecorderActivity extends Activity implements */ private SurfaceView mSurfaceView; + /** * SDK视频录制对象 */ @@ -72,6 +81,8 @@ public class MediaRecorderActivity extends Activity implements */ private MediaObject mMediaObject; + private MediaPlayer mPlayer; + /** * 视屏地址 */ @@ -262,8 +273,8 @@ public class MediaRecorderActivity extends Activity implements mRecordLed.setEnabled(true); } - private void setPreviewUI(){ - //TODO 预览界面 + private void setPreviewUI() throws IOException { + mMediaRecorder.stopPreview(); mCameraSwitch.setVisibility(View.INVISIBLE); mRecordLed.setVisibility(View.INVISIBLE); mRecordController.setVisibility(View.INVISIBLE); @@ -271,6 +282,24 @@ public class MediaRecorderActivity extends Activity implements redoButton.setVisibility(View.VISIBLE); okButton.setVisibility(View.VISIBLE); + + if(mPlayer == null){ + mPlayer = new MediaPlayer(); + mPlayer.setOnCompletionListener(this); + mPlayer.setOnErrorListener(this); + mPlayer.setOnInfoListener(this); + mPlayer.setOnPreparedListener(this); + mPlayer.setOnSeekCompleteListener(this); + mPlayer.setOnVideoSizeChangedListener(this); + 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()); + //使用手机本地视频 + mPlayer.prepareAsync(); } private void setRecordUI(){ @@ -278,7 +307,7 @@ public class MediaRecorderActivity extends Activity implements mRecordLed.setVisibility(View.VISIBLE); mRecordController.setVisibility(View.VISIBLE); mTitleBack.setVisibility(View.VISIBLE); - + mSurfaceView.setVisibility(View.VISIBLE); redoButton.setVisibility(View.INVISIBLE); okButton.setVisibility(View.INVISIBLE); } @@ -294,6 +323,8 @@ public class MediaRecorderActivity extends Activity implements part.remove = true; } } + mPlayer.reset(); + mMediaRecorder.startPreview(); } private void done(){ @@ -378,10 +409,12 @@ public class MediaRecorderActivity extends Activity implements @Override public void onEncodeStart() { + Log.d("EncodeStart"); } @Override public void onEncodeProgress(int progress) { + Log.d("onEncodeProgress:"+progress); } /** @@ -390,6 +423,7 @@ public class MediaRecorderActivity extends Activity implements @Override public void onEncodeComplete() { this.completed = true; + Log.d("onEncodeComplete"); } /** @@ -397,6 +431,7 @@ public class MediaRecorderActivity extends Activity implements */ @Override public void onEncodeError() { + Log.e("onEncodeError"); Toast.makeText(this, getString(getId("record_video_transcoding_faild", "string")), Toast.LENGTH_SHORT).show(); finish(); @@ -404,32 +439,32 @@ public class MediaRecorderActivity extends Activity implements @Override public void onVideoError(int what, int extra) { - + Log.e("onVideoError"); } @Override public void onAudioError(int what, String message) { - + Log.e("onAudioError"); } @Override public void onPrepared() { + Log.d("onPrepared"); initSurfaceView(); } - public void onFinished() { - finish(); - } - @Override protected void onDestroy() { super.onDestroy(); - mMediaRecorder.release(); + if(mMediaRecorder != null){ + mMediaRecorder.release(); + } } @Override protected void onStop() { super.onStop(); + Log.d("onStop"); if (mMediaRecorder instanceof MediaRecorderNative) { ((MediaRecorderNative) mMediaRecorder).activityStop(); } @@ -460,6 +495,45 @@ public class MediaRecorderActivity extends Activity implements Log.d("onRecordFinishedListener:拍摄完成"); startState = false; stopRecord(); - setPreviewUI(); + try { + setPreviewUI(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void onCompletion(MediaPlayer mp) { + Log.d("player onCompletion"); + } + + @Override + public boolean onError(MediaPlayer mp, int what, int extra) { + return false; + } + + @Override + public boolean onInfo(MediaPlayer mp, int what, int extra) { + Log.d("player onInfo"); + return false; + } + + @Override + public void onPrepared(MediaPlayer mp) { + Log.d("player onPrepared"); + if(mPlayer !=null) { + mPlayer.setLooping(true); + mPlayer.start(); + } + } + + @Override + public void onSeekComplete(MediaPlayer mp) { + Log.d("player onSeekComplete"); + } + + @Override + public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { + Log.d("player onVideoSizeChanged"); } } diff --git a/src/android/MediaRecorderBase.java b/src/android/MediaRecorderBase.java index 1824478..87cf8d0 100644 --- a/src/android/MediaRecorderBase.java +++ b/src/android/MediaRecorderBase.java @@ -430,6 +430,10 @@ public abstract class MediaRecorderBase implements Callback, PreviewCallback, IM startPreview(); } + public void reset(){ + + } + /** * 设置视频临时存储文件夹 * @@ -558,11 +562,9 @@ public abstract class MediaRecorderBase implements Callback, PreviewCallback, IM // mParameters.setPreviewFpsRange(15 * 1000, 20 * 1000); // TODO 设置浏览尺寸 boolean findWidth = false; - Log.d("screenHeight: " + screenHeight + ", screenWidth: " + screenWidth + ", ratio: " + (1.0d * screenHeight / screenWidth)); double ratio = 1.0d * screenHeight / screenWidth; for (int i = mSupportedPreviewSizes.size() - 1; i >= 0; i--) { Size size = mSupportedPreviewSizes.get(i); - Log.d("width: " + size.width + ", height: " + size.height + ", ratio: " + (1.0d * size.width / size.height) + ", target: " + ratio); if (size.height >= SMALL_VIDEO_HEIGHT && (1.0d * size.width / size.height) == ratio) { mSupportedPreviewWidth = size.width; checkFullWidth(mSupportedPreviewWidth,SMALL_VIDEO_WIDTH); diff --git a/src/android/MediaRecorderNative.java b/src/android/MediaRecorderNative.java index 55c05c2..f07ec03 100644 --- a/src/android/MediaRecorderNative.java +++ b/src/android/MediaRecorderNative.java @@ -9,6 +9,8 @@ import android.util.Log; import com.mabeijianxi.smallvideorecord2.jniinterface.FFmpegBridge; import com.mabeijianxi.smallvideorecord2.model.MediaObject; +import java.io.File; + /** * 视频录制:边录制边底层处理视频(旋转和裁剪) @@ -35,7 +37,12 @@ public class MediaRecorderNative extends MediaRecorderBase implements MediaRecor } else { vCustomFormat=FFmpegBridge.ROTATE_270_CROP_LT_MIRROR_LR; } - + File file = new File(mMediaObject.getOutputDirectory()); + if(!file.exists() || !file.isDirectory() || !file.delete()){ + file.mkdirs(); + } + Log.d("output",file.getAbsolutePath()); + Log.d("output-files",""+file.listFiles()); FFmpegBridge.prepareJXFFmpegEncoder( mMediaObject.getOutputDirectory(), mMediaObject.getBaseName(),vCustomFormat, mSupportedPreviewWidth, SMALL_VIDEO_HEIGHT, SMALL_VIDEO_WIDTH, SMALL_VIDEO_HEIGHT, mFrameRate, mVideoBitrate); MediaObject.MediaPart result = null;