Search code examples
xcodeaudioios4mp3

how to launch mp3 from code in XCode4


I'm writing an application for iOS4. Is there any fast way to launch an mp3 file from code? I need to loop on an mp3 file as long as the app is running.

Thank you.


Solution

  • Use the AVAudioPlayer:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    audioPlayer.numberOfLoops = -1; // infinite loops
    [audioPlayer play];
    

    This does the job.