Search code examples
iosswiftavfoundationavplayeravkit

How to Play and Save AudioFile from filePath iOS Swift


I'm selecting the audio file from the folder and getting the URL in this delegate function

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    print("url", url)
    self.filePickedBlock?(url)
}

Url looks like this

file:///private/var/mobile/Containers/Data/Application/8012C1BB-43E3-4E0C-9167-B7B5A49824A1/tmp/com.ios.PrankYourFriends-Inbox/AUDIO1.m4a

I am saving this URL as a string in Realm Database and fetching it when I want to play.

Issue I am Facing

When I play this Audio immediately after selecting then it's playing fine. But if I go to any other ViewController and Came back again and press the play button it's not playing the audio.

I am playing Audio like this:

self.avPlayer = AVPlayer.init(url: filePath)
self.avPlayer?.play()

Kindly let me know what I am doing wrong.


Solution

  • Its in tmp folder

    /tmp/com.ios.PrankYourFriends-Inbox/AUDIO1.m4a
    

    Copy it to say documents/library then use it any time