【发布时间】:2011-03-13 12:51:52
【问题描述】:
我的应用中有 2 个视图,它们是相同的,它们只是加载到不同的声音文件数组中,以便与它们各自的 AVAudioPlayer 一起使用。 但是,仅在设备上,在我的第二个视图中,声音似乎在初始化后只播放一次,然后在我再次初始化播放器之前它不会播放。
这是我的播放按钮代码:
- (IBAction)play:(id)sender {
if (((int)buttonCount % 2) == 0) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
self.audioPlayer.currentTime = 0;
self.audioPlayer.volume = volumeSlider.value;
[self.audioPlayer play];
[playButton setTitle:@"Stop..." forState:UIControlStateNormal];
buttonCount++;
[self fadePickerAnimation:NO];
NSLog(@"Should play");
}
else {
[self initializeSoundAfterStop:YES];
NSLog(@"Shouldn't play");
}
NSLog(@"buttoncount %i",buttonCount);
NSLog(@"What is the playing state? %i", self.audioPlayer.playing);
}
以及它在 else 子句下调用的函数:
- (void)initializeSoundAfterStop:(BOOL)addToButtonCount {
//Initialize audioPlayer once completely stopped
[self.audioPlayer stop];
[self.audioPlayer prepareToPlay];
[playButton setTitle:@"Chime!" forState:UIControlStateNormal];
if (addToButtonCount == YES) {
buttonCount++;
}}
当我连续按下播放按钮时,上面代码中的 NSLogs 会输出这个:
Should play
buttoncount 1
What is the playing state? 1
Shouldn't play
buttoncount 2
What is the playing state? 0
Should play
buttoncount 3
What is the playing state? 0
Shouldn't play
buttoncount 4
What is the playing state? 0
Should play
buttoncount 5
What is the playing state? 0
Shouldn't play
buttoncount 6
What is the playing state? 0
如您所见,由于某种原因,它卡在了非播放模式中。 在我的第一个视图中,此方法的使用方式完全相同,并且没有任何问题。在模拟器上一切正常,只是在我的 iPhone 4 上不行。无法弄清楚。设置断点时会调用 audioPlayer 的播放方法,所以我不知道! 谢谢!
【问题讨论】:
-
刚刚在我的播放按钮IBAction中添加了这个:NSLog(@"%@", self.audioPlayer.url);并且控制台显示每次我按下它都会找到声音URL。那为什么不播放!?
标签: iphone objective-c avaudioplayer