Here i am attaching my code and permission screen shot please advice what is the issues here
i Have tried apple developer guideline with this url https://developer.apple.com/documentation/watchkit/playing_background_audio
but still not working.
func play(url : URL) {
if #available(watchOSApplicationExtension 5.0, *) {
do {
WKExtension.shared().isFrontmostTimeoutExtended = true
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category(rawValue: AVAudioSession.Category.playback.rawValue), mode: AVAudioSession.Mode.moviePlayback, options: AVAudioSession.CategoryOptions.duckOthers)
} catch let error {
print("** Unable to set up the audio session: \(error.localizedDescription) **")
// Handle the error here.
return
}
do {
self.player = try AVAudioPlayer(contentsOf: url)
// player!.prepareToPlay()
player?.delegate = self
} catch let error {
print("** Unable to set up the audio player: \(error.localizedDescription) **")
// Handle the error here.
return
}
print("\nPlaying audio!")
self.player?.play()
// Activate and request the route.
audioSession?.activate(options: []) { (success, error) in
print("Success \(success)")
print("error \(String(describing: error))")
guard error == nil else {
print("** An error occurred: \(error!.localizedDescription) **")
// Handle the error here.
return
}
// Play the audio file.
if success {
} else {
print("audio session activation failded")
}
}
} else {
print("alert")
}
}
You need to set the category before the activate option
Code listing below shows all the steps needed to set up the session, activate it, and begin playing.