我遇到了AVAudioPlayer类的奇怪问题。 每当我尝试访问其中一个属性时,它们都为空。
// 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并打印bool我得到一个值...
答案 0 :(得分:2)
NSLog(@"%@", [self.audioPlayer isPlaying]);
%@ - 表示您要打印[yourObject toString]的结果 所以如果你想检查[self.audioPlayer isPlaying]返回的bool值 - 你应该使用%d。
P.S。不要忘记打电话[audioPlayer play]; ;)
答案 1 :(得分:-1)
您可以使用(audioPlay.playing)代替[audioPlayer isPlaying]
if (audioPlayer.playing) {
[audioPlayer stop];
} else {
[audioPlayer play];
}