@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
1818*/
1919package org .apache .cordova .media ;
2020
21+ import android .media .AudioAttributes ;
2122import android .media .AudioManager ;
2223import android .media .MediaPlayer ;
2324import android .media .MediaPlayer .OnCompletionListener ;
@@ -39,6 +40,10 @@ Licensed to the Apache Software Foundation (ASF) under one
3940import java .io .IOException ;
4041import java .util .LinkedList ;
4142
43+ final class Options {
44+ Integer usage = AudioAttributes .USAGE_UNKNOWN ;
45+ }
46+
4247/**
4348 * This class implements the audio playback and recording capabilities used by Cordova.
4449 * It is called by the AudioHandler Cordova class.
@@ -77,6 +82,8 @@ public enum STATE { MEDIA_NONE,
7782// private static int MEDIA_ERR_DECODE = 3;
7883// private static int MEDIA_ERR_NONE_SUPPORTED = 4;
7984
85+ private Options options = new Options ();
86+
8087 private AudioHandler handler ; // The AudioHandler object
8188 private String id ; // The id of this player (used to identify Media object in JavaScript)
8289 private MODE mode = MODE .NONE ; // Playback or Recording mode
@@ -106,6 +113,10 @@ public AudioPlayer(AudioHandler handler, String id, String file) {
106113 this .tempFiles = new LinkedList <String >();
107114 }
108115
116+ public void setOptions (Options options ) {
117+ this .options = options ;
118+ }
119+
109120 private String generateTempFile () {
110121 String tempFileName = null ;
111122 if (Environment .getExternalStorageState ().equals (Environment .MEDIA_MOUNTED )) {
@@ -609,6 +620,12 @@ private boolean readyPlayer(String file) {
609620 case MEDIA_NONE :
610621 if (this .player == null ) {
611622 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+ }
612629 this .player .setOnErrorListener (this );
613630 }
614631 try {
@@ -632,6 +649,12 @@ private boolean readyPlayer(String file) {
632649 //maybe it was recording?
633650 if (player == null ) {
634651 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+ }
635658 this .player .setOnErrorListener (this );
636659 this .prepareOnly = false ;
637660
0 commit comments