@@ -13,6 +13,8 @@ import MediaPlayer
13
13
public class RNTrackPlayer : RCTEventEmitter {
14
14
15
15
// MARK: - Attributes
16
+
17
+ private var hasInitialized = false
16
18
17
19
private lazy var player : QueuedAudioPlayer = {
18
20
let player = QueuedAudioPlayer ( )
@@ -136,6 +138,11 @@ public class RNTrackPlayer: RCTEventEmitter {
136
138
137
139
@objc ( setupPlayer: resolver: rejecter: )
138
140
public func setupPlayer( config: [ String : Any ] , resolve: RCTPromiseResolveBlock , reject: RCTPromiseRejectBlock ) {
141
+ if hasInitialized {
142
+ resolve ( NSNull ( ) )
143
+ return
144
+ }
145
+
139
146
setupInterruptionHandling ( ) ;
140
147
141
148
// configure if player waits to play
@@ -192,34 +199,12 @@ public class RNTrackPlayer: RCTEventEmitter {
192
199
}
193
200
}
194
201
195
- resolve ( NSNull ( ) )
196
- }
197
-
198
- @objc ( destroy)
199
- public func destroy( ) {
200
- print ( " Destroying player " )
201
- }
202
-
203
- @objc ( updateOptions: resolver: rejecter: )
204
- public func update( options: [ String : Any ] , resolve: RCTPromiseResolveBlock , reject: RCTPromiseRejectBlock ) {
205
- let capabilitiesStr = options [ " capabilities " ] as? [ String ]
206
- let capabilities = capabilitiesStr? . compactMap { Capability ( rawValue: $0) } ?? [ ]
207
-
208
- let remoteCommands = capabilities. map { capability in
209
- capability. mapToPlayerCommand ( jumpInterval: options [ " jumpInterval " ] as? NSNumber ,
210
- likeOptions: options [ " likeOptions " ] as? [ String : Any ] ,
211
- dislikeOptions: options [ " dislikeOptions " ] as? [ String : Any ] ,
212
- bookmarkOptions: options [ " bookmarkOptions " ] as? [ String : Any ] )
213
- }
214
-
215
- player. enableRemoteCommands ( remoteCommands)
216
-
217
202
player. remoteCommandController. handleChangePlaybackPositionCommand = { [ weak self] event in
218
203
if let event = event as? MPChangePlaybackPositionCommandEvent {
219
204
self ? . sendEvent ( withName: " remote-seek " , body: [ " position " : event. positionTime] )
220
205
return MPRemoteCommandHandlerStatus . success
221
206
}
222
-
207
+
223
208
return MPRemoteCommandHandlerStatus . commandFailed
224
209
}
225
210
@@ -293,6 +278,29 @@ public class RNTrackPlayer: RCTEventEmitter {
293
278
return MPRemoteCommandHandlerStatus . success
294
279
}
295
280
281
+ hasInitialized = true
282
+ resolve ( NSNull ( ) )
283
+ }
284
+
285
+ @objc ( destroy)
286
+ public func destroy( ) {
287
+ print ( " Destroying player " )
288
+ }
289
+
290
+ @objc ( updateOptions: resolver: rejecter: )
291
+ public func update( options: [ String : Any ] , resolve: RCTPromiseResolveBlock , reject: RCTPromiseRejectBlock ) {
292
+ let capabilitiesStr = options [ " capabilities " ] as? [ String ]
293
+ let capabilities = capabilitiesStr? . compactMap { Capability ( rawValue: $0) } ?? [ ]
294
+
295
+ let remoteCommands = capabilities. map { capability in
296
+ capability. mapToPlayerCommand ( jumpInterval: options [ " jumpInterval " ] as? NSNumber ,
297
+ likeOptions: options [ " likeOptions " ] as? [ String : Any ] ,
298
+ dislikeOptions: options [ " dislikeOptions " ] as? [ String : Any ] ,
299
+ bookmarkOptions: options [ " bookmarkOptions " ] as? [ String : Any ] )
300
+ }
301
+
302
+ player. enableRemoteCommands ( remoteCommands)
303
+
296
304
resolve ( NSNull ( ) )
297
305
}
298
306
0 commit comments