File tree Expand file tree Collapse file tree 6 files changed +44
-3
lines changed
ios/ReactNativeAudioToolkit/ReactNativeAudioToolkit Expand file tree Collapse file tree 6 files changed +44
-3
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
4
4
The format is based on [ Keep a Changelog] ( https://keepachangelog.com/en/1.0.0/ ) .
5
5
6
6
## Unreleased
7
+ - Add ability to override iOS audio session category
7
8
- Add Player option mixWithOthers
8
9
9
10
## [ 2.0.2] - 2019-07-09
Original file line number Diff line number Diff line change @@ -29,6 +29,10 @@ Media methods
29
29
// iOS will always pause in this case.
30
30
continuesToPlayInBackground : boolean (default: False)
31
31
32
+ // (iOS only) Define the audio session category
33
+ // Options: Playback, Ambient and SoloAmbient
34
+ category : PlaybackCategory (default: PlaybackCategory .Playback )
35
+
32
36
// Boolean to determine whether other audio sources on the device will mix
33
37
// with sounds being played back by this module. If this is not set, playback
34
38
// of audio will stop other sources
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- import Player from './src/Player' ;
3
+ import Player , { PlaybackCategories } from './src/Player' ;
4
4
import Recorder from './src/Recorder' ;
5
5
import MediaStates from './src/MediaStates' ;
6
6
7
- export { Player , Recorder , MediaStates } ;
7
+ export { Player , Recorder , MediaStates , PlaybackCategories } ;
Original file line number Diff line number Diff line change @@ -123,9 +123,23 @@ - (NSURL *)findUrlForPath:(NSString *)path {
123
123
object: item];
124
124
125
125
// 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
+ }
126
140
NSNumber *mixWithOthers = [options objectForKey: @" mixWithOthers" ];
127
141
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];
129
143
if (error) {
130
144
NSDictionary * dict = [Helpers errObjWithCode: @" preparefail"
131
145
withMessage: @" Failed to set audio session category." ];
Original file line number Diff line number Diff line change @@ -14,9 +14,16 @@ const RCTAudioPlayer = NativeModules.AudioPlayer;
14
14
15
15
let playerId = 0 ;
16
16
17
+ export const PlaybackCategories = {
18
+ Playback : 1 ,
19
+ Ambient : 2 ,
20
+ SoloAmbient : 3 ,
21
+ }
22
+
17
23
const defaultPlayerOptions = {
18
24
autoDestroy : true ,
19
25
continuesToPlayInBackground : false ,
26
+ category : PlaybackCategories . Playback ,
20
27
mixWithOthers : false ,
21
28
} ;
22
29
@@ -38,6 +45,8 @@ class Player extends EventEmitter {
38
45
options . autoDestroy = defaultPlayerOptions . autoDestroy ;
39
46
if ( options . continuesToPlayInBackground == null )
40
47
options . continuesToPlayInBackground = defaultPlayerOptions . continuesToPlayInBackground ;
48
+ if ( options . category == null )
49
+ options . category = defaultPlayerOptions . category ;
41
50
if ( options . mixWithOthers == null )
42
51
options . mixWithOthers = defaultPlayerOptions . mixWithOthers ;
43
52
Original file line number Diff line number Diff line change @@ -14,6 +14,12 @@ declare enum MediaStates {
14
14
PAUSED = 5
15
15
}
16
16
17
+ declare enum PlaybackCategories {
18
+ Playback = 1 ,
19
+ Ambient = 2 ,
20
+ SoloAmbient = 3 ,
21
+ }
22
+
17
23
interface BaseError < T > {
18
24
err : "invalidpath" | "preparefail" | "startfail" | "notfound" | "stopfail" | T ;
19
25
message : string ;
@@ -46,6 +52,13 @@ interface PlayerOptions {
46
52
*/
47
53
48
54
continuesToPlayInBackground ?: boolean ;
55
+
56
+ /**
57
+ * (iOS only) Define the audio session category
58
+ * (Default: Playback)
59
+ */
60
+ category ?: PlaybackCategories ;
61
+
49
62
/**
50
63
* Boolean to determine whether other audio sources on the device will mix
51
64
* with sounds being played back by this module.
You can’t perform that action at this time.
0 commit comments