Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

主要修改录音权限问题 #35

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.6",
"version": "0.0.8",
"name": "cordova-plugin-audio-recorder-api",
"cordova_name": "AudioRecorderAPI",
"description": "This plugin is a Cordova audio recorder plugin which works as API.",
Expand All @@ -10,7 +10,7 @@
"engines": [
{
"name": "cordova",
"version": ">=3.0.0"
"version": ">=6.0.0"
}
],
"cordova": {
Expand Down
11 changes: 9 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-plugin-audio-recorder-api"
xmlns:android="http://schemas.android.com/apk/res/android"
version="0.0.1">
version="0.0.8">

<name>AudioRecorderAPI</name>

Expand All @@ -12,8 +12,10 @@

<license>MIT</license>

<dependency id="cordova-plugin-compat" version=">=1.0.0" />

<engines>
<engine name="cordova" version=">=3.0.0"/>
<engine name="cordova" version=">=6.0.0"/>
</engines>

<js-module src="www/AudioRecorderAPI.js" name="AudioRecorderAPI">
Expand All @@ -29,6 +31,11 @@
</config-file>
<header-file src="src/ios/AudioRecorderAPI.h"/>
<source-file src="src/ios/AudioRecorderAPI.m"/>

<preference name="MICROPHONE_USAGE_DESCRIPTION" default="需要您的麦克风发送语音消息或记录语音信息" />
<config-file target="*-Info.plist" parent="NSMicrophoneUsageDescription">
<string>$MICROPHONE_USAGE_DESCRIPTION</string>
</config-file>
</platform>

<!-- android -->
Expand Down
339 changes: 221 additions & 118 deletions src/android/com/emj365/plugins/AudioRecorderAPI.java
Original file line number Diff line number Diff line change
@@ -1,118 +1,221 @@
package com.emj365.plugins;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import android.media.AudioManager;
import android.os.CountDownTimer;
import android.os.Environment;
import android.content.Context;
import java.util.UUID;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;

public class AudioRecorderAPI extends CordovaPlugin {

private MediaRecorder myRecorder;
private String outputFile;
private CountDownTimer countDowntimer;

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
Context context = cordova.getActivity().getApplicationContext();
Integer seconds;
if (args.length() >= 1) {
seconds = args.getInt(0);
} else {
seconds = 7;
}
if (action.equals("record")) {
outputFile = context.getFilesDir().getAbsoluteFile() + "/"
+ UUID.randomUUID().toString() + ".m4a";
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
myRecorder.setAudioSamplingRate(44100);
myRecorder.setAudioChannels(1);
myRecorder.setAudioEncodingBitRate(32000);
myRecorder.setOutputFile(outputFile);

try {
myRecorder.prepare();
myRecorder.start();
} catch (final Exception e) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
callbackContext.error(e.getMessage());
}
});
return false;
}

countDowntimer = new CountDownTimer(seconds * 1000, 1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
stopRecord(callbackContext);
}
};
countDowntimer.start();
return true;
}

if (action.equals("stop")) {
countDowntimer.cancel();
stopRecord(callbackContext);
return true;
}

if (action.equals("playback")) {
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
FileInputStream fis = new FileInputStream(new File(outputFile));
mp.setDataSource(fis.getFD());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
callbackContext.success("playbackComplete");
}
});
mp.start();
return true;
}

return false;
}

private void stopRecord(final CallbackContext callbackContext) {
myRecorder.stop();
myRecorder.release();
cordova.getThreadPool().execute(new Runnable() {
public void run() {
callbackContext.success(outputFile);
}
});
}

}
package com.emj365.plugins;

import android.Manifest;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.CountDownTimer;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PermissionHelper;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

public class AudioRecorderAPI extends CordovaPlugin {

private MediaRecorder myRecorder;
private String outputFile;
private CountDownTimer countDowntimer;
private CallbackContext callbackContext;

private final int STATE_RECORDING = -1;
private final int STATE_NO_PERMISSION = -2;
private final int STATE_SUCCESS = 1;


/**
* 用于检测是否具有录音权限
*
* @return
*/
public int getRecordState() {
int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100));
short[] point = new short[minBuffer];
int readSize = 0;
try {

audioRecord.startRecording();//检测是否可以进入初始化状态
} catch (Exception e) {
if (audioRecord != null) {
audioRecord.release();
audioRecord = null;
//CLog.d("CheckAudioPermission","无法进入录音初始状态");
}
return STATE_NO_PERMISSION;
}
if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
//6.0以下机型都会返回此状态,故使用时需要判断bulid版本
//检测是否在录音中
if (audioRecord != null) {
audioRecord.stop();
audioRecord.release();
audioRecord = null;
//CLog.d("CheckAudioPermission","录音机被占用");
}
return STATE_RECORDING;
} else {
//检测是否可以获取录音结果

readSize = audioRecord.read(point, 0, point.length);


if (readSize <= 0) {
if (audioRecord != null) {
audioRecord.stop();
audioRecord.release();
audioRecord = null;

}
//CLog.d("CheckAudioPermission","录音的结果为空");
return STATE_NO_PERMISSION;

} else {
if (audioRecord != null) {
audioRecord.stop();
audioRecord.release();
audioRecord = null;
}
return STATE_SUCCESS;
}
}
}

public boolean requestPermission() {
try {
if (Build.VERSION.SDK_INT < 23) {

if (getRecordState() != STATE_SUCCESS) {
return false;
}
} else {
//检查是否有录音权限,如果没有申请
if (PermissionHelper.hasPermission(this, Manifest.permission.RECORD_AUDIO)) {

} else {
PermissionHelper.requestPermission(this, 0, Manifest.permission.RECORD_AUDIO);
}
}
} catch (final Exception e) {
return false;
}
return true;
}

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
Context context = cordova.getActivity().getApplicationContext();
this.callbackContext = callbackContext;

if (action.equals("requestPermission")) {
if (!requestPermission()) {
callbackContext.error("未获得授权使用麦克风,请在设置中打开");
} else {
return true;
}
}

if (action.equals("record")) {
if (!requestPermission()) {
callbackContext.error("未获得授权使用麦克风,请在设置中打开");
return false;
} else {
try {
outputFile = context.getFilesDir().getAbsoluteFile() + "/"
+ UUID.randomUUID().toString() + ".m4a";
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
myRecorder.setAudioSamplingRate(44100);
myRecorder.setAudioChannels(1);
myRecorder.setAudioEncodingBitRate(32000);
myRecorder.setOutputFile(outputFile);
myRecorder.prepare();
myRecorder.start();
} catch (final Exception e) {
callbackContext.error("未获得授权使用麦克风,请在设置中打开");
return false;
}
callbackContext.success("recordSuccess");
}

return false;
}

if (action.equals("stop")) {
stopRecord(callbackContext);
return true;
}

if (action.equals("playback")) {
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
FileInputStream fis = new FileInputStream(new File(outputFile));
mp.setDataSource(fis.getFD());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (callbackContext != null) {
callbackContext.success("playbackComplete");
}
}
});
mp.start();
return true;
}

return false;
}

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
super.onRequestPermissionResult(requestCode, permissions, grantResults);
if (callbackContext != null) {
callbackContext.error("未获得授权使用麦克风,请在设置中打开");
}
}

private void stopRecord(final CallbackContext callbackContext) {

try {
myRecorder.stop();
myRecorder.release();
cordova.getThreadPool().execute(new Runnable() {
public void run() {
callbackContext.success(outputFile);
}
});
} catch (Exception e) {

}

}

}
Loading