Skip to content

Commit 0ed2544

Browse files
authored
Merge pull request #36 from Calsign/android-audio-input
Improve Android audio input permission not granted error message
2 parents 45d1ce3 + 9b09f2a commit 0ed2544

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package processing.sound;
2+
3+
/**
4+
* AndroidPermissionException is thrown when trying to create an AudioIn on
5+
* Android when you have not granted the required RECORD_AUDIO permission.
6+
*/
7+
public class AndroidPermissionException extends RuntimeException {
8+
public AndroidPermissionException(String message) {
9+
super(message);
10+
}
11+
}

src/processing/sound/AudioIn.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public class AudioIn extends SoundObject {
2020
// port, so we need to control the amplitude via an extra multiplier unit
2121
private Multiply multiplier;
2222

23+
private String ANDROID_PERMISSION_WARNING_MESSAGE =
24+
"\nsketch does not have permission to record audio from microphone,\n" +
25+
"please request the permission in your AndroidManifest.xml and in\n" +
26+
"your sketch initialization code (the Sound library's\n" +
27+
"AudioInputAndroid example demonstrates how to do both)\n";
28+
2329
public AudioIn(PApplet parent) {
2430
this(parent, 0);
2531
}
@@ -32,17 +38,15 @@ public AudioIn(PApplet parent) {
3238
*/
3339
public AudioIn(PApplet parent, int in) {
3440
super(parent);
41+
3542
if (Engine.getAudioManager() instanceof JSynAndroidAudioDeviceManager) {
3643
// we're on Android, check if the sketch has permission to capture Audio
3744
if (!parent.hasPermission(Manifest.permission.RECORD_AUDIO)) {
38-
// if (ContextCompat.checkSelfPermission(parent.getContext(), Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED) {
39-
Engine.printWarning("sketch does not have permission to record audio from microphone, please request the permission in your AndroidManifest.xml or in your sketch initialization code (the Sound library's AudioInputAndroid example demonstrates how to do both)");
40-
// requesting permission in here is problematic because the
41-
// user dialogue and granting of permission are asynchronous
42-
// ActivityCompat.requestPermissions(parent.getContext(), new String[]{ Manifest.permission.RECORD_AUDIO }, -1);
43-
// parent.requestPermission("android.permission.RECORD_AUDIO", "notObviousHowThisApproachCouldBeUsed");
45+
Engine.printWarning(ANDROID_PERMISSION_WARNING_MESSAGE);
46+
throw new AndroidPermissionException("RECORD_AUDIO permission not granted");
4447
}
4548
}
49+
4650
this.input = new ChannelIn(in);
4751
this.multiplier = new Multiply();
4852
this.multiplier.inputA.connect(this.input.output);

src/processing/sound/JSynAndroidAudioDeviceManager.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ public void write(double[] buffer, int start, int count) {
119119
}
120120

121121
public void stop() {
122-
this.audioTrack.stop();
123-
this.audioTrack.release();
122+
if (this.audioTrack != null) {
123+
this.audioTrack.stop();
124+
this.audioTrack.release();
125+
}
124126
}
125127

126128
public void close() {
@@ -189,8 +191,10 @@ public int read(double[] buffer, int start, int count) {
189191
}
190192

191193
public void stop() {
192-
this.audioRecord.stop();
193-
this.audioRecord.release();
194+
if (this.audioRecord != null) {
195+
this.audioRecord.stop();
196+
this.audioRecord.release();
197+
}
194198
}
195199

196200
public int available() {

0 commit comments

Comments
 (0)