7

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

sclv
  • 38,665
  • 7
  • 99
  • 204
Klem
  • 529
  • 1
  • 4
  • 18

4 Answers4

8

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

titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
6

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.

saadnib
  • 11,145
  • 2
  • 33
  • 54
6

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.

gnuchu
  • 1,496
  • 13
  • 21
  • Great! thanks. I already have implemented a sound streamer in OpenAL. Unfortunately bugs have appeared since IOS 4.3 and I have to change the API. – Klem Mar 16 '11 at 09:45
  • OpenAL bug on ios 4.3 is detailled here : https://devforums.apple.com/message/396043#396043 – Klem Mar 16 '11 at 09:47
4

" 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.

Michal Gumny
  • 1,770
  • 1
  • 16
  • 24
  • You can do NSData *data = [NSData dataWithContentsOfURL:soundUrl]; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:nil]; Basically it will stream the data into memory, and AVAudioPlayer will play from the data inside memory – Gia Dang Jun 10 '14 at 10:44
  • @GiaDang Yes, but your thread will hang until the audio file has been *completely* downloaded by the dataWithContentsOfURL: method, rather than playing within a second or two and buffering while playback continues, as AVPlayer does. So not only will this take a long time, but if you don't put this code on a background thread, the app will be *completely frozen* until the song has been downloaded. All in all, I don't think this is the greatest solution. – Jacob Pritchett May 16 '16 at 19:21