From 41ca90093ddde6d609a5365a5ad0be09053cb19b Mon Sep 17 00:00:00 2001 From: Robert Florin <39056172+BringTheChill@users.noreply.github.com> Date: Wed, 7 Jul 2021 16:45:02 +0300 Subject: [PATCH 1/2] Fix to not play music in silent mode on iOS --- darwin/Classes/Music.swift | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/darwin/Classes/Music.swift b/darwin/Classes/Music.swift index 9266cb9e..74ec1229 100644 --- a/darwin/Classes/Music.swift +++ b/darwin/Classes/Music.swift @@ -514,17 +514,6 @@ public class Player : NSObject, AVAudioPlayerDelegate { print("displayNotification " + displayNotification.description) print("url: " + url.absoluteString) - /* set session category and mode with options */ - if #available(iOS 10.0, *) { - //try AVAudioSession.sharedInstance().setCategory(category, mode: mode, options: [.mixWithOthers]) - try AVAudioSession.sharedInstance().setCategory(category, mode: .default, options: []) - try AVAudioSession.sharedInstance().setActive(true) - } else { - - try AVAudioSession.sharedInstance().setCategory(category) - try AVAudioSession.sharedInstance().setActive(true) - - } #endif var item : SlowMoPlayerItem @@ -590,6 +579,29 @@ public class Player : NSObject, AVAudioPlayerDelegate { #endif } + // Additional code to enable respectSilentMode from here. + #if os(iOS) + do{ + if #available(iOS 10.0, *) { + try AVAudioSession.sharedInstance().setCategory(category, mode: .default, options: []) + try AVAudioSession.sharedInstance().setActive(true) + } else { + try AVAudioSession.sharedInstance().setCategory(category) + try AVAudioSession.sharedInstance().setActive(true) + } + } catch let errorSetCategory { + result(FlutterError( + code: "PLAY_ERROR", + message: "Cannot play "+assetPath, + details: errorSetCategory.localizedDescription) + ) + self?.log(errorSetCategory.localizedDescription) + print(errorSetCategory.localizedDescription) + return; + } + #endif + // To here. + if(autoStart == true){ self?.play() } From 7f9882a4e911f02b2d286561462451df05cceb38 Mon Sep 17 00:00:00 2001 From: Robert Florin <39056172+BringTheChill@users.noreply.github.com> Date: Wed, 7 Jul 2021 17:26:55 +0300 Subject: [PATCH 2/2] Fix automatically playing when opening app --- darwin/Classes/Music.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/darwin/Classes/Music.swift b/darwin/Classes/Music.swift index 74ec1229..12bec3d4 100644 --- a/darwin/Classes/Music.swift +++ b/darwin/Classes/Music.swift @@ -556,7 +556,13 @@ public class Player : NSObject, AVAudioPlayerDelegate { self.setBuffering(true) self.isLiveStream = false observerStatus.append( item.observe(\.status, changeHandler: { [weak self] (item, value) in - + //don't execute readyToPlay again when coming back from foreground to avoid re-playing of audio due to line #603 + if(self?.currentStatus != .readyToPlay) { + self?.currentStatus = item.status + } else { + return + } + switch item.status { case .unknown: debugPrint("status: unknown") @@ -817,6 +823,7 @@ public class Player : NSObject, AVAudioPlayerDelegate { } func stop(){ + self.currentStatus = nil self.player?.pause() self.player?.rate = 0.0