通过扬声器的 AVAudioPlayer

发布于 2024-08-28 22:53:56 字数 982 浏览 15 评论 0原文

我得到以下代码:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

合约呢 2024-09-04 22:53:56

设置它的方式很挑剔...

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

使用 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...

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

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 an AVAudioPlayer 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

海夕 2024-09-04 22:53:56

对于 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

喜爱纠缠 2024-09-04 22:53:56

这里有很好的例子 http://ruckt.info/playing-sound-through-iphone-扬声器/ 解决了我的问题。

下面的方法 - configureAVAudioSession - 将音频重新路由到 iOS 7.0 中的主扬声器。为此,请在播放音频之前调用它:[self configureAVAudioSession]

- (void) configureAVAudioSession //To play through main iPhone Speakers
{
    //get your app's audioSession singleton object
    AVAudioSession* session = [AVAudioSession sharedInstance];

    //error handling
    BOOL success;
    NSError* error;

    //set the audioSession category.
    //Needs to be Record or PlayAndRecord to use audioRouteOverride:

    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

    if (!success)  
    {
        NSLog(@"AVAudioSession error setting category:%@",error);
    }
    //set the audioSession override
    success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
    if (!success)  
    {
        NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
    }
    //activate the audio session
    success = [session setActive:YES error:&error];
    if (!success) 
    {
        NSLog(@"AVAudioSession error activating: %@",error);
    }
    else 
    {
        NSLog(@"audioSession active");
    }
}

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].

- (void) configureAVAudioSession //To play through main iPhone Speakers
{
    //get your app's audioSession singleton object
    AVAudioSession* session = [AVAudioSession sharedInstance];

    //error handling
    BOOL success;
    NSError* error;

    //set the audioSession category.
    //Needs to be Record or PlayAndRecord to use audioRouteOverride:

    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

    if (!success)  
    {
        NSLog(@"AVAudioSession error setting category:%@",error);
    }
    //set the audioSession override
    success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
    if (!success)  
    {
        NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
    }
    //activate the audio session
    success = [session setActive:YES error:&error];
    if (!success) 
    {
        NSLog(@"AVAudioSession error activating: %@",error);
    }
    else 
    {
        NSLog(@"audioSession active");
    }
}
蝶舞 2024-09-04 22:53:56

我使用了 AudioToolbox 框架,这就是我初始化音频会话的原因,如下所示:

AudioSessionInitialize(NULL, NULL, NULL, NULL);

这是我用来配置音频会话的其余代码。我没有覆盖音频路线,我也认为这是没有必要的。

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
OSStatus err = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                                       sizeof(sessionCategory),
                                       &sessionCategory);
AudioSessionSetActive(TRUE);
if (err) {
    NSLog(@"AudioSessionSetProperty kAudioSessionProperty_AudioCategory failed: %d", err);
}

I used the AudioToolbox framework that's why I initialized my audio session as following:

AudioSessionInitialize(NULL, NULL, NULL, NULL);

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.

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
OSStatus err = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                                       sizeof(sessionCategory),
                                       &sessionCategory);
AudioSessionSetActive(TRUE);
if (err) {
    NSLog(@"AudioSessionSetProperty kAudioSessionProperty_AudioCategory failed: %d", err);
}
人生百味 2024-09-04 22:53:56

我只是想向这篇文章的其他读者澄清这一点。

我一直在寻找这个问题的答案,特别是针对 SpeakHere 示例。我抓住了这一小块代码,它成功了。

 UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

我希望这不是太离题。我只是认为这对于那些只需要这两个小声明的人来说是有好处的。

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.

 UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

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.

﹎☆浅夏丿初晴 2024-09-04 22:53:56

您用于播放声音的代码看起来是正确的,并且您没有理由必须使用 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?

别再吹冷风 2024-09-04 22:53:56

虽然其他答案确实提供了在扬声器而不是接收器上播放音频的预期效果,但它们并不是实现该目标的惯用方式。

人们应该使用AVAudioSessionCategoryOption(真是拗口)DefaultToSpeaker。其文档如下(强调我的):

使用此选项并且没有其他音频路由(例如耳机)可用时,会话音频将通过设备的内置扬声器播放不使用此选项时,并且没有可用或未选择其他音频输出时,音频将通过接收器播放(旨在放在耳边的扬声器)。请注意,只有 iPhone 设备才配备接收器;在 iPad 和 iPod touch 设备上,此选项无效。

要使用此功能,请将您的调用更改为 setCategory: 以传递选项,如下所示:

let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [.DefaultToSpeaker])

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):

When using this option and no other audio route (such as a headset) is available, session audio will play through the device’s built-in speaker. When not using this option, and no other audio output is available or selected, audio will play through the receiver (a speaker intended to be held to the ear). Note that only iPhone devices are equipped with a receiver; on iPad and iPod touch devices, this option has no effect.

To use this, change your call to setCategory: to pass the option, like this:

let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [.DefaultToSpeaker])
你的背包 2024-09-04 22:53:56

试试这个:

NSError *error;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];    
if(error) { 
    NSLog(@"STKAudioManager: AudioSession cannot use speakers");
}

Try this:

NSError *error;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];    
if(error) { 
    NSLog(@"STKAudioManager: AudioSession cannot use speakers");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文