当我保持手腕向下时,如何在 iWatch 应用程序中继续播放音频? - watchOS

While I keep my wrist down, how to keep playing audio in iWatch app? - watchOS

我正在尝试为 Apple Watch 构建一个音频应用程序。但问题是每当我放下手时,音频就会停止播放。 我也打开了后台模式。 谁能帮我解决这个问题?我被困在这部分。 这是我用来播放音频的代码。

func play(url : URL) {
        do {
            if #available(watchOSApplicationExtension 4.0, *) {
                WKExtension.shared().isFrontmostTimeoutExtended = true
            } else {
                // Fallback on earlier versions
            }
            self.player = try AVAudioPlayer(contentsOf: url)
                player!.prepareToPlay()
            player?.delegate = self
            player?.play()
            print("-----------------")
            print("Playing Audio")
            print("*****************\nCurrent Time \(String(describing: self.player?.currentTime))")
        } catch let error as NSError {
            self.player = nil
            print(error.localizedDescription)
        } catch {
            print("*************************")
            print("AVAudioPlayer init failed")
        }
    }

仅仅打开后台模式是不够的。您还需要激活 AVAudioSession.
Apple 在这里对此进行了详细记录:Playing Background Audio.

Configure and Activate the Audio Session

Before you can play audio, you need to set up and activate the audio session.

session.setCategory(AVAudioSession.Category.playback,
                    mode: .default,
                    policy: .longForm,
                    options: [])

Next, activate the session, by calling the activate(options:completionHandler:) method.

session.activate(options: []) { (success, error) in
    // Check for an error and play audio.
}

Ref: https://developer.apple.com/documentation/watchkit/playing_background_audio


示例:

var player: AVAudioPlayer?
let session: AVAudioSession = .sharedInstance()

func prepareSession() {
    do {
        try session.setCategory(AVAudioSession.Category.playback,
                                mode: .default,
                                policy: .longForm,
                                options: [])
    }
    catch {
        print(error)
    }
}

func play(url: URL) {
    do {
        player = try AVAudioPlayer(contentsOf: url)
    }
    catch {
        print(error)
        return
    }

    session.activate(options: []) { (success, error) in
        guard error == nil else {
            print(error!)
            return
        }

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

简单测试:

prepareSession()

if let url = Bundle.main.url(forResource: "test", withExtension: "mp3") {
    play(url: url)
}
else {
    print("test.mp3 not found in project: put any mp3 file in and name it so")
}

确保您尝试使用 Audio Data,而不是 Audio URL,并在您的类别设置中添加了 policy: .longFormAudio。根据 Apple documentation,必须设置这两个设置才能在后台模式下播放音频。

// Set up the session. 
let session = AVAudioSession.sharedInstance()
    
do {
    try session.setCategory(
        .playback,
        mode: .default,
        policy: .longFormAudio
    ) 
} 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. 
session.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() 
}

我已经测试了此代码及其仅在 Watch 应用程序中而非手表扬声器中使用蓝牙连接的情况。