views:

32

answers:

1

Hey guys,

I'm experiencing an odd problem with AVAudioPlayer class. Everytime I try to access one of it's properties they are null.

// Get the file path to the song to play.
 NSString *filePath = [[[NSBundle mainBundle] pathForResource:pSound.fileName 
               ofType:pSound.fileType] retain];

 // Convert the file path to a URL.
 NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

 NSError* err;
 //Initialize the AVAudioPlayer
 self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&err];

    if( err ){
  NSLog(@"Initializing failed with reason: %@", [err localizedDescription]);
 }
 else{
  [self.audioPlayer setDelegate:self];
  [self.audioPlayer prepareToPlay];
  self.playCount = self.playCount + 1;
        NSLog(@"%@", [self.audioPlayer isPlaying]); //this displays null; also if I call it at a later time
 };

 [filePath release];
 [fileURL release];

Any Ideas?

EDIT: Oddly enough when I save isPlaying to a bool and print the bool I get a value...

+1  A: 
NSLog(@"%@", [self.audioPlayer isPlaying]); 

%@ - mean that you want to print result of [yourObject toString] so if you want to check bool value returned by [self.audioPlayer isPlaying] - you should use %d.

p.s. don't foget call [audioPlayer play]; ;)

mOlind
Oh ok thank you.But anyway if I access the value in a if it's always returning null too. I have a playOrPause method where I check that.Since it's working when I first assign the value to a variable and then query the variable it doesn't matter I suppose.
Daniel