I am building a MP3 player for iOS that plays audio files hosted on the web. I want to offer the ability to play the files offline so I have the files downloading using ASIHTTP but I am cannot seem to find info on initiallzing AVPlayer with a mp3 in the app documents directory. Has anyone done this before? Is it even possible?
*I posted an answer below that shows how to use the iOS AvPlayer for both local and http files. Hope this helps!
I decided to answer my own question because I felt like there is very little documentation on how to use the Apple provided AVPlayer for both local and stream (over http) files. To help understand the solution, I put together a sample project on GitHub in Objective-C and Swift The code below is Objective-C but you can download my Swift example to see that. It is very similar!
What I found is that the two ways of setting up the files are almost identical except for how you instantiate your NSURL for the Asset > PlayerItem > AVPlayer chain.
Here is an outline of the core methods
.h file (partial code)
.m file (partial code)
Check out the Objective-C source code for a working example of this. Hope this helps!
-Update 12/7/2015 I now have a Swift example of the source code you can view here.
Yes,thats possible to download and save the .mp3(or any kind of file)into NSDocument directory and then you can retrive from that and play by using AVAudioPlayer.
Retrieve the downloaded Audio and Play:
Just write your code to play Audio by using AudioURL
I Like to know if u have any clarification in this regard.
Thank you
its very difficult to play a song using a Avplayer why you are not use a MPMoviePlayerController player . i have play song from document directory .i am posting a code pls refer this .its working fine .and also u directly from url live .
I got
AVPlayer
to work with local URL by prependingfile://
to my local urlTry this