2010-06-25 59 views
0

我遇到AVAudioPlayer类的奇怪问题。 每次尝试访问它的一个属性时,它们都是空的。AVAudioPlayer isPlaying始终返回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]; 

任何想法?

编辑:奇怪的是当我保存IsPlaying模块为BOOL和打印布尔我得到一个值...

+0

你有没有设法解决这个问题?我有同样的问题,无法弄清楚。 – Neeku 2013-11-23 15:59:07

回答

2
NSLog(@"%@", [self.audioPlayer isPlaying]); 

%@ - 表示要打印的[yourObject的toString] 所以结果如果你想检查由[self.audioPlayer isPlaying]返回的布尔值 - 你应该使用%d。

p.s.不要致电[audioPlayer play]; ;)

+0

哦,好的,谢谢。 但无论如何,如果我访问的值,如果它总是返回null。我有一个playOrPause方法在那里检查。 因为它的工作原理当我第一次将值赋值给一个变量,然后查询变量,我想这不重要。 – Daniel 2010-06-25 19:50:59

0

可以使用(audioPlay.playing)而不是[audioPlayer IsPlaying模块]

if (audioPlayer.playing) { 
     [audioPlayer stop]; 
} else { 
     [audioPlayer play]; 
} 
相关问题