From dc42180d592e01903f4db85144c31febdd39baed Mon Sep 17 00:00:00 2001 From: zher52 Date: Wed, 9 Mar 2022 17:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9D=83=E9=99=90=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E9=80=A0=E6=88=90=E7=9A=84=E9=97=AA=E9=80=80=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A2=84=E8=A7=88=E7=95=8C=E9=9D=A2=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E4=BD=8D=E7=BD=AE=20=E5=A4=84=E7=90=86=E9=A2=84?= =?UTF-8?q?=E8=A7=88=E7=BB=93=E6=9D=9F=E5=90=8E=E5=90=8E=E5=8F=B0=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/android/CaptureCordovaPlugin.java | 83 ++++--------------- src/android/MediaRecorderActivity.java | 81 ++++++++++++++++-- .../res/layout/activity_media_recorder.xml | 16 ++-- 3 files changed, 97 insertions(+), 83 deletions(-) diff --git a/src/android/CaptureCordovaPlugin.java b/src/android/CaptureCordovaPlugin.java index 26ca139..96452ef 100644 --- a/src/android/CaptureCordovaPlugin.java +++ b/src/android/CaptureCordovaPlugin.java @@ -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 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 diff --git a/src/android/MediaRecorderActivity.java b/src/android/MediaRecorderActivity.java index d8b6bbb..c79ab7a 100644 --- a/src/android/MediaRecorderActivity.java +++ b/src/android/MediaRecorderActivity.java @@ -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 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 diff --git a/src/android/res/layout/activity_media_recorder.xml b/src/android/res/layout/activity_media_recorder.xml index 691f073..2e82c6a 100644 --- a/src/android/res/layout/activity_media_recorder.xml +++ b/src/android/res/layout/activity_media_recorder.xml @@ -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" /> + android:layout_centerHorizontal="true" + android:layout_alignLeft="@+id/record_controller" + android:layout_marginLeft="108dp" + android:background="@drawable/record_ok" + android:visibility="invisible" />