Skip to content

Commit 8cbc9c3

Browse files
committed
(android) added ability set usage type
1 parent eb99c85 commit 8cbc9c3

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cordova plugin add cordova-plugin-media
6262
## Media
6363

6464
```js
65-
var media = new Media(src, mediaSuccess, [mediaError], [mediaStatus]);
65+
var media = new Media(src, mediaSuccess, [mediaError], [mediaStatus], [options]);
6666
```
6767

6868
### Parameters
@@ -75,13 +75,21 @@ var media = new Media(src, mediaSuccess, [mediaError], [mediaStatus]);
7575

7676
- __mediaStatus__: (Optional) The callback that executes to indicate status changes. It takes a integer status code. _(Function)_
7777

78+
- __options__: (Optional) The object to pass additional options to player instance. (Android) Supports only `usage` property atm. _(Object)_
79+
7880
__NOTE__: `cdvfile` path is supported as `src` parameter:
7981
```javascript
8082
var my_media = new Media('cdvfile://localhost/temporary/recording.mp3', ...);
8183
```
8284

8385
### Constants
8486

87+
(Android) Usage types:
88+
89+
- `Media.ANDROID_USAGE_UNKNOWN` = 0;
90+
- `Media.ANDROID_USAGE_MEDIA` = 1;
91+
- `Media.ANDROID_USAGE_VOICE_COMMUNICATION` = 2;
92+
8593
The following constants are reported as the only parameter to the
8694
`mediaStatus` callback:
8795

src/android/AudioHandler.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,14 @@ else if (action.equals("getDurationAudio")) {
164164
else if (action.equals("create")) {
165165
String id = args.getString(0);
166166
String src = FileHelper.stripFileProtocol(args.getString(1));
167-
getOrCreatePlayer(id, src);
167+
AudioPlayer player = getOrCreatePlayer(id, src);
168+
169+
JSONObject jsonOptions = args.optJSONObject(2);
170+
if(jsonOptions != null) {
171+
Options options = new Options();
172+
options.usage = jsonOptions.optInt("usage", options.usage);
173+
player.setOptions(options);
174+
}
168175
}
169176
else if (action.equals("release")) {
170177
boolean b = this.release(args.getString(0));

src/android/AudioPlayer.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
1818
*/
1919
package org.apache.cordova.media;
2020

21+
import android.media.AudioAttributes;
2122
import android.media.AudioManager;
2223
import android.media.MediaPlayer;
2324
import android.media.MediaPlayer.OnCompletionListener;
@@ -39,6 +40,10 @@ Licensed to the Apache Software Foundation (ASF) under one
3940
import java.io.IOException;
4041
import 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

www/Media.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,21 @@ var mediaObjects = {};
3838
* errorCallback(int errorCode) - OPTIONAL
3939
* @param statusCallback The callback to be called when media status has changed.
4040
* statusCallback(int statusCode) - OPTIONAL
41+
* @param options The options object.
42+
* (android) { usage } - OPTIONAL
4143
*/
42-
var Media = function (src, successCallback, errorCallback, statusCallback) {
43-
argscheck.checkArgs('sFFF', 'Media', arguments);
44+
var Media = function (src, successCallback, errorCallback, statusCallback, options) {
45+
argscheck.checkArgs('sFFFO', 'Media', arguments);
4446
this.id = utils.createUUID();
4547
mediaObjects[this.id] = this;
4648
this.src = src;
4749
this.successCallback = successCallback;
4850
this.errorCallback = errorCallback;
4951
this.statusCallback = statusCallback;
52+
this.options = options;
5053
this._duration = -1;
5154
this._position = -1;
52-
exec(null, this.errorCallback, 'Media', 'create', [this.id, this.src]);
55+
exec(null, this.errorCallback, 'Media', 'create', [this.id, this.src, this.options]);
5356
};
5457

5558
// Media messages
@@ -66,6 +69,11 @@ Media.MEDIA_PAUSED = 3;
6669
Media.MEDIA_STOPPED = 4;
6770
Media.MEDIA_MSG = ['None', 'Starting', 'Running', 'Paused', 'Stopped'];
6871

72+
// Usage types
73+
Media.ANDROID_USAGE_UNKNOWN = 0;
74+
Media.ANDROID_USAGE_MEDIA = 1;
75+
Media.ANDROID_USAGE_VOICE_COMMUNICATION = 2;
76+
6977
// "static" function to return existing objs.
7078
Media.get = function (id) {
7179
return mediaObjects[id];

0 commit comments

Comments
 (0)