Skip to content

Commit 220b7fe

Browse files
authored
iOS Audio Playback Category (react-native-audio-toolkit#188)
1 parent 9b73eeb commit 220b7fe

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +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+
- Add ability to override iOS audio session category
78
- Add Player option mixWithOthers
89

910
## [2.0.2] - 2019-07-09

docs/API.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Media methods
2929
// iOS will always pause in this case.
3030
continuesToPlayInBackground : boolean (default: False)
3131

32+
// (iOS only) Define the audio session category
33+
// Options: Playback, Ambient and SoloAmbient
34+
category : PlaybackCategory (default: PlaybackCategory.Playback)
35+
3236
// Boolean to determine whether other audio sources on the device will mix
3337
// with sounds being played back by this module. If this is not set, playback
3438
// of audio will stop other sources

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
import Player from './src/Player';
3+
import Player, { PlaybackCategories } from './src/Player';
44
import Recorder from './src/Recorder';
55
import MediaStates from './src/MediaStates';
66

7-
export { Player, Recorder, MediaStates };
7+
export { Player, Recorder, MediaStates, PlaybackCategories };

ios/ReactNativeAudioToolkit/ReactNativeAudioToolkit/AudioPlayer.m

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

125125
// Set audio session
126+
NSNumber *category = [options objectForKey:@"category"];
127+
NSString *avAudioSessionCategory;
128+
switch ([category intValue]) {
129+
case 1:
130+
default:
131+
avAudioSessionCategory = AVAudioSessionCategoryPlayback;
132+
break;
133+
case 2:
134+
avAudioSessionCategory = AVAudioSessionCategoryAmbient;
135+
break;
136+
case 3:
137+
avAudioSessionCategory = AVAudioSessionCategorySoloAmbient;
138+
break;
139+
}
126140
NSNumber *mixWithOthers = [options objectForKey:@"mixWithOthers"];
127141
NSError *error = nil;
128-
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback withOptions: mixWithOthers ? AVAudioSessionCategoryOptionMixWithOthers : 0 error: &error];
142+
[[AVAudioSession sharedInstance] setCategory: avAudioSessionCategory withOptions: mixWithOthers ? AVAudioSessionCategoryOptionMixWithOthers : 0 error: &error];
129143
if (error) {
130144
NSDictionary* dict = [Helpers errObjWithCode:@"preparefail"
131145
withMessage:@"Failed to set audio session category."];

src/Player.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ const RCTAudioPlayer = NativeModules.AudioPlayer;
1414

1515
let playerId = 0;
1616

17+
export const PlaybackCategories = {
18+
Playback: 1,
19+
Ambient: 2,
20+
SoloAmbient: 3,
21+
}
22+
1723
const defaultPlayerOptions = {
1824
autoDestroy: true,
1925
continuesToPlayInBackground: false,
26+
category: PlaybackCategories.Playback,
2027
mixWithOthers: false,
2128
};
2229

@@ -38,6 +45,8 @@ class Player extends EventEmitter {
3845
options.autoDestroy = defaultPlayerOptions.autoDestroy;
3946
if (options.continuesToPlayInBackground == null)
4047
options.continuesToPlayInBackground = defaultPlayerOptions.continuesToPlayInBackground;
48+
if (options.category == null)
49+
options.category = defaultPlayerOptions.category;
4150
if (options.mixWithOthers == null)
4251
options.mixWithOthers = defaultPlayerOptions.mixWithOthers;
4352

typings/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ declare enum MediaStates {
1414
PAUSED = 5
1515
}
1616

17+
declare enum PlaybackCategories {
18+
Playback = 1,
19+
Ambient = 2,
20+
SoloAmbient = 3,
21+
}
22+
1723
interface BaseError<T> {
1824
err: "invalidpath" | "preparefail" | "startfail" | "notfound" | "stopfail" | T;
1925
message: string;
@@ -46,6 +52,13 @@ interface PlayerOptions {
4652
*/
4753

4854
continuesToPlayInBackground?: boolean;
55+
56+
/**
57+
* (iOS only) Define the audio session category
58+
* (Default: Playback)
59+
*/
60+
category?: PlaybackCategories;
61+
4962
/**
5063
* Boolean to determine whether other audio sources on the device will mix
5164
* with sounds being played back by this module.

0 commit comments

Comments
 (0)