Iphone AVAudioPlayer显示始终返回null
我在AVAudioPlayer类中遇到了一个奇怪的问题。 每次我试图访问它的一个属性时,它们都是空的Iphone AVAudioPlayer显示始终返回null,iphone,cocoa,xcode,avaudioplayer,Iphone,Cocoa,Xcode,Avaudioplayer,我在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
// 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时,我得到一个值
NSLog(@"%@", [self.audioPlayer isPlaying]);
%@-表示要打印[yourObject toString]的结果
因此,如果要检查[self.audioPlayer isPlaying]返回的bool值,应该使用%d
p、 不要忘记呼叫[音频播放器播放];
;) 您可以使用(audioPlay.playing)而不是[audioPlayer isPlaying]
if (audioPlayer.playing) {
[audioPlayer stop];
} else {
[audioPlayer play];
}
哦,好的,谢谢。但是不管怎样,如果我访问一个函数中的值,如果它也总是返回null。我有一个playOrPause方法来检查它。因为当我第一次将值赋给变量,然后查询变量时,它就起作用了,所以我想这无关紧要。你有没有解决过这个问题?我也有同样的问题,无法解决。