AVAudioPlayer not playing audio in Swift

后端 未结 10 1480
抹茶落季
抹茶落季 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:17

    Here is a working snippet from my swift project. Replace "audiofile" by your file name.

        var audioPlayer = AVAudioPlayer()
        let audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("audiofile", ofType: "mp3"))
        audioPlayer = AVAudioPlayer(contentsOfURL: audioPath, error: nil)
        audioPlayer.delegate = self
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    

    You can download fully functional Swift Audio Player application source code from here https://github.com/bpolat/Music-Player

提交回复
热议问题