@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
18
18
*/
19
19
package org .apache .cordova .media ;
20
20
21
+ import android .media .AudioAttributes ;
21
22
import android .media .AudioManager ;
22
23
import android .media .MediaPlayer ;
23
24
import android .media .MediaPlayer .OnCompletionListener ;
@@ -39,6 +40,10 @@ Licensed to the Apache Software Foundation (ASF) under one
39
40
import java .io .IOException ;
40
41
import java .util .LinkedList ;
41
42
43
+ final class Options {
44
+ Integer usage = AudioAttributes .USAGE_UNKNOWN ;
45
+ }
46
+
42
47
/**
43
48
* This class implements the audio playback and recording capabilities used by Cordova.
44
49
* It is called by the AudioHandler Cordova class.
@@ -77,6 +82,8 @@ public enum STATE { MEDIA_NONE,
77
82
// private static int MEDIA_ERR_DECODE = 3;
78
83
// private static int MEDIA_ERR_NONE_SUPPORTED = 4;
79
84
85
+ private Options options = new Options ();
86
+
80
87
private AudioHandler handler ; // The AudioHandler object
81
88
private String id ; // The id of this player (used to identify Media object in JavaScript)
82
89
private MODE mode = MODE .NONE ; // Playback or Recording mode
@@ -106,6 +113,10 @@ public AudioPlayer(AudioHandler handler, String id, String file) {
106
113
this .tempFiles = new LinkedList <String >();
107
114
}
108
115
116
+ public void setOptions (Options options ) {
117
+ this .options = options ;
118
+ }
119
+
109
120
private String generateTempFile () {
110
121
String tempFileName = null ;
111
122
if (Environment .getExternalStorageState ().equals (Environment .MEDIA_MOUNTED )) {
@@ -609,6 +620,12 @@ private boolean readyPlayer(String file) {
609
620
case MEDIA_NONE :
610
621
if (this .player == null ) {
611
622
this .player = new MediaPlayer ();
623
+ if (this .options .usage != AudioAttributes .USAGE_UNKNOWN ) {
624
+ this .player .setAudioAttributes (
625
+ new AudioAttributes .Builder ()
626
+ .setUsage (options .usage )
627
+ .build ());
628
+ }
612
629
this .player .setOnErrorListener (this );
613
630
}
614
631
try {
@@ -632,6 +649,12 @@ private boolean readyPlayer(String file) {
632
649
//maybe it was recording?
633
650
if (player == null ) {
634
651
this .player = new MediaPlayer ();
652
+ if (this .options .usage != AudioAttributes .USAGE_UNKNOWN ) {
653
+ this .player .setAudioAttributes (
654
+ new AudioAttributes .Builder ()
655
+ .setUsage (options .usage )
656
+ .build ());
657
+ }
635
658
this .player .setOnErrorListener (this );
636
659
this .prepareOnly = false ;
637
660
0 commit comments