菜鸟教程小白 发表于 2022-12-12 20:38:32

ios - 是否可以以编程方式将音频输出重定向到 iOS 中的手机扬声器或耳机


                                            <p><p>我正在开发一个应用程序,我想在其中将音频输出重定向到电话扬声器,但我不知道如何以编程方式执行此操作。</p>

<p>有什么想法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您无法通过您的应用打开/关闭扬声器。但是,您可以感觉到设备的静音开关。通过它您可以知道扬声器的当前状态。 <a href="https://stackoverflow.com/questions/287543/how-to-programmatically-sense-the-iphone-mute-switch" rel="noreferrer noopener nofollow">This Question</a>将引导您找到解决方案。</p>

<p>为了完整起见,我把答案贴在这里..</p>

<pre><code>// &#34;Ambient&#34; makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
    AudioSessionInterruptionListener    inInterruptionListener = NULL;
    OSStatus    error;
    if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
    {
      NSLog(@&#34;*** Error *** error in AudioSessionInitialize: %d.&#34;, error);
    }
    else
    {
      gAudioSessionInited = YES;
    }
}

SInt32ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &amp;ambient))
{
    NSLog(@&#34;*** Error *** could not set Session property to ambient.&#34;);
}
</code></pre>

<p><strong>更新:</strong>
插入耳机时,您需要覆盖 AudioRoute 以打开扬声器。使用以下代码。</p>

<pre><code>UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&amp;audioRouteOverride);
</code></pre>

<p>确保在 Xcode 中包含 AudioToolbox 库。我想它会解决你的问题..</p>

<p>如果需要更多信息,请告诉我..:)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 是否可以以编程方式将音频输出重定向到 iOS 中的手机扬声器或耳机,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22550744/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22550744/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 是否可以以编程方式将音频输出重定向到 iOS 中的手机扬声器或耳机