本項目使用Androidstudio開發(fā)工具
引入權(quán)限
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
引入依賴
//視頻錄制
implementation 'cjt.library.wheel:camera:1.1.9'
//視頻播放
api('com.shuyu:GSYVideoPlayer:6.0.1') {
exclude group: 'com.android.support:appcompat-v7:27.0.0'
}
api('com.shuyu:gsyVideoPlayer-java:6.0.1') {
exclude group: 'com.android.support:appcompat-v7:27.0.0'
}
api('com.shuyu:GSYVideoPlayer-exo2:6.0.1') {
exclude group: 'com.android.support:appcompat-v7:27.0.0'
}
maven {
url 'https://dl.bintray.com/cjt/maven'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍視頻"
/>
<ImageView
android:id="@+id/show_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
MainActivity
package heath.com.recordvideo;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import heath.com.recordvideo.utils.ACache;
public class MainActivity extends AppCompatActivity {
private Button button;
public static ACache aCache;
public static File cache;
private static ImageView mShowImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
open();
init();
button = this.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
MainActivity.this,
RecordVideoActivity.class);
startActivityForResult(intent, 0);
}
});
}
public static void update(Bitmap bitmap){
mShowImg.setImageBitmap(bitmap);
}
private void init() {
aCache = ACache.get(this);
mShowImg = findViewById(R.id.show_img);
cache = new File(Environment.getExternalStorageDirectory() + "/" + getPackageName() + "/nim", "cache");
if (!cache.exists()) {
cache.mkdirs();
}
}
private void open() {
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_PHONE_STATE, android.Manifest.permission.CAMERA};
//驗證是否許可權(quán)限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申請權(quán)限
this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
}
}
}
}
}
視頻錄制和拍照
activity_record_video.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.cjt2325.cameralibrary.JCameraView
android:id="@+id/jcv_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:duration_max="10000"
app:iconMargin="20dp"
app:iconSize="30dp"
app:iconSrc="@drawable/icon_switch"/>
</RelativeLayout>
RecordVideoActivity
package heath.com.recordvideo;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ClickListener;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
public class RecordVideoActivity extends AppCompatActivity {
private JCameraView mJcvVideo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record_video);
initView();
initListener();
init();
}
@Override
protected void onResume() {
super.onResume();
mJcvVideo.onResume();
}
@Override
protected void onPause() {
super.onPause();
mJcvVideo.onPause();
}
private void initView() {
mJcvVideo = findViewById(R.id.jcv_video);
}
private void initListener() {
mJcvVideo.setErrorLisenter(new ErrorListener() {
@Override
public void onError() {
//打開Camera失敗回調(diào)
Log.i("CJT", "open camera error");
}
@Override
public void AudioPermissionError() {
//沒有錄取權(quán)限回調(diào)
Log.i("CJT", "AudioPermissionError");
}
});
mJcvVideo.setJCameraLisenter(new JCameraListener() {
@Override
public void captureSuccess(Bitmap bitmap) {
//獲取圖片bitmap
Log.i("CJT", "bitmap = " + bitmap);
MainActivity.update(bitmap);
finish();
}
@Override
public void recordSuccess(String url, Bitmap firstFrame) {
//獲取視頻路徑
Log.i("CJT", "url = " + url);
Intent intent = new Intent(
RecordVideoActivity.this,
ShowVideo.class);
intent.putExtra("path", url);
startActivityForResult(intent, 0);
finish();
}
});
mJcvVideo.setLeftClickListener(new ClickListener() {
@Override
public void onClick() {
finish();
}
});
mJcvVideo.setRightClickListener(new ClickListener() {
@Override
public void onClick() {
Toast.makeText(RecordVideoActivity.this, "Right", Toast.LENGTH_SHORT).show();
}
});
}
private void init() {
if (Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
} else {
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(option);
}
//設置視頻保存路徑
mJcvVideo.setSaveVideoPath(Environment.getExternalStorageDirectory() + "/" + getPackageName() + "/nim/"+"cache");
//設置只能錄像或只能拍照或兩種都可以(默認兩種都可以)
mJcvVideo.setFeatures(JCameraView.BUTTON_STATE_BOTH);
//設置視頻質(zhì)量
mJcvVideo.setMediaQuality(JCameraView.MEDIA_QUALITY_HIGH);
}
}
視頻播放
activity_show_video.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_play"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
android:id="@+id/video_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
ShowVideo
package heath.com.recordvideo;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
public class ShowVideo extends AppCompatActivity {
StandardGSYVideoPlayer videoPlayer;
OrientationUtils orientationUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_video);
init();
}
private void init() {
Intent intent = getIntent();
videoPlayer = this.findViewById(R.id.video_player);
String source1 = (String) intent.getSerializableExtra("path");
videoPlayer.setUp(source1, true, "測試視頻");
//增加封面
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
videoPlayer.setThumbImageView(imageView);
//增加title
videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
//設置返回鍵
videoPlayer.getBackButton().setVisibility(View.VISIBLE);
//設置旋轉(zhuǎn)
orientationUtils = new OrientationUtils(this, videoPlayer);
//設置全屏按鍵功能,這是使用的是選擇屏幕黍少,而不是全屏
videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orientationUtils.resolveByClick();
}
});
//是否可以滑動調(diào)整
videoPlayer.setIsTouchWiget(true);
//設置返回按鍵功能
videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
videoPlayer.startPlayLogic();
}
@Override
protected void onPause() {
super.onPause();
videoPlayer.onVideoPause();
}
@Override
protected void onResume() {
super.onResume();
videoPlayer.onVideoResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
GSYVideoManager.releaseAllVideos();
if (orientationUtils != null)
orientationUtils.releaseListener();
}
@Override
public void onBackPressed() {
//先返回正常狀態(tài)
if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
videoPlayer.getFullscreenButton().performClick();
return;
}
//釋放所有
videoPlayer.setVideoAllCallBack(null);
super.onBackPressed();
}
}
完整項目下載地址:https://download.csdn.net/download/qq_32090185/11109780