gpt4 book ai didi

objective-c - AVAudioPlayer 返回成功但设备没有声音

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:28 24 4
gpt4 key购买 nike

我构建了一个非常简单的应用程序来测试 AVAudioPlayer。我有一个 16 秒的 aif 文件,它在我的 Mac 上播放得很好。将它放在一个简单的应用程序中并在我的 iPhone 上运行但没有声音。向对象发送“播放”返回成功,audioPlayerDidFinishPlaying 在 16 秒后返回成功。还是没有声音。检查了 iPhone 上的音量,没问题。已检查以确保我的 AVAudioPlayer 对象的音量设置为 1.0 - 它确实如此。有人可以帮忙吗?

这是我的代码

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Soothing" ofType:@"aif"];
NSURL *playURL = [NSURL fileURLWithPath:soundPath];
NSError *error;

self.myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playURL error:&error];
NSLog(@"About to play sound");

self.myAudioPlayer.delegate = self;
[self.myAudioPlayer prepareToPlay];
bool success = [self.myAudioPlayer play];

if (!success) {
NSLog(@"Failed to play file");
} else NSLog(@"Looks like it succeded");

最佳答案

好的 - 明白了。我没有为我的应用配置 AVAudioSession。做了如下非常简单的事情(从 Apple 的示例代码中复制):

[[AVAudioSession sharedInstance] setDelegate: self];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting category! %@", setCategoryError);

现在可以播放声音 - 并且学到了很多有关如何通过屏幕锁定等方式控制播放的知识,这将很有值(value)。

关于objective-c - AVAudioPlayer 返回成功但设备没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12532290/

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