AVAudioPlayer isPlaying always return null

自古美人都是妖i 提交于 2019-12-13 06:42:31

问题


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:


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




回答2:


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

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


来源:https://stackoverflow.com/questions/3119731/avaudioplayer-isplaying-always-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!