菜鸟教程小白 发表于 2022-12-11 22:06:24

ios - 如何使用 AVFoundation 检测单个 Airpod 是否起飞?


                                            <p><p>正如标题所说,我需要检测用户在播放音乐时是否摘下 Airpods。由于某些原因,我必须使用 AVAudioplayer 而不是 MPMusicPlayer 来播放音乐,并且 AVAudioSession 只能在所有 airpods 都被取下时才能接收暂停事件。</p>

<p>有没有什么方法可以用AVFoundation检测单个Airpod被取下来?还是苹果没有在 AVFoundation 中实现这个?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>最后我通过使用 <code>MPRemoteCommandCenter</code> 来接收暂停事件来解决这个问题。暂停事件可能由起飞的一个或多个 Airpods 触发。收到事件后,我可以在处理程序 block 中暂停 AVAudioplayer 播放的音乐。 </p>

<pre><code>[ beginReceivingRemoteControlEvents];
[.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {

    // Paused by one or more Airpods taken off !
    return MPRemoteCommandHandlerStatusSuccess;
}];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用 AVFoundation 检测单个 Airpod 是否起飞?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54399572/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54399572/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用 AVFoundation 检测单个 Airpod 是否起飞?