在使用调试脚本之前,AVAudioPlayer无法播放声音

时间:2018-02-05 08:52:06

标签: ios swift audio avaudioplayer

我正在使用Xcode 7.3.1和iOS 9,并且遇到AVAudioPlayer播放声音的问题。我已经尝试了所有可能的解决方案,没有任何作用这是我的代码

        let mp3Url = NSBundle.mainBundle().URLForResource("BCA", withExtension:"mp3")!

        var player = AVAudioPlayer()

        do {
            player = try AVAudioPlayer(contentsOfURL: mp3Url)
            player.prepareToPlay()
            player.play()
        } catch let error as NSError {
            print(error.description)
        }

player.play()并使用 po player 设置断点之前,声音才会播放。然后会播放声音。

任何想法对我都有帮助。

2 个答案:

答案 0 :(得分:0)

试试这个:

    var player: AVAudioPlayer?

    func playSound(){
        guard let url = Bundle.main.url(forResource: "alert_song", withExtension: "mp3") else {
            print("MP3 resource not found.")
            return
        }

        print("Music to play : \(String(describing: url))")
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)

            player = try AVAudioPlayer(contentsOf: url)
            guard let player = player else { return }
            player.play()
        } catch let error {
            print(error.localizedDescription)
        }

    }

答案 1 :(得分:0)

试试这个:

Warning: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found
Warning: Failed to download any source lists!