修复权限请求造成的闪退 修复预览界面按钮位置 处理预览结束后后台播放的问题

This commit is contained in:
zher52 2022-03-09 17:58:16 +08:00
parent e1f51d5070
commit dc42180d59
3 changed files with 97 additions and 83 deletions

View File

@ -32,16 +32,9 @@ import java.util.Map;
*/
public class CaptureCordovaPlugin extends CordovaPlugin {
public static final int REQUEST_CODE = 0x777578;
private final int PERMISSION_REQUEST_CODE = 0x001;
private static String LOG_TAG = "CAPTURE_PLUGIN";
private CallbackContext callbackContext;
private Map<String,Integer> param = new HashMap<>();
private String [] permissions = {
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
@Override
protected void pluginInitialize() {
@ -66,24 +59,20 @@ public class CaptureCordovaPlugin extends CordovaPlugin {
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
if (action.equals("capture")) {
if(!hasPermisssion()) {
requestPermissions(PERMISSION_REQUEST_CODE);
} else {
int width = args.optInt(0, 360);
int height = args.optInt(1, 640 );
int frameRate = args.optInt(2, 30 );
int bitRate = args.optInt(3, width * height * 6 );
int limit = args.optInt(4, 3);
int duration = args.optInt(5, 10);
this.param.put("width",width);
this.param.put("height",height);
this.param.put("frameRate",frameRate);
this.param.put("bitRate",bitRate);
this.param.put("limit",limit);
this.param.put("duration",duration);
this.capture();
}
return true;
int width = args.optInt(0, 360);
int height = args.optInt(1, 640 );
int frameRate = args.optInt(2, 30 );
int bitRate = args.optInt(3, width * height * 6 );
int limit = args.optInt(4, 3);
int duration = args.optInt(5, 10);
this.param.put("width",width);
this.param.put("height",height);
this.param.put("frameRate",frameRate);
this.param.put("bitRate",bitRate);
this.param.put("limit",limit);
this.param.put("duration",duration);
this.capture();
return true;
}
return false;
}
@ -132,51 +121,7 @@ public class CaptureCordovaPlugin extends CordovaPlugin {
}
}
/**
* check application's permissions
*/
public boolean hasPermisssion() {
for(String p : permissions) {
if(!PermissionHelper.hasPermission(this, p)) {
return false;
}
}
return true;
}
/**
* We override this so that we can access the permissions variable, which no longer exists in
* the parent class, since we can't initialize it reliably in the constructor!
*
* @param requestCode The code to get request action
*/
public void requestPermissions(int requestCode) {
PermissionHelper.requestPermissions(this, requestCode, permissions);
}
/**
* processes the result of permission request
*
* @param requestCode The code to get request action
* @param permissions The collection of permissions
* @param grantResults The result of grant
*/
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException {
PluginResult result;
for (int r : grantResults) {
if (r == PackageManager.PERMISSION_DENIED) {
Log.d(LOG_TAG, "Permission Denied!");
result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION);
this.callbackContext.sendPluginResult(result);
return;
}
}
if (requestCode == PERMISSION_REQUEST_CODE) {
this.capture();
}
}
/**
* This plugin launches an external Activity when the camera is opened, so we

View File

@ -1,10 +1,13 @@
package com.mabeijianxi.smallvideorecord2;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceView;
@ -24,6 +27,8 @@ import com.mabeijianxi.smallvideorecord2.model.MediaRecorderConfig;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
/**
* 视频录制
@ -38,6 +43,11 @@ public class MediaRecorderActivity extends Activity implements
MediaPlayer.OnSeekCompleteListener,
MediaPlayer.OnVideoSizeChangedListener{
protected static final int REQ_PERMISSION_CODE = 0x1000;
protected int mGrantedCount = 0;
private int RECORD_TIME_MIN = (int) (1.5f * 1000);
/**
* 录制最长时间
@ -109,8 +119,9 @@ public class MediaRecorderActivity extends Activity implements
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 防止锁屏
initData();
loadViews();
if (checkPermission()) {
onPermissionGranted();
}
}
private void initData() {
@ -223,12 +234,13 @@ public class MediaRecorderActivity extends Activity implements
@Override
public void onResume() {
super.onResume();
if (mMediaRecorder == null) {
initMediaRecorder();
} else {
mRecordLed.setChecked(false);
mMediaRecorder.prepare();
if(checkPermission()){
if (mMediaRecorder == null) {
initMediaRecorder();
} else {
mRecordLed.setChecked(false);
mMediaRecorder.prepare();
}
}
}
@ -478,6 +490,59 @@ public class MediaRecorderActivity extends Activity implements
if(mMediaRecorder != null){
mMediaRecorder.release();
}
if(mPlayer != null && mPlayer.isPlaying()){
mPlayer.stop();
mPlayer = null;
}
}
private void onPermissionGranted() {
initData();
loadViews();
}
protected boolean checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<String> permissions = new ArrayList<>();
if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.CAMERA)) {
permissions.add(Manifest.permission.CAMERA);
}
if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.RECORD_AUDIO)) {
permissions.add(Manifest.permission.RECORD_AUDIO);
}
if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissions.size() != 0) {
requestPermissions(permissions.toArray(new String[0]), REQ_PERMISSION_CODE);
return false;
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQ_PERMISSION_CODE:
for (int ret : grantResults) {
if (PackageManager.PERMISSION_GRANTED == ret) {
mGrantedCount ++;
}
}
if (mGrantedCount == permissions.length) {
onPermissionGranted();
} else {
Toast.makeText(this, "录制视频缺少必要的权限", Toast.LENGTH_SHORT).show();
}
mGrantedCount = 0;
break;
default:
break;
}
}
@Override

View File

@ -93,18 +93,22 @@
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_marginLeft="70dp"
android:visibility="invisible"
android:background="@drawable/record_redo" />
android:layout_centerHorizontal="true"
android:layout_alignRight="@+id/record_controller"
android:layout_marginRight="108dp"
android:background="@drawable/record_redo"
android:visibility="invisible" />
<TextView
android:id="@+id/record_ok"
android:layout_width="80dp"
android:layout_height="80dp"
android:visibility="invisible"
android:layout_centerVertical="true"
android:layout_marginLeft="260dp"
android:background="@drawable/record_ok" />
android:layout_centerHorizontal="true"
android:layout_alignLeft="@+id/record_controller"
android:layout_marginLeft="108dp"
android:background="@drawable/record_ok"
android:visibility="invisible" />
</RelativeLayout>