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

Add Android 6.0 permissions #33

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
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ This plugin is a Cordova audio recorder plugin that works as API.

Different than http://plugins.cordova.io/#/package/org.apache.cordova.media-capture this plugin does not request the native recorder app (system default recorder) and active recording manually.

**This is a fork of https://github.com/emj365/cordova-plugin-audio-recorder-api. This fork adds support for Android 6.0+ permissions and increases the quality of the iOS recording.

Without the Android 6.0+ permission fix, an error of "setAudioSource failed" occurs when recording is attempted.**

Supports platforms:
--------------------

- iOS
- Android

Install:
---------

```bash
$ cordova plugin add cordova-plugin-audio-recorder-api
```

How to use:
------------

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why delete?

Expand Down Expand Up @@ -55,9 +52,17 @@ recorder.playback = function() {
alert('ko: ' + msg);
});
}
window.plugins.audioRecorderAPI.dopermissions(function (msg) {
// success
console.log('Audio permissions ok: ' + msg);
}, function (msg) {
// failed
console.error("audioRecorderAPI: permission not granted");
alert("The app needs access to your microphone to function.");
});
```

Where are files save?
Where are files saved?
---------------------

iOS: `/var/mobile/Applications/<UUID>/Library/NoCloud/<file-id>.m4a`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.6",
"version": "0.0.7",
"name": "cordova-plugin-audio-recorder-api",
"cordova_name": "AudioRecorderAPI",
"description": "This plugin is a Cordova audio recorder plugin which works as API.",
Expand Down
2 changes: 1 addition & 1 deletion 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.7">

<name>AudioRecorderAPI</name>

Expand Down
45 changes: 42 additions & 3 deletions src/android/com/emj365/plugins/AudioRecorderAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,53 @@
import android.os.CountDownTimer;
import android.os.Environment;
import android.content.Context;
import android.Manifest;
import android.content.pm.PackageManager;
import java.util.UUID;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;

public class AudioRecorderAPI extends CordovaPlugin {

public class AudioRecorderAPI extends CordovaPlugin {
private MediaRecorder myRecorder;
private String outputFile;
private CountDownTimer countDowntimer;

private CallbackContext myCallbackContext;

public static final int P_REC_AUDIO = 0;
public static final int P_MOD_AUDIO = 1;
public static final int P_READ_PHONE = 2;

public String [] permissionArray = {
Manifest.permission.RECORD_AUDIO,
Manifest.permission.MODIFY_AUDIO_SETTINGS,
Manifest.permission.READ_PHONE_STATE};

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
Context context = cordova.getActivity().getApplicationContext();
Integer seconds;
myCallbackContext=callbackContext;
if (args.length() >= 1) {
seconds = args.getInt(0);
} else {
seconds = 7;
}

if (action.equals("doPermissions"))
{
for( int i = 0; i < permissionArray.length - 1; i++)
{
if (!cordova.hasPermission(permissionArray[i]))
{
cordova.requestPermission(this, i, permissionArray[i]);
}
}

return true;
}

if (action.equals("record")) {
outputFile = context.getFilesDir().getAbsoluteFile() + "/"
+ UUID.randomUUID().toString() + ".m4a";
Expand Down Expand Up @@ -102,7 +129,7 @@ public void onCompletion(MediaPlayer mp) {
return true;
}

return false;
return false;
}

private void stopRecord(final CallbackContext callbackContext) {
Expand All @@ -114,5 +141,17 @@ public void run() {
}
});
}

public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
for(int r:grantResults)
{
if(r == PackageManager.PERMISSION_DENIED)
{
myCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "PERMS_REFUSED"));
return;
}
}
myCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "PERMS_GRANTED"));
}

}
1 change: 1 addition & 0 deletions src/ios/AudioRecorderAPI.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <Cordova/CDV.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioServices.h>

@interface AudioRecorderAPI : CDVPlugin {
NSString *recorderFilePath;
Expand Down
24 changes: 16 additions & 8 deletions src/ios/AudioRecorderAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,31 @@ - (void)record:(CDVInvokedUrlCommand*)command {
NSError *err;
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
if (err)
{
NSLog(@"%@ %d %@", [err domain], [err code], [[err userInfo] description]);
{
NSLog(@"AVAudioSession error AVAudioSessionCategoryPlayAndRecord:%@",err);
}
err = nil;

err = nil;
[audioSession setActive:YES error:&err];
if (err)
{
NSLog(@"%@ %d %@", [err domain], [err code], [[err userInfo] description]);
{
NSLog(@"AVAudioSession error setActive:%@",err);
}

err = nil;
[audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&err];
if (err)
{
NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",err);
}

NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
[recordSettings setObject:[NSNumber numberWithInt: kAudioFormatMPEG4AAC] forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:8000.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithFloat:22050.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:12000] forKey:AVEncoderBitRateKey];
[recordSettings setObject:[NSNumber numberWithInt:56000] forKey:AVEncoderBitRateKey];
[recordSettings setObject:[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityLow] forKey: AVEncoderAudioQualityKey];
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];

// Create a new dated file
NSString *uuid = [[NSUUID UUID] UUIDString];
Expand Down
4 changes: 4 additions & 0 deletions www/AudioRecorderAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ AudioRecorderAPI.prototype.playback = function (successCallback, errorCallback)
cordova.exec(successCallback, errorCallback, "AudioRecorderAPI", "playback", []);
};

AudioRecorderAPI.prototype.dopermissions = function (successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, "AudioRecorderAPI", "doPermissions", []);
};

AudioRecorderAPI.install = function () {
if (!window.plugins) {
window.plugins = {};
Expand Down