iOS watch application Audio not working with background mode

1k views Asked by At

enter image description hereHere 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")
        }
    }
1

There are 1 answers

1
Abhijith Mohan On

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.

// Set up the session.
let audioSession = AVAudioSession.sharedInstance()

do {
    try audioSession.setCategory(AVAudioSession.Category.playback,
                            mode: .default,
                            policy: .longForm,
                            options: [])
} catch let error {
    fatalError("*** Unable to set up the audio session: \(error.localizedDescription) ***")
}

// Set up the player.
let player: AVAudioPlayer
do {
    player = try AVAudioPlayer(data: audioData)
} catch let error {
    print("*** Unable to set up the audio player: \(error.localizedDescription) ***")
    // Handle the error here.
    return
}

// Activate and request the route.
audioSession.activate(options: []) { (success, error) in
    guard error == nil else {
        print("*** An error occurred: \(error!.localizedDescription) ***")
        // Handle the error here.
        return
    }

    // Play the audio file.
    player.play()
}