我初始化一个 AVAudioPlayer 实例:
NSString* path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"wav"];
AVAudioPlayer* player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil]; //Returned object not nil, indicating that the file has loaded successfully
BOOL b = [player prepareToPlay]; //returns TRUE
BOOL b2 = [player play];
//returns TRUE, goes immediately to the next line since asynchronous
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]; //wait 1 sec
printf("%10.4f\n",player.duration); //duration is 2s
printf("%10.4f\n",player.currentTime); //position remains 0
/* There is no sound output, player is also stuck since position==0 */
有谁知道为什么 AVAudioPlayer 没有响应?在初始化、播放音频播放器时,我是否忽略了什么?样板代码可能吗?
我在 iPhone 模拟器上运行它。此外,AudioServicesPlaySystemSound() 适用于同一个文件,这令人费解,但这表明声音播放可能也适用于 AVAudioPlayer。