问题描述
我有一个非常简单的应用程序,可以用来测试AVAudioPlayer.我有一个16s aif文件,可以在Mac上正常播放.将其放在一个简单的应用中,然后在我的iPhone上运行,但没有声音.向对象发送播放"将返回成功,而audioPlayerDidFinishPlaying将在16秒后成功返回.仍然没有声音.已经检查了iPhone上的音量,就可以了.检查以确保我的AVAudioPlayer对象的音量设置为1.0-确实如此.有人可以帮忙吗?
I have a very simple app that I have built to test out AVAudioPlayer. I have a 16s aif file that plays just fine on my Mac. Put it in a simple app and ran on my iPhone but no sound. Sending 'play' to the object returns success and audioPlayerDidFinishPlaying comes back with success after 16 seconds. Still no sound. Have checked the sound volume on the iPhone, just fine. Have checked to make sure my AVAudioPlayer object has volume set at 1.0 - it does. Can anybody help?
这是我的代码
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的示例代码复制而来):
OK - got it figured out. I had not configured the AVAudioSession for my app. Did something very simple as follows (copied from Apple's example code):
[[AVAudioSession sharedInstance] setDelegate: self];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting category! %@", setCategoryError);
现在可以播放声音了-并学到了很多有关如何通过屏幕锁定等控制播放的知识,这将很有价值.
Sound now plays - plus learned a lot about how to control playback through screen locks, etc., which will be valuable.
这篇关于AVAudioPlayer返回成功,但设备未发出声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!