简体   繁体   English

当我的应用程序的音频开始播放时,如何关闭另一个应用程序的音频?

[英]How do I turn off another app's audio when my app's audio starts?

So, I need two things: 所以,我需要两件事:

  1. When my app starts, and another app's audio is already playing, I don't want to stop that other app's audio. 当我的应用程序启动并且另一个应用程序的音频已经在播放时,我不想停止其他应用程序的音频。

  2. When my app's audio starts playing (the user initiates this action) I want any other app's audio that may be currently playing to stop. 当我的应用程序的音频开始播放时(用户启动此操作),我希望停止当前正在播放的其他任何应用程序的音频。

I've already accomplished #1, but need some help figuring out #2. 我已经完成了#1的工作,但需要一些帮助来找出#2。

Here's the code I have in my App Delegate applicationDidFinishLaunchingWithOptions method that makes #1 work: 这是我在应用程序委托applicationDidFinishLaunchingWithOptions方法中拥有的代码,该代码使#1起作用:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
                                 withOptions:AVAudioSessionCategoryOptionMixWithOthers 
                                       error:nil];

So, the key was to set the options to 0 when I actually started playing the audio in my app with a category of AVAudioSessionCategoryPlayback. 因此,关键是当我实际上开始使用AVAudioSessionCategoryPlayback类别在应用中播放音频时,将选项设置为0。 If you just don't specify the options when you change the category, it seems to assume that you want to use the originally specified options. 如果只是在更改类别时未指定选项,则似乎假设您要使用最初指定的选项。 That's my guess anyway. 无论如何,这是我的猜测。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
                                 withOptions:0 
                                       error:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS上停止其他应用的背景音频 - Stop another app's background audio on iOS 当另一个应用程序开始/停止播放音频时,可以通知我的应用程序吗? - Can my app be notified when another application starts/stops playing audio? iOS-如何在我的应用启动时将另一个视图控制器设置为我的初始视图控制器? - iOS - How do I set another view controller to be my initial view controller when my app starts? 在Swift中,当我的应用程序打开时,如何让其他应用程序继续播放音频? - In Swift, how do I let other apps continue to play audio when my app is open? iPhone - 我的应用程序的音频播放器是什么? - iPhone - What's the right audio player for my app? 本地来电正在杀死我的VOIP应用程序的音频访问权限 - Native incoming call is killing my VOIP app's access to audio 当音频无法启动时,禁用应用程序的背景模式 - Disable app's background mode when audio works not activate iOS-应用的音频输出的音频电平计量 - iOS - Audio level metering of app's audio output 当其他音频开始在同一个应用程序中播放时,将AVPlayer静音 - Mute AVPlayer when other audio starts playing in the same app 当我使用我的应用程序时如何关闭其他应用程序的推送通知 - How can I turn off push notifications of other apps when Im using my app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM