通过扬声器播放音频-watchOS / Swift

时间:2019-12-02 16:45:32

标签: swift audio streaming

我有麻烦了。我正在尝试通过Apple Watch的扬声器流式传输来自实时.mp3的音频,但是即使URL正确且不是nil,我始终会收到“ OSStatus错误2003334207”。

我在awake(withContext context:Any?)中完全使用此代码:

do {
            // Working Reroutes to headset
            //            try session.setCategory(AVAudioSession.Category.playback,
            //                                    mode: .default,
            //                                    policy: .longForm,
            //                                    options: [])

            // Plays in watch speaker
            try audioSession.setCategory(AVAudioSession.Category.playback,
                                         mode: .default,
                                         policy: .default,
                                         options: [])
        } catch let error {
            fatalError("*** Unable to set up the audio session: \(error.localizedDescription) ***")
        }
        if let path = context as? String {
            let fileUrl = URL(string: "https://sychrovnet1-priv.mtv.cdn.moderntv.eu/moderntv/stream/radio_seejay/20-mp3/live-media.mp3?_cdn_session=17861386792&_cdn_timestamp=1575730953&_cdn_token=34161d95974e0f33f00d2944819347a065128c81")
            do{
                player = try AVAudioPlayer(contentsOf: fileUrl!)
            }
            catch
            {
                print("*** Unable to set up the audio player: \(error.localizedDescription) ***")
                // Handle the error here.
                return
            }
        }

        audioSession.activate(options: []) { (success, error) in
            guard error == nil else {
                print("*** error occurred: \(error!.localizedDescription) ***")
                // Handle the error here.
                return
            }
            if(success){
                // Play the audio file.
                self.player.play()
            }
        }

有什么主意吗?谢谢!

0 个答案:

没有答案