iphone - AVAudioPlayer isPlaying 始终返回 null

标签 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 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。

附:不要忘记调用 [audioPlayer play]; ;)

关于iphone - AVAudioPlayer isPlaying 始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3119731/

相关文章:

javascript - 我可以在 iPhone 上运行 javascript 运行时(如 v8)吗?

cocoa - 如何在基于文档的应用程序中制作safari的tabView?

objective-c - 用于类延续的 Xcode 代码片段?

ios - 没有这样的表 Sqlite 错误 iOS

iphone - 对 NSArray 对象进行排序 WRT 这些对象中 NSSet 中包含的对象

cocoa - NSWindow 最大化?

c++ - 如何将 C++ 库导入 Xcode Objective C 项目?

c++ - 有人在 Mac 上使用过 libvlc 吗?

iphone - 如何使用 CMake 添加额外的 plist 属性?

iphone - 在 _one_ ASIHTTPRequest 中流式传输多个文件