我有一个播放音频的应用程序 .
我试图让所有“优势案例”在应用发布之前得到解决..无论多么轻微 .
您可能知道......播放音频的应用需要告诉IOS系统他们打算如何表现 . 他们的音频会中断其他应用吗?他们与其他应用程序混合?等等
因此,应用程序需要正确设置其AVAudioSession代码...我根据Apple的文档做了:
import UIKit
import AVFoundation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
} catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
do {
try audioSession.setActive(true)
} catch {
print("Setting AudioSession active failed.")
}
// Other project setup
return true
}
}
想象一下以下测试:
1)打开蓝牙并连接蓝牙扬声器 .
2)加载一个知名的应用程序,它在后台运行时播放音频,如Spotify . 播放歌曲/音频和背景 .
3)当APP B的音频仍在播放时,加载APP A(我的应用程序)并按下三个按钮中的任意一个 .
所以......执行第3步后,会发生什么?
那么,这取决于你使用的是什么......例如:
-
如果您只是使用手机扬声器(不是外接蓝牙扬声器),一切正常,正如预期的那样 .
-
如果您正在使用ION Tailgater iPA77蓝牙音箱,一切正常 .
-
BUT, if you are using, for example, an MTX MUDHSB-B Bluetooth speaker, then NOTHING HAPPENS when you press any of the three buttons... until you continue waiting and/or pressing buttons for a seemingly arbitrary amount of time (about 30 seconds)... then the sound will start to come through and behave normally.
一个蓝牙扬声器与另一个蓝牙扬声器的行为有何不同(大概是AVAudioSession)?蓝牙是蓝牙,对吗?我猜不会 . 任何人都可以阐明这一点吗?
谢谢 .
2 回答
听起来像扬声器的某个问题......你是否尝试过与2个不属于你的应用相同的场景?例如Spotify和Apple Music?
你需要清除
AVAudioSessionCategoryOption
到AVAudioSessionCategoryOptionMixWithOthers
如果你查看documentation,在底部你会看到:
根据您的代码,另一个选项是您在设置类别后没有激活音频会话: