Reputation: 499
I have a music player that uses Superpowered as an audio player. I followed this article to implement a player widget. However, the widget sometimes shows up and many times doesn't. I want the player widget to show up whenever there is audio playing.
RemoteCommandManager.swift: (From the article)
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()
}
UPDATE:
I have noticed the following scenario:
If an iTunes track is playing (i.e the player widget is present) then I play an audio file using my app, the player widget changes so that it shows info of my app.
My guess is that when I activate the audio session, the player widget switches from iTunes to my app. However, when the player widget is not present, activating the audio session fails to present it.
Upvotes: 0
Views: 626
Reputation: 2916
try setting up the audioSession to active in AppDelegate, just above the returning the function, func application(_ 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)")
}
and check if that helps.
Upvotes: 0