views:

134

answers:

1

Hi everyone,

I'm having trouble playing some files with AVAudioPlayer. When I try to play a certain m4a, it works fine. It also works with an mp3 that I try. However it fails on one particular mp3 every time (15 Step, by Radiohead), regardless of the order in which I try to play them. The audio just does not play, though the view loading and everything that happens concurrently happens correctly. The code is below. I get the "Player loaded." log output on the other two songs, but not on 15 Step. I know the file path is correct (I have it log outputted earlier in the app, 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.");

Thanks, Brendan

A: 
NSData *musicData = [NSData dataWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[song filename] ofType:nil]]];

Here, is musicData nil for the problem case?

Also:

self.songView.player = [[AVAudioPlayer alloc] initWithData:musicData error:nil];

You shouldn't pass nil for methods that take an NSError object. Likely it will 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]);
}
Shaggy Frog
Thanks for the tip with NSError... I'm new to objective-c and wasn't sure how to use it properly. I'll give that a shot.Unfortunately, musicData is not nil in the problem case. I logged the URL that goes into it, and it is correct. I'll try some more stuff and repost if I continue to have an issue.Thanks again!
Brendan