AVAudioPlayer on iPhone Simulator

AVAudioPlayerWith iPhone 2.2, AVAudioPlayer Class is introduced.
AVAudioPlayer  simplifies making an object from a file, start/stop/pause media, volume control and looping. 
You can find documentation of this class via Apple iPhone Developer site, here we will discuss an issue you’ll probably face while trying to use this class on simulator. 

If you are facing problem, playing your media with AVAudioPlayer on Simulator.
Check your /Library/Quicktime directory.
Browse files in this directory and find  third party add-ons like Perian, DivX etc.
Move these add-ons in to another folder while you are developing with AVAudioPlayer
Now Test your Application, AVAudioPlayer should run without any problem.
Don’t forget to move your add-ons back to Quicktime Directory after you finished development ;)

 Here is a quick AVAudioPlayer usage example, 

 

-(IBAction)playMovie:(id)sender{

NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@”MusicFile”  ofType:@”mp3″];

audioPlayer =  [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];

[audioPlayer setDelegate:self];

[audioPlayer prepareToPlay];

BOOL plays = [audioPlayer play];

}
 

Thanks to Mert Yentur, for discovering and sharing that issue. 
Bests.
G.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Post Metadata

Date
January 25th, 2009

Author
Guvener Gokce


8 Comments


  1. stationstops

    bless you sir.

    good god, how long would I have been a programmer without google?



  2. t

    Oh man. I’ve been wrestling with this for DAYS. Thank you kind sir.



  3. Rich

    This is great…I never would have figured that out. Thanks!


  4. Thanks for this example.This is simplest and exaplainful

    I have some other problem, and i think it is realted with AVAuidioPlayer class..

    I am workin on an application that allows users to play and record Audio/Video.
    But problem is that, functionality is working fine on simulator.. Its recording and playing too..

    But on iPhone, its not playing the file, even though file is getting recorded..

    Thanks in advance :)



  5. shiva

    Hi,

    I dont have any problem with AVAudioPlayer in simulator. But i am facing strange memory leak.

    In simulator :
    GeneralBlock-240,
    SegmentMachO

    in Device
    GeneralBlock-3584

    Could any one give me some sample code..

    Thank you
    shiva


  6. Hi Harinder,
    It is hard to find out real problem without checking the code but I think you should check the recording format. Because When targeting the Simulator, if you are using SpeakHere sample of Apple, SpeakHere uses linear PCM regardless of the format specified in the code. May be you should try other recording formats and see the results :)
    Other Recording formats:
    kAudioFormatLinearPCM, kAudioFormatAppleLossless, kAudioFormatAppleIMA4, kAudioFormatiLBC, kAudioFormatULaw,kAudioFormatALaw

    Hi Shiva,
    I have created a sample application for you,
    http://www.guvenergokce.com/codesample/AVAudioPlayerSample.zip
    BTW you should also check Apple’s avTouch sample code.
    http://developer.apple.com/iphone/library/samplecode/avTouch/index.html

    Bests,
    G.


  7. hi guvener,
    i downloaded apple’s avTouch.zip, but errors showing up when compiling in these lines:

    - (void)updateCurrentTime;
    - (void)updateViewForPlayerState; : error: expected `{’ before ‘-’ token
    - (void)updateViewForPlayerInfo; : error: expected `{’ before ‘-’ token

    - (void)setupAudioSession; : error: expected `{’ before ‘-’ token

    - (void)ffwd : error: expected `{’ before ‘-’ token
    - (void)rewind : error: expected `{’ before ‘-’ token

    - (void)awakeFromNib : error: expected `{’ before ‘-’ token

    and i did not chance anything inside the code!
    what is wrong there?

    greetings
    blacksheep


  8. Oh wow just what ya posted about really made me happier! (ok i know you’ll reckon im a retard haha!) You deserve to spend some time relaxing with some decent


Leave a Reply