Skip to content

Commit 9b73eeb

Browse files
authored
1 parent e2a3c9a commit 9b73eeb

File tree

6 files changed

+29
-3
lines changed

6 files changed

+29
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
## Unreleased
7-
7+
- Add Player option mixWithOthers
88

99
## [2.0.2] - 2019-07-09
1010
### Added

android/src/main/java/com/reactnativecommunity/rctaudiotoolkit/AudioPlayerModule.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class AudioPlayerModule extends ReactContextBaseJavaModule implements Med
4646
private ReactApplicationContext context;
4747
private AudioManager mAudioManager;
4848
private Integer lastPlayerId;
49+
boolean mixWithOthers = false;
4950

5051
public AudioPlayerModule(ReactApplicationContext reactContext) {
5152
super(reactContext);
@@ -301,6 +302,13 @@ public void onPrepared(MediaPlayer player) {
301302
continueInBackground = options.getBoolean("continuesToPlayInBackground");
302303
}
303304

305+
// Don't mix audio with others by default
306+
this.mixWithOthers = false;
307+
308+
if (options.hasKey("mixWithOthers")) {
309+
this.mixWithOthers = options.getBoolean("mixWithOthers");
310+
}
311+
304312
this.playerAutoDestroy.put(playerId, autoDestroy);
305313
this.playerContinueInBackground.put(playerId, continueInBackground);
306314

@@ -382,7 +390,9 @@ public void play(Integer playerId, Callback callback) {
382390
}
383391

384392
try {
385-
this.mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
393+
if (!this.mixWithOthers) {
394+
this.mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
395+
}
386396
player.start();
387397

388398
callback.invoke(null, getInfo(player));

docs/API.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ Media methods
2828
// (Android only) Should playback continue if app is sent to background?
2929
// iOS will always pause in this case.
3030
continuesToPlayInBackground : boolean (default: False)
31+
32+
// Boolean to determine whether other audio sources on the device will mix
33+
// with sounds being played back by this module. If this is not set, playback
34+
// of audio will stop other sources
35+
mixWithOthers : boolean (default: False)
3136
}
3237
```
3338

ios/ReactNativeAudioToolkit/ReactNativeAudioToolkit/AudioPlayer.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ - (NSURL *)findUrlForPath:(NSString *)path {
123123
object:item];
124124

125125
// Set audio session
126+
NSNumber *mixWithOthers = [options objectForKey:@"mixWithOthers"];
126127
NSError *error = nil;
127-
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &error];
128+
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback withOptions: mixWithOthers ? AVAudioSessionCategoryOptionMixWithOthers : 0 error: &error];
128129
if (error) {
129130
NSDictionary* dict = [Helpers errObjWithCode:@"preparefail"
130131
withMessage:@"Failed to set audio session category."];

src/Player.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ let playerId = 0;
1717
const defaultPlayerOptions = {
1818
autoDestroy: true,
1919
continuesToPlayInBackground: false,
20+
mixWithOthers: false,
2021
};
2122

2223
/**
@@ -37,6 +38,8 @@ class Player extends EventEmitter {
3738
options.autoDestroy = defaultPlayerOptions.autoDestroy;
3839
if (options.continuesToPlayInBackground == null)
3940
options.continuesToPlayInBackground = defaultPlayerOptions.continuesToPlayInBackground;
41+
if (options.mixWithOthers == null)
42+
options.mixWithOthers = defaultPlayerOptions.mixWithOthers;
4043

4144
this._options = options;
4245
}

typings/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ interface PlayerOptions {
4444
* iOS will always pause in this case.
4545
* (Default: false)
4646
*/
47+
4748
continuesToPlayInBackground?: boolean;
49+
/**
50+
* Boolean to determine whether other audio sources on the device will mix
51+
* with sounds being played back by this module.
52+
* (Default: false)
53+
*/
54+
mixWithOthers?: boolean;
4855
}
4956

5057
/**

0 commit comments

Comments
 (0)