Quantcast
Viewing latest article 2
Browse Latest Browse All 3

Answer by Masoud Dadashi for AVAudioPlayer not playing the MP3

this piece of code worked for me fine

func playAudio(){     let audioUrl = URL(fileURLWithPath: value, relativeTo: someBasePath)            do{                try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)                try AVAudioSession.sharedInstance().setActive(true)                audioPlayer = try AVAudioPlayer(contentsOf: audioUrl, fileTypeHint: AVFileTypeMPEGLayer3)                audioPlayer.prepareToPlay()                audioPlayer.play()            }catch{                print(error.localizedDescription)            }}

and you have to define audioPlayer as a class member or it goes out of scope otherwise and it won't work.

var audioPlayer: AVAudioPlayer!

thats it


Viewing latest article 2
Browse Latest Browse All 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>