AVAudioPlayer not playing audio in Swift

后端 未结 10 1470
抹茶落季
抹茶落季 2020-12-16 03:10

I have this code in a very simple, single view Swift application in my ViewController:

var audioPlayer = AVAudioPlayer()

@IBAction func playMyF         


        
10条回答
  •  醉酒成梦
    2020-12-16 03:16

    In Swift Coding using Try catch, this issues will solve and play audio for me and my code below,

    var playerVal = AVAudioPlayer()
    
             @IBAction func btnPlayAction(sender: AnyObject) {
                    let fileURL: NSURL = NSURL(string: url)!
                        let soundData = NSData(contentsOfURL: fileURL)
    
                        do {
                            playerVal = try AVAudioPlayer(data: soundData!)
                        }
                        catch {
                            print("Something bad happened. Try catching specific errors to narrow things down",error)
                        }
    
                        playerVal.delegate = self
                        playerVal.prepareToPlay()
    
                        playerVal.play()
    
                  }
    

提交回复
热议问题