2010-06-25 85 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]; 
}