From 249a07f5e85ab2315bc7eba32d147ea5392eee0b Mon Sep 17 00:00:00 2001 From: Jacob Spizziri Date: Tue, 17 May 2022 15:00:14 -0500 Subject: [PATCH] fix(ios, stop, event.playbackprogressupdated): make the iOS API the same as the Android API for stop (#1529) additionally, fix an issue with Event.PlaybackProgressUpdated causing exceptions when fired after certain queue manipulations https://github.com/doublesymmetry/react-native-track-player/issues/1225 --- ios/RNTrackPlayer/RNTrackPlayer.swift | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/ios/RNTrackPlayer/RNTrackPlayer.swift b/ios/RNTrackPlayer/RNTrackPlayer.swift index 5ace43635..d6302b1b0 100644 --- a/ios/RNTrackPlayer/RNTrackPlayer.swift +++ b/ios/RNTrackPlayer/RNTrackPlayer.swift @@ -512,15 +512,11 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { resolve(NSNull()) } + // NOTE: this method is really just an alias for pause. It should NOT call `player.stop` as + // that will reset the player, which is not the API intent. @objc(stop:rejecter:) public func stop(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) { - if !hasInitialized { - reject("player_not_initialized", "The player is not initialized. Call setupPlayer first.", nil) - return - } - - player.stop() - resolve(NSNull()) + self.pause(resolve: resolve, reject: reject) } @objc(seekTo:resolver:rejecter:) @@ -840,9 +836,11 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { func handleAudioPlayerSecondElapse(seconds: Double) { // because you cannot prevent the `event.secondElapse` from firing // do not emit an event if `progressUpdateEventInterval` is nil - if shouldEmitUpdateEventInterval == false { return } + // additionally, there are certain instances in which this event is emitted + // _after_ a manipulation to the queu causing no currentItem to exist (see reset) + // in which case we shouldn't emit anything or we'll get an exception. + if shouldEmitUpdateEventInterval == false || player.currentItem == nil { return } - let track = player.items[player.currentIndex] as! Track sendEvent( withName: "playback-progress-updated", body: [