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

feat: Implement playing audio in background #231

Open
wants to merge 2 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
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ The following constants are reported as the only parameter to the

- `media.play`: Start or resume playing an audio file.

- `media.playInBackground`: Start or resume playing an audio file in background.

- `media.pause`: Pause playback of an audio file.

- `media.pauseRecord`: Pause recording of an audio file.
Expand Down Expand Up @@ -364,6 +366,41 @@ function playAudio(url) {
var myMedia = new Media("audio/beer.mp3")
myMedia.play() // first looks for file in www/audio/beer.mp3 then in <application>/documents/tmp/audio/beer.mp3

## media.playInBackground

Starts or resumes playing an audio file in background.

```js
media.playInBackground();
```

### Supported Platforms

- Android
- iOS

### Quick Example

```js
// Play audio in background
//
function playAudioInBackground(url) {
// Play the audio file at url
var my_media = new Media(url,
// success callback
function () {
console.log("playAudioInBackground():Audio Success");
},
// error callback
function (err) {
console.log("playAudioInBackground():Audio Error: " + err);
}
);
// Play audio
my_media.playInBackground();
}
```

## media.release

Releases the underlying operating system's audio resources.
Expand Down Expand Up @@ -556,7 +593,7 @@ function recordAudio() {

This plugins requires the following usage description:

* `NSMicrophoneUsageDescription` describes the reason that the app accesses the user's microphone.
* `NSMicrophoneUsageDescription` describes the reason that the app accesses the user's microphone.

To add this entry into the `info.plist`, you can use the `edit-config` tag in the `config.xml` like this:

Expand Down
19 changes: 19 additions & 0 deletions src/android/AudioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ else if (action.equals("startPlayingAudio")) {
}
this.startPlayingAudio(args.getString(0), FileHelper.stripFileProtocol(fileUriStr));
}
else if (action.equals("startPlayingAudioInBackground")) {
String target = args.getString(1);
String fileUriStr;
try {
Uri targetUri = resourceApi.remapUri(Uri.parse(target));
fileUriStr = targetUri.toString();
} catch (IllegalArgumentException e) {
fileUriStr = target;
}
this.startPlayingAudioInBackground(args.getString(0), FileHelper.stripFileProtocol(fileUriStr));
}
else if (action.equals("seekToAudio")) {
this.seekToAudio(args.getString(0), args.getInt(1));
}
Expand Down Expand Up @@ -321,6 +332,14 @@ public void startPlayingAudio(String id, String file) {
getAudioFocus();
}

public void startPlayingAudioInBackground(final String id, final String file) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
startPlayingAudio(id, file);
}
});
}

/**
* Seek to a location.
* @param id The id of the audio player
Expand Down
1 change: 1 addition & 0 deletions src/ios/CDVSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ typedef NSUInteger CDVMediaMsg;
@property (nonatomic, strong) NSString* statusCallbackId;

- (void)startPlayingAudio:(CDVInvokedUrlCommand*)command;
- (void)startPlayingAudioInBackground:(CDVInvokedUrlCommand*)command;
- (void)pausePlayingAudio:(CDVInvokedUrlCommand*)command;
- (void)stopPlayingAudio:(CDVInvokedUrlCommand*)command;
- (void)seekToAudio:(CDVInvokedUrlCommand*)command;
Expand Down
11 changes: 8 additions & 3 deletions src/ios/CDVSound.m
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ - (void)setRate:(CDVInvokedUrlCommand*)command
// don't care for any callbacks
}

- (void)startPlayingAudioInBackground:(CDVInvokedUrlCommand*)command
{
[self startPlayingAudio:command];
}

- (void)startPlayingAudio:(CDVInvokedUrlCommand*)command
{
[self.commandDelegate runInBackground:^{
Expand Down Expand Up @@ -839,7 +844,7 @@ - (void)onMemoryWarning
{
/* https://issues.apache.org/jira/browse/CB-11513 */
NSMutableArray* keysToRemove = [[NSMutableArray alloc] init];

for(id key in [self soundCache]) {
CDVAudioFile* audioFile = [[self soundCache] objectForKey:key];
if (audioFile != nil) {
Expand All @@ -851,9 +856,9 @@ - (void)onMemoryWarning
}
}
}

[[self soundCache] removeObjectsForKeys:keysToRemove];

// [[self soundCache] removeAllObjects];
// [self setSoundCache:nil];
[self setAvSession:nil];
Expand Down
7 changes: 7 additions & 0 deletions www/Media.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ Media.prototype.play = function(options) {
exec(null, null, "Media", "startPlayingAudio", [this.id, this.src, options]);
};

/**
* Start or resume playing audio file in a background thread.
*/
Media.prototype.playInBackground = function(options) {
exec(null, null, "Media", "startPlayingAudioInBackground", [this.id, this.src, options]);
};

/**
* Stop playing audio file.
*/
Expand Down