Search code examples
swiftavaudioplayerios13

Can't play local mp3 above ios13 swift


I have an app that play local mp3 files within the app that doesnt work in iOS 13 but lower os it works....

appending this in the array before:

Declare this in the class:

var MediaPlayer = AVAudioPlayer()

Sending in to the function in viewdidload:

TrackListData.append(Track(TrackName: "Introduction",TrackDescription: "About the good reasons for taking mindful breaks", TrackURL: URL(fileURLWithPath: Bundle.main.path(forResource: "Introduction", ofType: "mp3")!)))
setSong(trackURL: TrackListArray[0].TrackList[0].TrackURL)
func setSong(trackURL : URL){
        do{
            MediaPlayer.prepareToPlay()
            MediaPlayer = try AVAudioPlayer(contentsOf: trackURL)
            ProgressbarSong.maximumValue = Float(MediaPlayer.duration)
        }
        catch {
            print(error)
        }
    }

Get this error in the function setSong at line: MediaPlayer = try AVAudioPlayer(contentsOf: trackURL)

Error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x58)


Solution

  • Looks like a duplicate of 58166133.

    AVAudioPlayer doesn't have an init so you should update your first line, like this

    var MediaPlayer: AVAudioPlayer!