I am trying to play an MP3 via AVAudioPlayer which I thought to be fairly simple. Unfortunately, it's not quite working. Here is all I did:
- For the sake of testing, I created a new iOS application (Single View) in Xcode.
I added the AVFoundation framework to the project as well as the
#import <AVFoundation/AVFoundation.h>to theViewController.mI added an MP3 File to the Apps 'Documents' folder.
I changed the
ViewControllersviewDidLoad:to the following:
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* recorderFilePath = [NSString stringWithFormat:@"%@/MySound.mp3", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recorderFilePath] error:nil];
audioPlayer.numberOfLoops = 1;
[audioPlayer play];
//[NSThread sleepForTimeInterval:20];
}
Unfortunately, the audio obviously stops right after it starts playing. If I uncomment the sleepForTimeInterval it plays for 20 seconds and stops afterwards. This problem occurs only when compiling with ARC, otherwise, it works flawlessly.