Skip to content

Commit

Permalink
fix(ios): avoid prematurely activating audio session (#2146)
Browse files Browse the repository at this point in the history
  • Loading branch information
puckey authored Oct 12, 2023
1 parent e28536c commit 1752a7c
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions ios/RNTrackPlayer/RNTrackPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,23 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
}


private func configureAudioSession(setActive: Bool = false) {
if #available(iOS 13.0, *) {
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, policy: sessionCategoryPolicy, options: sessionCategoryOptions)
} else if #available(iOS 11.0, *) {
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, policy: sessionCategoryPolicy, options: sessionCategoryOptions)
} else {
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, options: sessionCategoryOptions)
}
if (setActive) {
private func configureAudioSession() {

// deactivate the session when there is no current item to be played
if (player.currentItem == nil) {
try? audioSessionController.deactivateSession()
return
}

// activate the audio session when there is an item to be played
// and the player has been configured to start when it is ready loading:
if (player.playWhenReady) {
try? audioSessionController.activateSession()
if #available(iOS 11.0, *) {
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, policy: sessionCategoryPolicy, options: sessionCategoryOptions)
} else {
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, options: sessionCategoryOptions)
}
}
}

Expand Down Expand Up @@ -521,8 +527,6 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
@objc(play:rejecter:)
public func play(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
if (rejectWhenNotInitialized(reject: reject)) { return }

configureAudioSession(setActive: true)
player.play()
resolve(NSNull())
}
Expand All @@ -538,9 +542,6 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
@objc(setPlayWhenReady:resolver:rejecter:)
public func setPlayWhenReady(playWhenReady: Bool, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
if (rejectWhenNotInitialized(reject: reject)) { return }
if (playWhenReady) {
configureAudioSession(setActive: true)
}
player.playWhenReady = playWhenReady
resolve(NSNull())
}
Expand Down Expand Up @@ -860,6 +861,10 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
}
}

if ((item != nil && lastItem == nil) || item == nil) {
configureAudioSession();
}

var a: Dictionary<String, Any> = ["lastPosition": lastPosition ?? 0]
if let lastIndex = lastIndex {
a["lastIndex"] = lastIndex
Expand Down Expand Up @@ -908,6 +913,7 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
}

func handlePlayWhenReadyChange(playWhenReady: Bool) {
configureAudioSession();
emit(
event: EventType.PlaybackPlayWhenReadyChanged,
body: [
Expand Down

0 comments on commit 1752a7c

Please sign in to comment.