【问题标题】:AVAudioPlayer isPlaying always return nullAVAudioPlayer isPlaying 总是返回 null
【发布时间】:2010-06-25 16:31:46
【问题描述】:

我在使用 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 时,我得到了一个值...

【问题讨论】:

  • 你有没有设法解决这个问题?我有同样的问题,无法解决。

标签: iphone cocoa xcode avaudioplayer


【解决方案1】:
NSLog(@"%@", [self.audioPlayer isPlaying]); 

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

附言不要忘记调用 [audioPlayer 播放]; ;)

【讨论】:

  • 哦,好的,谢谢。但无论如何,如果我访问 a 中的值,它也总是返回 null 。我有一个 playOrPause 方法来检查它。因为当我第一次将值分配给变量然后查询变量时它正在工作,所以我想这并不重要。
【解决方案2】:

您可以使用 (audioPlay.playing) 代替 [audioPlayer isPlaying]

if (audioPlayer.playing) {
        [audioPlayer stop];
} else {
        [audioPlayer play];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2016-11-02
    • 2014-05-06
    • 2012-06-05
    • 2014-05-30
    • 2014-02-23
    • 2017-10-12
    相关资源
    最近更新 更多