Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming with an AVAudioplayer

In order to load a sound file, I have the following code in my application :

- (id) init:(NSString*)a_filename ext:(NSString*)a_ext 
{
    ...

    NSString *t_soundFilePath = [CFileLoader getPathForResource:filename WithExtension:ext];
    NSURL *t_fileURL = [[[NSURL alloc] initFileURLWithPath: t_soundFilePath] autorelease];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL: t_fileURL error: nil];
    [player prepareToPlay];
    ...

    }

All the sounds that I load are in my bundle, so I would like to know if the method "initwithcontentsofurl" stream the sound file or if all the file is cached.

I have a lot of sprites in my app so I want to minimize memory space used for sounds.

thx for your help

like image 846
Klem Avatar asked Mar 15 '11 13:03

Klem


4 Answers

I used AVPlayer (not AVAudioPlayer) to stream background audio.

like image 163
titaniumdecoy Avatar answered Nov 15 '22 11:11

titaniumdecoy


Sounds that are played using the AVAudioPlayer are streamed real time from storage. The drawback being that you can occasionally notice a small lag when it is due to start up. If you use OpenAL the sounds are loaded entirely into memory.

There's a good discussion of this sort of thing in the ObjectAL documentation - a library that is meant to simplify the playing of sounds and effects on the iPhone.

like image 41
gnuchu Avatar answered Nov 15 '22 09:11

gnuchu


If you want to stream audio in your app you can use the following player instead of using AVAudioPlayer

https://github.com/mattgallagher/AudioStreamer

For this player you don't need to put sound files in your bundle, you can put them at server and use url to stream audio.

Hope, this wil help you.

like image 6
saadnib Avatar answered Nov 15 '22 11:11

saadnib


" The AVAudioPlayer class does not provide support for streaming audio based on HTTP URL's. The URL used with initWithContentsOfURL: must be a File URL (file://). That is, a local path".

https://developer.apple.com/library/ios/qa/qa1634/_index.html

But you can work around - for example write asynchronously to local file, and init AVAudioPlayer with this file URL, it will work.

like image 4
Michal Gumny Avatar answered Nov 15 '22 11:11

Michal Gumny