修复权限请求造成的闪退 修复预览界面按钮位置 处理预览结束后后台播放的问题
This commit is contained in:
parent
e1f51d5070
commit
dc42180d59
@ -32,16 +32,9 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class CaptureCordovaPlugin extends CordovaPlugin {
|
public class CaptureCordovaPlugin extends CordovaPlugin {
|
||||||
public static final int REQUEST_CODE = 0x777578;
|
public static final int REQUEST_CODE = 0x777578;
|
||||||
private final int PERMISSION_REQUEST_CODE = 0x001;
|
|
||||||
private static String LOG_TAG = "CAPTURE_PLUGIN";
|
private static String LOG_TAG = "CAPTURE_PLUGIN";
|
||||||
private CallbackContext callbackContext;
|
private CallbackContext callbackContext;
|
||||||
private Map<String,Integer> param = new HashMap<>();
|
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
|
@Override
|
||||||
protected void pluginInitialize() {
|
protected void pluginInitialize() {
|
||||||
@ -66,24 +59,20 @@ public class CaptureCordovaPlugin extends CordovaPlugin {
|
|||||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||||
this.callbackContext = callbackContext;
|
this.callbackContext = callbackContext;
|
||||||
if (action.equals("capture")) {
|
if (action.equals("capture")) {
|
||||||
if(!hasPermisssion()) {
|
int width = args.optInt(0, 360);
|
||||||
requestPermissions(PERMISSION_REQUEST_CODE);
|
int height = args.optInt(1, 640 );
|
||||||
} else {
|
int frameRate = args.optInt(2, 30 );
|
||||||
int width = args.optInt(0, 360);
|
int bitRate = args.optInt(3, width * height * 6 );
|
||||||
int height = args.optInt(1, 640 );
|
int limit = args.optInt(4, 3);
|
||||||
int frameRate = args.optInt(2, 30 );
|
int duration = args.optInt(5, 10);
|
||||||
int bitRate = args.optInt(3, width * height * 6 );
|
this.param.put("width",width);
|
||||||
int limit = args.optInt(4, 3);
|
this.param.put("height",height);
|
||||||
int duration = args.optInt(5, 10);
|
this.param.put("frameRate",frameRate);
|
||||||
this.param.put("width",width);
|
this.param.put("bitRate",bitRate);
|
||||||
this.param.put("height",height);
|
this.param.put("limit",limit);
|
||||||
this.param.put("frameRate",frameRate);
|
this.param.put("duration",duration);
|
||||||
this.param.put("bitRate",bitRate);
|
this.capture();
|
||||||
this.param.put("limit",limit);
|
return true;
|
||||||
this.param.put("duration",duration);
|
|
||||||
this.capture();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
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
|
* This plugin launches an external Activity when the camera is opened, so we
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package com.mabeijianxi.smallvideorecord2;
|
package com.mabeijianxi.smallvideorecord2;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
@ -24,6 +27,8 @@ import com.mabeijianxi.smallvideorecord2.model.MediaRecorderConfig;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
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.OnSeekCompleteListener,
|
||||||
MediaPlayer.OnVideoSizeChangedListener{
|
MediaPlayer.OnVideoSizeChangedListener{
|
||||||
|
|
||||||
|
protected static final int REQ_PERMISSION_CODE = 0x1000;
|
||||||
|
protected int mGrantedCount = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private int RECORD_TIME_MIN = (int) (1.5f * 1000);
|
private int RECORD_TIME_MIN = (int) (1.5f * 1000);
|
||||||
/**
|
/**
|
||||||
* 录制最长时间
|
* 录制最长时间
|
||||||
@ -109,8 +119,9 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 防止锁屏
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 防止锁屏
|
||||||
initData();
|
if (checkPermission()) {
|
||||||
loadViews();
|
onPermissionGranted();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initData() {
|
private void initData() {
|
||||||
@ -223,12 +234,13 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
if(checkPermission()){
|
||||||
if (mMediaRecorder == null) {
|
if (mMediaRecorder == null) {
|
||||||
initMediaRecorder();
|
initMediaRecorder();
|
||||||
} else {
|
} else {
|
||||||
mRecordLed.setChecked(false);
|
mRecordLed.setChecked(false);
|
||||||
mMediaRecorder.prepare();
|
mMediaRecorder.prepare();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -478,6 +490,59 @@ public class MediaRecorderActivity extends Activity implements
|
|||||||
if(mMediaRecorder != null){
|
if(mMediaRecorder != null){
|
||||||
mMediaRecorder.release();
|
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
|
@Override
|
||||||
|
@ -93,18 +93,22 @@
|
|||||||
android:layout_width="80dp"
|
android:layout_width="80dp"
|
||||||
android:layout_height="80dp"
|
android:layout_height="80dp"
|
||||||
android:layout_centerVertical="true"
|
android:layout_centerVertical="true"
|
||||||
android:layout_marginLeft="70dp"
|
android:layout_centerHorizontal="true"
|
||||||
android:visibility="invisible"
|
android:layout_alignRight="@+id/record_controller"
|
||||||
android:background="@drawable/record_redo" />
|
android:layout_marginRight="108dp"
|
||||||
|
android:background="@drawable/record_redo"
|
||||||
|
android:visibility="invisible" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/record_ok"
|
android:id="@+id/record_ok"
|
||||||
android:layout_width="80dp"
|
android:layout_width="80dp"
|
||||||
android:layout_height="80dp"
|
android:layout_height="80dp"
|
||||||
android:visibility="invisible"
|
|
||||||
android:layout_centerVertical="true"
|
android:layout_centerVertical="true"
|
||||||
android:layout_marginLeft="260dp"
|
android:layout_centerHorizontal="true"
|
||||||
android:background="@drawable/record_ok" />
|
android:layout_alignLeft="@+id/record_controller"
|
||||||
|
android:layout_marginLeft="108dp"
|
||||||
|
android:background="@drawable/record_ok"
|
||||||
|
android:visibility="invisible" />
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user