gpt4 book ai didi

ios - AVAudioPlayer 播放在硬件上返回 false

转载 作者:可可西里 更新时间:2023-11-01 05:04:05 29 4
gpt4 key购买 nike

我正在尝试编写一些非常简单的代码来播放音频文件。该代码在 iPad2 模拟器上运行良好。然而,在我的 iPad2 设备上,[audooPlayer play] 返回 false 并且听不到任何音频。

我试过 .caf 和 .wav 文件。我重新启动了我的 iPad2 并确保静音开关已关闭。非常感谢有关如何调试的任何提示。

示例代码:

- (void)playAudio {
NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];
url = [NSURL fileURLWithPath:file];
NSError *error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error != nil) {
NSLog(@"Error creating audio player: %@", [error localizedDescription]);
return;
}
[audioPlayer setDelegate:self];
if (![audioPlayer prepareToPlay]) {
NSLog(@"Error preparing audio");
return;
}
// Everything works fine up to this point
if (![audioPlayer play]) {
// I hit this on hardware. Works perfect on the simulator.
NSLog(@"Error playing audio");
return;
}

最佳答案

经过太多的调试,我终于找到了罪魁祸首。我正在按照 http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2 的指南进行操作,并且在我的应用程序中播放音频之前,我录制了一些音频。根据我打电话的指南

[[AVAudioSession sharedInstance] setActive: NO error: nil];

录制完成后。这显然会导致 AVAudioPlayer 无法播放音频。将调用更改为

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

而是修复问题。很奇怪,这不是模拟器中的问题,而是导致硬件问题。

关于ios - AVAudioPlayer 播放在硬件上返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13618810/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com