AVAudioPlayer only initializes some files
I am having trouble playing some files
with AVAudioPlayer
. When I try to play a specific m4a it works fine. It also works with mp3 which I am trying. However, it fails on one particular one mp3
every time (15 Step, by Radiohead), regardless of the order in which I try to reproduce them. The sound just doesn't play
, although the image loading and everything that happens at the same time is happening correctly. The code is below. I get "Player loaded". the magazine released two other songs, but not 15 Step. I know which is file path
correct (I have the log printed earlier in the application and it is correct). Any ideas?
NSData *musicData = [NSData dataWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[song filename] ofType:nil]]];
NSLog([[NSBundle mainBundle] pathForResource:[song filename] ofType:nil]);
if(musicData)
{
NSLog(@"File found.");
}
self.songView.player = [[AVAudioPlayer alloc] initWithData:musicData error:nil];
if(self.songView.player)
{
NSLog(@"Player loaded.");
}
[self.songView.player play];
NSLog(@"You should be hearing something now.");
source to share
I'm sorry for attending the party so late. I just want to send a review so that it will be useful for anyone linking to it in the future.
Small correction for shaggy frog . Correct use:
NSError *anError = nil;
anAVAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:someURL] error:&anError];
if (!anAVAudioPlayer) {
NSLog(@"localizedDescription : %@", [anError localizedDescription]);
}
source to share
NSData *musicData = [NSData dataWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[song filename] ofType:nil]]];
Here musicData
nil
for a problem case?
also:
self.songView.player = [[AVAudioPlayer alloc] initWithData:musicData error:nil];
You shouldn't pass nil
for methods that take an object NSError
. It will probably shed more light on your problem if you use it correctly:
NSError* error = nil;
self.songView.player = [[AVAudioPlayer alloc] initWithData:musicData error:&error];
if (error)
{
NSLog(@"Error with initWithData: %@", [error localizedDescription]);
}
source to share