Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to not play music in silent mode on iOS and automatically playing when opening app #609

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions darwin/Classes/Music.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -567,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")
Expand All @@ -590,6 +585,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()
}
Expand Down Expand Up @@ -805,6 +823,7 @@ public class Player : NSObject, AVAudioPlayerDelegate {
}

func stop(){
self.currentStatus = nil
self.player?.pause()
self.player?.rate = 0.0

Expand Down