How to let AVPlayer plays mp3 from URL without waiting to load the whole file in swift - ios

I am playing a file using AVPlayer and it is working fine. What I am looking for is that when the application plays the file it should play as soon as some enough buffer available. Currently what I have noticed is that if the file is big it waits for longer time to start playing the file which means it waits until the file is loaded in the buffer or so.
Any thoughts?

First, thank you Paul, your hint helped.
I have resolved the issue as follow:
Instead of having the URL as the mp3 file; I have created a text file and called it audio.m3u then I have just wrote the url address of my mp3 file in the newly created file (the m3u). Then played the file (audio.m3u) in the AVPlayer.
thats all :)

Related

AVAggregateAssetDownloadTask seems to load m3u8 file twice

I am using AssetPersistenceManager from this Apple Sample : Using AVFoundation to play and persist http live streams
I create an AVURLAsset with a playlist master url, then call downloadStream(for:) with this asset, and the AVAggregateAssetDownloadTask complete download successfully !
It saves a folder named 286943_chapter_01_C955A55455AC0C4E.movpkg which contains the downloaded segments, my .m3u8 file and a StreamInfoBoot.xml file.
The StreamInfoBook.xml file contains references between local segment (or .frag) file and m3u8 segment URL.
However, those urls are signed and generated by Cloudfront. And some times, some URLs inside the .m3u8 file doesn't match URLs in StreamInfoBook.xml because of Expires parameter of cloudfrount.
So I think maybe AVAggregateAssetDownloadTask is calling the .m3u8 file twice : Once to save the .m3u8 and once to download segments and write StreamInfoBook.xml.
Does anyone already faced this issue ? What could I do to check if my Asset is playable offline or not ?
Do you have any suggestion ?
Here is an exemple of not matching segment URLs
In .m3u8 downloaded file :
https: //ezvoiznbov32f.cloudfront.net/file/265579/audio/chapter_08_10.mp3?Expires=1576159816&Signature=vfAYZ8ivP7yKeiranpba3R24gTSxexX4Nb1VjbDrR917pyFQqi2DMMMf8gi3LcueKXxv0FITprXzLXV62Py0Ph4nMxp8ixf5qG5sYnW1JcO74BxNfJE~DEurN~yQgczNBOK4b~bEXrDDZId1AweYJrgjZtjSoyUwAGXf~LA_&Key-Pair-Id=APKAAFEFNOSQQ34Q
In StreamInfoBook.xml file :
https: //ezvoiznbov32f.cloudfront.net/file/265579/audio/chapter_08_10.mp3?Expires=1576159817&Signature=PZ~tqOeIa3Vm26qKTFYU1LCGfvsWNpkUDMi3eFr~EhyRTLj66CDInSwfpoorYjl4Kl4B9eQ1W8ZzTlIMDGvs36wJ235JOszeov23IuIOzAJTlNw7zQYEd4Y1tq~nyZinpFoyDRGk0CelKOhH91Itb3rpQUeMN6lDhO-cq1X1OM_&Key-Pair-Id=APKAAFEFNOSQQ34Q
I'm testing with iOS 13.2
Well, answering myself :
After a very long time trying to make AVAggregateAssetDownloadTask works in background while the application is killed by the system, I finally switched to AVAssetDownloadTask.
Now everything is working perfectly ! It's like magic !
The difference between aggregate and normal tasks is in media selections. But I don't need it for now....

Why isn't AVPlayer playing .wav file

I am downloading a .wav file from a server and storing it locally in the file system. Once the file has been downloaded I try and play the file using the AVPlayer.
The url of the file being stored is something like this:
/Users/user/Library/Developer/CoreSimulator/Devices/FC767376-76BC-4402-B51B-SDFS/data/Containers/Data/Application/FA18B9RE-8D6E-4553-0617-4A064478AFF4/Documents/voice/test.wav
I have gone through all the related questions and have checked that I am doing the following:
Keeping reference to the AVPlayer in the viewController
Setting the category on the AVAudioSession
Setting the AVAudioSession to active
using NSURL(fileURLWithPath:) to retrieve the file url
I have tried using AVAudioPlayer
I have also confirmed the url being set on the AVPlayer does infact contain the audio file.
I have generated a test project that reproduces this issues. It can be found at https://github.com/va3093/AVAudioTest
Is it the way that I am storing the file? Is there something wrong with the file formats?
It turns out that the webservice I was using to generate the audiofiles genertate .mp3 files while I was saving them as .wav. When I changed to .mp3 then the audio player started playing the audio files.

Background Audio Player: not working on device

I'm trying to use the Background Audio Player without success. The application works perfectly in the emulator but not on a Nokia Lumia 800. I've read the whole thread at: http://social.msdn.microsoft.com/Forums/en-US/wpdevelop/thread/394de7c4-4334-46f8-a01a-30b49c6ec242/ but this is not a codec issue.
What am I doing? I create an AudioTrack object with a remote server source URI and set it to the BAP instance. Then I call Play(). The player's state is going to "Playing" but no sound on the device.
What did I did to ensure that this is not a codec issue? I updated the code described above to first download the whole file from the remote server source URI and save it to the isolated storage. Then I created an AudioTrack object with the local file URI and got sound.
So, I suspect this is a buffering issue, as my player never change its state to BufferingStarted nor BufferingStopped. But, unfortunately, I don't know what I can do to help on this.
Any ideas?
Thanks a lot in advance,
Fabian
There is a great article about that kind of problem: http://www.johanpaul.com/blog/2012/09/wp75-backgroundaudioplayer-crashes/

Intro MP3 file with SHOUTcast

I have a PHP MP3 file which changes every time it is loaded, and plays a different advert, what I would like is away or is there away to play that before the SHOUTcast stream as the SHOUTcast intro file.
I personally dont really want to put it into flash or create custom playlist files but more would like SHOUTcast to fetch an intro file from a URL address.
Unfortunately, the SHOUTcast configuration doesn't allow this directly.
However, you can easily accomplish what you need by having a script pick a new intro file every few seconds or so. Then you can either write that MP3 to the file configured in SHOUTcast, or create a symlink and update it.

How to play a downloaded mp3 file?

I have downloaded a mp3 file from a server successfully, but the problem I encountered is, I don't know how to play that file.. Should I use AVAudioPlayer or AudioQueue to play the file?? I will be appreciate if you could post some sample code here.
Both AVAudioPlayer and the Audio Queue API can be used to play MP3 files. The AVAudioPlayer is well documented and has a lot of sample code available. So why not give it try first and if that does not work, post a more specific question here.

Resources