@@ -82,67 +82,69 @@ export class TNSPlayer extends Observable {
82
82
return this . playFromFile ( options ) ;
83
83
}
84
84
85
- public playFromFile ( options : AudioPlayerOptions ) : Promise < any > {
86
- return new Promise ( ( resolve , reject ) => {
87
- // only if not explicitly set, default to true
88
- if ( options . autoPlay !== false ) {
89
- options . autoPlay = true ;
85
+ private prepareAudioSession ( options : AudioPlayerOptions ) {
86
+ this . completeCallback = options . completeCallback ;
87
+ this . errorCallback = options . errorCallback ;
88
+ this . infoCallback = options . infoCallback ;
89
+
90
+ const audioSession = AVAudioSession . sharedInstance ( ) ;
91
+ audioSession . setCategoryModeRouteSharingPolicyOptionsError (
92
+ options . sessionCategory !== undefined ? options . sessionCategory : AVAudioSessionCategoryAmbient ,
93
+ options . sessionMode !== undefined ? options . sessionMode : AVAudioSessionModeDefault ,
94
+ options . sessionRouteSharingPolicy !== undefined ? options . sessionRouteSharingPolicy : AVAudioSessionRouteSharingPolicy . Default ,
95
+ options . audioMixing ? AVAudioSessionCategoryOptions . MixWithOthers : AVAudioSessionCategoryOptions . DuckOthers ,
96
+ //@ts -ignore
97
+ null
98
+ ) ;
99
+ const output = audioSession . currentRoute . outputs . lastObject . portType ;
100
+ if ( output . match ( / R e c e i v e r / ) ) {
101
+ try {
102
+ audioSession . setCategoryError ( AVAudioSessionCategoryPlayAndRecord ) ;
103
+ audioSession . overrideOutputAudioPortError ( AVAudioSessionPortOverride . Speaker ) ;
104
+ audioSession . setActiveError ( true ) ;
105
+ } catch ( err ) {
106
+ console . error ( 'setting audioSession catergory failed' , err ) ;
90
107
}
108
+ }
109
+ }
110
+ private handleStartPlayer ( options : AudioPlayerOptions ) {
111
+ if ( this . delegate === undefined ) {
112
+ this . delegate = TNSPlayerDelegate . initWithOwner ( this ) ;
113
+ }
114
+ this . _player . delegate = this . delegate ;
115
+ // enableRate to change playback speed
116
+ this . _player . enableRate = true ;
117
+
118
+ if ( options . metering ) {
119
+ this . _player . meteringEnabled = true ;
120
+ }
121
+
122
+ if ( options . loop ) {
123
+ this . _player . numberOfLoops = - 1 ;
124
+ }
91
125
126
+ if ( options . autoPlay !== false ) {
127
+ this . _player . play ( ) ;
128
+ }
129
+ }
130
+
131
+ public playFromFile ( options : AudioPlayerOptions ) : Promise < any > {
132
+ return new Promise ( ( resolve , reject ) => {
92
133
try {
93
134
let fileName = Utils . isString ( options . audioFile ) ? options . audioFile . trim ( ) : '' ;
94
135
if ( fileName . indexOf ( '~/' ) === 0 ) {
95
136
fileName = nsFilePath . join ( knownFolders . currentApp ( ) . path , fileName . replace ( '~/' , '' ) ) ;
96
137
}
97
138
98
- this . completeCallback = options . completeCallback ;
99
- this . errorCallback = options . errorCallback ;
100
- this . infoCallback = options . infoCallback ;
101
-
102
- const audioSession = AVAudioSession . sharedInstance ( ) ;
103
- audioSession . setCategoryModeRouteSharingPolicyOptionsError (
104
- options . sessionCategory || AVAudioSessionCategoryAmbient ,
105
- options . sessionMode || AVAudioSessionModeDefault ,
106
- options . sessionRouteSharingPolicy || AVAudioSessionRouteSharingPolicy . LongForm ,
107
- options . audioMixing ? AVAudioSessionCategoryOptions . MixWithOthers : AVAudioSessionCategoryOptions . DuckOthers ,
108
- //@ts -ignore
109
- null
110
- ) ;
111
- const output = audioSession . currentRoute . outputs . lastObject . portType ;
112
- if ( output . match ( / R e c e i v e r / ) ) {
113
- try {
114
- audioSession . setCategoryError ( AVAudioSessionCategoryPlayAndRecord ) ;
115
- audioSession . overrideOutputAudioPortError ( AVAudioSessionPortOverride . Speaker ) ;
116
- audioSession . setActiveError ( true ) ;
117
- } catch ( err ) {
118
- console . error ( 'setting audioSession catergory failed' , err ) ;
119
- }
120
- }
139
+ this . prepareAudioSession ( options ) ;
121
140
122
141
const errorRef = new interop . Reference ( ) ;
123
142
this . _player = AVAudioPlayer . alloc ( ) . initWithContentsOfURLError ( NSURL . fileURLWithPath ( fileName ) , errorRef ) ;
124
143
if ( errorRef && errorRef . value ) {
125
144
reject ( errorRef . value ) ;
126
145
return ;
127
146
} else if ( this . _player ) {
128
- if ( this . delegate === undefined ) {
129
- this . delegate = TNSPlayerDelegate . initWithOwner ( this ) ;
130
- }
131
- this . _player . delegate = this . delegate ;
132
- // enableRate to change playback speed
133
- this . _player . enableRate = true ;
134
-
135
- if ( options . metering ) {
136
- this . _player . meteringEnabled = true ;
137
- }
138
-
139
- if ( options . loop ) {
140
- this . _player . numberOfLoops = - 1 ;
141
- }
142
-
143
- if ( options . autoPlay ) {
144
- this . _player . play ( ) ;
145
- }
147
+ this . handleStartPlayer ( options ) ;
146
148
147
149
resolve ( null ) ;
148
150
} else {
@@ -165,66 +167,23 @@ export class TNSPlayer extends Observable {
165
167
166
168
public playFromUrl ( options : AudioPlayerOptions ) : Promise < any > {
167
169
return new Promise ( ( resolve , reject ) => {
168
- // only if not explicitly set, default to true
169
- if ( options . autoPlay !== false ) {
170
- options . autoPlay = true ;
171
- }
172
-
173
170
try {
174
171
this . _task = NSURLSession . sharedSession . dataTaskWithURLCompletionHandler ( NSURL . URLWithString ( options . audioFile ) , ( data , response , error ) => {
175
172
if ( error !== null ) {
176
173
if ( this . errorCallback ) {
177
174
this . errorCallback ( { error } ) ;
178
175
}
179
-
180
- reject ( ) ;
176
+ reject ( error ) ;
181
177
}
182
178
183
- this . completeCallback = options . completeCallback ;
184
- this . errorCallback = options . errorCallback ;
185
- this . infoCallback = options . infoCallback ;
186
-
187
- const audioSession = AVAudioSession . sharedInstance ( ) ;
188
- audioSession . setCategoryModeRouteSharingPolicyOptionsError (
189
- options . sessionCategory || AVAudioSessionCategoryAmbient ,
190
- options . sessionMode || AVAudioSessionModeDefault ,
191
- options . sessionRouteSharingPolicy || AVAudioSessionRouteSharingPolicy . LongForm ,
192
- options . audioMixing ? AVAudioSessionCategoryOptions . MixWithOthers : AVAudioSessionCategoryOptions . DuckOthers ,
193
- //@ts -ignore
194
- null
195
- ) ;
196
- const output = audioSession . currentRoute . outputs . lastObject . portType ;
197
-
198
- if ( output . match ( / R e c e i v e r / ) ) {
199
- try {
200
- audioSession . setCategoryError ( AVAudioSessionCategoryPlayAndRecord ) ;
201
- audioSession . overrideOutputAudioPortError ( AVAudioSessionPortOverride . Speaker ) ;
202
- audioSession . setActiveError ( true ) ;
203
- } catch ( err ) {
204
- console . error ( 'Setting audioSession category failed.' , err ) ;
205
- }
206
- }
179
+ this . prepareAudioSession ( options ) ;
207
180
208
181
const errorRef = new interop . Reference ( ) ;
209
182
this . _player = AVAudioPlayer . alloc ( ) . initWithDataError ( data , errorRef ) ;
210
183
if ( errorRef && errorRef . value ) {
211
- reject ( errorRef . value ) ;
212
- return ;
184
+ return reject ( errorRef . value ) ;
213
185
} else if ( this . _player ) {
214
- this . _player . delegate = TNSPlayerDelegate . initWithOwner ( this ) ;
215
-
216
- // enableRate to change playback speed
217
- this . _player . enableRate = true ;
218
-
219
- this . _player . numberOfLoops = options . loop ? - 1 : 0 ;
220
-
221
- if ( options . metering ) {
222
- this . _player . meteringEnabled = true ;
223
- }
224
-
225
- if ( options . autoPlay ) {
226
- this . _player . play ( ) ;
227
- }
186
+ this . handleStartPlayer ( options ) ;
228
187
229
188
resolve ( null ) ;
230
189
} else {
0 commit comments