我有一个音乐播放器,它使用超动力作为音频播放器。我遵循这个文章来实现一个播放器小部件。但是,小部件有时会出现,但很多时候没有。,我希望播放器小部件在有音频播放时出现。
RemoteCommandManager.swift:(从文章中)
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.beginReceivingRemoteControlEvents()
// Override point for customization after application launch.
// Initializer the `RemoteCommandManager`.
remoteCommandManager = RemoteCommandManager()
// Always enable playback commands in MPRemoteCommandCenter.
remoteCommandManager.activatePlaybackCommands(true)
// Setup AVAudioSession to indicate to the system you how intend to play audio.
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault)
}
catch {
print("An error occured setting the audio session category: \(error)")
}
return true
}PlayerManager.swift
func play() {
let _ = try? AVAudioSession.sharedInstance().setActive(true)
superpowered.play()
}更新:
我注意到以下情况:
如果一个iTunes曲目正在播放(即播放器小部件存在),那么我使用我的应用程序播放一个音频文件,player小部件会发生变化,从而显示我的应用程序的信息。
我的猜测是,当我激活音频会话时,播放器部件从iTunes切换到我的应用程序。但是,当player小部件不存在时,激活音频会话将无法显示它。
发布于 2018-06-19 14:48:26
尝试将audioSession设置为AppDelegate中的active,就在返回函数func应用程序(_ application: UIApplication,didFinishLaunchingWithOptions.
// Set the AVAudioSession as active. This is required so that your application becomes the "Now Playing" app.
do {
try audioSession.setActive(true, with: [])
}
catch {
print("An Error occured activating the audio session: \(error)")
}看看这是否有帮助。
https://stackoverflow.com/questions/50789034
复制相似问题