开发者

AVAudioPlayer isPlaying always return null

开发者 https://www.devze.com 2023-01-04 14:51 出处:网络
I\'m experiencing an odd problem with AVAudioPlayer class. Everytime I try to access one of it\'s properties they are null.

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.audi开发者_运维知识库oPlayer 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...


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]; ;)


You can use the (audioPlay.playing) instead of [audioPlayer isPlaying]

if (audioPlayer.playing) {
        [audioPlayer stop];
} else {
        [audioPlayer play];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消