1: https://github.com/mabeijianxi/small-video-record
D:\workspace\vedioSDK\small-video-record-master\SmallVideoRecord2
// 秒拍 compile 'com.mabeijianxi:small-video-record2:2.0.3@aar'
2:项目中引入
sourceSets {
main {
jni.srcDirs = []
jniLibs.srcDir "src/main/jniLibs"
}
}
3:项目中使用
<activity android:name="com.mabeijianxi.smallvideorecord2.MediaRecorderActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize"
android:theme="@style/PhotoPickerTheme"/>
<activity android:name=".activity.VideoPlayerActivity"
android:screenOrientation="portrait"/>
<activity android:name=".activity.ToReportActivtiy"
4: 初始化 注意下宽和高是反的
private void initVideo() {
// 清空保存的播放路径
SharedPreferencesUtils.saveString(Contants.VIDEO_URL, "");
// 设置拍摄视频缓存路径
File dcim = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
if (DeviceUtils.isZte()) {
if (dcim.exists()) {
JianXiCamera.setVideoCachePath(dcim + "/mabeijianxi/");
} else {
JianXiCamera.setVideoCachePath(dcim.getPath().replace("/sdcard/",
"/sdcard-ext/")
+ "/mabeijianxi/");
}
} else {
JianXiCamera.setVideoCachePath(dcim + "/mabeijianxi/");
}
// 初始化拍摄,遇到问题可选择开启此标记,以方便生成日志
JianXiCamera.initialize(false, null);
// 录制
DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
display.getMetrics(metrics);
mConfig = new MediaRecorderConfig.Buidler()
.fullScreen(true)
.smallVideoWidth(metrics.heightPixels)
.smallVideoHeight(metrics.widthPixels)
.recordTimeMax(6000)
.recordTimeMin(1000)
.maxFrameRate(20)
.videoBitrate(1500000)
.captureThumbnailsTime(1)
.build();
}
5: 录制完成后 跳转到VideoPlayerActivity 页面,并将录制的视频的本地地址带过去
MediaRecorderActivity.goSmallVideoRecorder(ToRepareActivity.this, VideoPlayerActivity.class.getName(), mConfig);
6:视频播放
package com.welleplus.yfinspection.activity;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.TextView;
import com.mabeijianxi.smallvideorecord2.DeviceUtils;
import com.mabeijianxi.smallvideorecord2.MediaRecorderActivity;
import com.mabeijianxi.smallvideorecord2.MediaRecorderBase;
import com.mabeijianxi.smallvideorecord2.SurfaceVideoView;
import com.welleplus.yfinspection.R;
import com.welleplus.yfinspection.bean.MessageEvent;
import com.welleplus.yfinspection.config.Contants;
import com.welleplus.yfinspection.utils.SharedPreferencesUtils;
import org.greenrobot.eventbus.EventBus;
import java.io.File;
/**
* 通用单独播放界面
*
* @author tangjun
*/
public class VideoPlayerActivity extends AppCompatActivity implements
SurfaceVideoView.OnPlayStateListener, OnErrorListener,
OnPreparedListener, OnClickListener, OnCompletionListener,
OnInfoListener {
/**
* 播放控件
*/
private SurfaceVideoView mVideoView;
/**
* 暂停按钮
*/
private View mPlayerStatus;
private View mLoading;
/**
* 播放路径
*/
private String mPath;
/**
* 是否需要回复播放
*/
private boolean mNeedResume;
private Toolbar mToolbar;
private TextView mTv_toolbar_title;
private TextView mIv_back;
private String mRecordPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 防止锁屏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_video_player);
initView();
initToolBar();
initData();
listener();
}
private void initToolBar() {
setSupportActionBar(mToolbar);
ActionBar supportActionBar = getSupportActionBar();
if(supportActionBar!=null){
supportActionBar.setDisplayShowTitleEnabled(false);
}
mTv_toolbar_title.setText("预览视频");
mToolbar.setBackgroundColor(Color.BLACK);
}
private void initView() {
mToolbar = findViewById(R.id.toolbar);
mTv_toolbar_title = findViewById(R.id.tv_toolbar_title);
mIv_back = findViewById(R.id.iv_back);
mVideoView = (SurfaceVideoView) findViewById(R.id.videoview);
mPlayerStatus = findViewById(R.id.play_status);
mLoading = findViewById(R.id.loading);
}
private void listener() {
mPlayerStatus.setOnClickListener(this);
mVideoView.setOnPreparedListener(this);
mVideoView.setOnPlayStateListener(this);
mVideoView.setOnErrorListener(this);
mVideoView.setOnClickListener(this);
mVideoView.setOnInfoListener(this);
mVideoView.setOnCompletionListener(this);
findViewById(R.id.root).setOnClickListener(this);
mIv_back.setOnClickListener(this);
}
private void initData() {
Intent intent = getIntent();
// 录制页面传递过来的播放
mRecordPath = intent.getStringExtra(MediaRecorderActivity.VIDEO_URI);
String playPath = intent.getStringExtra("path"); // 仅仅是播放按钮传递过来的
if (TextUtils.isEmpty(mRecordPath) && TextUtils.isEmpty(playPath)) {
finish();
return;
}else if(!TextUtils.isEmpty(mRecordPath)){
mPath= mRecordPath;
SharedPreferencesUtils.saveString(Contants.VIDEO_URL,mPath);
}else if(!TextUtils.isEmpty(playPath)){
mPath=playPath;
}
try {
String fileSize = com.welleplus.yfinspection.utils.StringUtils.getFileSize(new File(mPath));
System.out.println("--------size:"+fileSize);
} catch (Exception e) {
e.printStackTrace();
}
int screenWidth = getScreenWidth(this);
int videoHight = (int) (screenWidth / (MediaRecorderBase.SMALL_VIDEO_HEIGHT / (MediaRecorderBase.SMALL_VIDEO_WIDTH * 1.0f)));
mVideoView.getLayoutParams().height = videoHight;
mVideoView.requestLayout();
mVideoView.setVideoPath(mPath);
}
public int getScreenWidth(Activity context) {
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
int W = mDisplayMetrics.widthPixels;
return W;
}
@Override
public void onResume() {
super.onResume();
// if (mVideoView != null && mNeedResume) {
// mNeedResume = false;
// if (mVideoView.isRelease())
// mVideoView.reOpen();
// else
// mVideoView.start();
// }
}
@Override
public void onPause() {
super.onPause();
if (mVideoView != null) {
if (mVideoView.isPlaying()) {
mNeedResume = true;
mVideoView.pause();
}
}
}
@Override
protected void onDestroy() {
if (mVideoView != null) {
mVideoView.release();
mVideoView = null;
}
if(!TextUtils.isEmpty(mRecordPath)){
EventBus.getDefault().post(new MessageEvent(mRecordPath));
}
super.onDestroy();
}
@Override
public void onPrepared(MediaPlayer mp) {
mVideoView.setVolume(SurfaceVideoView.getSystemVolumn(this));
mVideoView.start();
mLoading.setVisibility(View.GONE);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
switch (event.getKeyCode()) {// 跟随系统音量走
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
mVideoView.dispatchKeyEvent(this, event);
break;
}
return super.dispatchKeyEvent(event);
}
@Override
public void onStateChanged(boolean isPlaying) {
// mPlayerStatus.setVisibility(isPlaying ? View.GONE : View.VISIBLE);
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if (!isFinishing()) {
// 播放失败
}
finish();
return false;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.videoview:
break;
case R.id.root:
// mVideoView.pause();
break;
case R.id.play_status:
mVideoView.start();
case R.id.iv_back:
finish();
break;
}
}
@Override
public void onCompletion(MediaPlayer mp) {
if (!isFinishing())
mVideoView.reOpen();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
// 音频和视频数据不正确
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
if (!isFinishing())
mVideoView.pause();
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
if (!isFinishing())
mVideoView.start();
break;
case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
if (DeviceUtils.hasJellyBean()) {
mVideoView.setBackground(null);
} else {
mVideoView.setBackgroundDrawable(null);
}
break;
}
return false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:orientation="vertical">
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mabeijianxi.smallvideorecord2.SurfaceVideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:fitsSystemWindows="true"
android:saveEnabled="true"/>
<ImageView
android:id="@+id/play_status"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:contentDescription="@string/imageview_content_description"
android:src="@drawable/play"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/loading"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>