This code:
NSString *urlPath = [[NSBundle mainBundle] pathForResource:@"snd" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSError *err;
AVAudioPlayer* audioPlayerMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
[audioPlayerMusic play];
Works just fine.
While this one:
NSString *urlPath = [[NSBundle mainBundle] pathForResource:@"snd" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
AVPlayer* audioPlayerMusic = [AVPlayer playerWithURL:url];
[audioPlayerMusic play];
Plays nothing!
What’s going wrong?
When playing/streaming a remote file, AVPlayer isn’t ready to play it – you must wait for it to buffer enough data to start paying, while this is not necessary when using AVAudioPlayer. So, either use AVAudioPlayer, or make AVPlayer notify your class using key-value observing when it’s ready to begin playback:
And in your class (
selfrefers to its instance in the above line):