通过扬声器的 AVAudioPlayer
我得到以下代码:
- (id)init {
if (self = [super init]) {
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
}
return self;
}
但不知何故,声音不想从扬声器中发出,有人可以看到我做错了什么吗?
我用来玩的代码是:
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFilePathURL error:nil];
[player prepareToPlay];
[player setVolume:1.0];
[player play];
I got the following code:
- (id)init {
if (self = [super init]) {
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
}
return self;
}
But somehow the sound does not want to come out of the speakers, can someone see what I am doing wrong?
The code I use for playing is:
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFilePathURL error:nil];
[player prepareToPlay];
[player setVolume:1.0];
[player play];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
设置它的方式很挑剔...
使用
AVAudioSessionCategoryPlayAndRecord
非常重要,否则路线将无法到达扬声器。设置音频会话的覆盖路由后,您可以使用AVAudioPlayer
实例并向扬声器发送一些输出。希望这对其他人有用,就像对我一样。有关于此的文档很分散,但 Skype 应用程序证明这是可能的。坚持下去,我的朋友们! :)
这里有一些苹果文档: http:// /developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html
在页面上搜索
kAudioSessionProperty_OverrideAudioRoute
It's picky about how you set it up...
It's very important to use
AVAudioSessionCategoryPlayAndRecord
or the route will fail to go to the speaker. Once you've set the override route for the audio session, you can use anAVAudioPlayer
instance and send some output to the speaker.Hope that works for others like it did for me. The documentation on this is scattered, but the Skype app proves it's possible. Persevere, my friends! :)
Some Apple documentation here: http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html
Do a search on the page for
kAudioSessionProperty_OverrideAudioRoute
对于 iOS7,AudioSessionSetProperty 已弃用。 Foundry 在以下帖子中的答案展示了如何针对 iOS7 执行此操作:
https://stackoverflow.com/a/18808124/1949877< /a>
For iOS7 AudioSessionSetProperty is deprecated. The answer in the following post by foundry shows how to do this for iOS7:
https://stackoverflow.com/a/18808124/1949877
这里有很好的例子 http://ruckt.info/playing-sound-through-iphone-扬声器/ 解决了我的问题。
下面的方法 - configureAVAudioSession - 将音频重新路由到 iOS 7.0 中的主扬声器。为此,请在播放音频之前调用它:
[self configureAVAudioSession]
。Very good example here http://ruckt.info/playing-sound-through-iphone-speaker/ Solved my problem.
The method below - configureAVAudioSession - reroutes the audio to the main speakers in iOS 7.0. To do so, call it before playing the audio in this way:
[self configureAVAudioSession]
.我使用了 AudioToolbox 框架,这就是我初始化音频会话的原因,如下所示:
这是我用来配置音频会话的其余代码。我没有覆盖音频路线,我也认为这是没有必要的。
I used the AudioToolbox framework that's why I initialized my audio session as following:
Here's the rest of my code that I used to configure the audio session. I didn't override the audio route and I also think this is not necessary.
我只是想向这篇文章的其他读者澄清这一点。
我一直在寻找这个问题的答案,特别是针对 SpeakHere 示例。我抓住了这一小块代码,它成功了。
我希望这不是太离题。我只是认为这对于那些只需要这两个小声明的人来说是有好处的。
I'm just going to clarify this for other readers of this post.
I was looking for the answer to this problem, specifically for the SpeakHere example. I grabbed this little chunk of code and it did the trick.
I hope this is not too much of a digression. I just thought it would be good for those who just need these two little statements.
您用于播放声音的代码看起来是正确的,并且您没有理由必须使用 AudioToolbox。您可能需要确保的一件事是您使用的是受支持的音频格式。 (我个人在 MP3 和 WAV 方面取得了成功)
另外,您能否与我们分享您的代码,了解如何获取声音文件的 url?
You're code for playing the sound looks correct, and there's no reason you should have to use AudioToolbox. One thing you might want to make sure of is that you are using supported audio formats. (I personally have had success with MP3 and WAV)
Also, could you share with us your code as to how you get the url to your sound file?
虽然其他答案确实提供了在扬声器而不是接收器上播放音频的预期效果,但它们并不是实现该目标的惯用方式。
人们应该使用
AVAudioSessionCategoryOption
(真是拗口)DefaultToSpeaker
。其文档如下(强调我的):要使用此功能,请将您的调用更改为
setCategory:
以传递选项,如下所示:While the other answers do provide the desired effect of playing audio on the speaker instead of the receiver, they are not the idiomatic way to achieve that goal.
One should rather use the
AVAudioSessionCategoryOption
(what a mouthful)DefaultToSpeaker
. The documentation for that reads (emphasis mine):To use this, change your call to
setCategory:
to pass the option, like this:试试这个:
Try this: