How Do I Get Audio Controls on Lock Screen/Control Center from AVAudioPlayer in Swift
You need to invoke beginReceivingRemoteControlEvents() otherwise it will not work on the actual device.
Swift 3.1
UIApplication.shared.beginReceivingRemoteControlEvents()
If you would like to specify custom actions for the MPRemoteCommandCenter:
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.nextTrackCommand.isEnabled = true
commandCenter.nextTrackCommand.addTarget(self, action:#selector(nextTrackCommandSelector))
edit/update:
Apple has a sample project showing how to Becoming a Now Playable App
AudioPlayer and lockscreen/control center control Swift
You need to use the MPRemoteCommandCenter
to do this. For example in your viewControllers viewDidLoad()
you can add this:
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
//Update your button here for the pause command
return .success
}
commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
//Update your button here for the play command
return .success
}
}
Just change the comments I have included to update your buttons UI. You will also need to import MediaPlayer
and MediaPlayer.framework
if you have not done so already.
Is there a way to show lock screen controls using AVAudioEngine and AVAudioPlayerNode?
So here is the solution to my problem, I was starting my AVAudioEngine together with its setup function called from viewDidLoad()
, that was the issue, and i used .play()
/.pause()
methods on my AVAudioPlayerNode to manipulate the audio, however AVAudioPlayerNode does not emit master audio, outputNode of AVAudioEngine does.
So whenever you want to play/pause audio from inside your app or from command center, if you are using AVAudioEngine to handle audio in you application, don’t forget to call .stop()
/.start()
methods on your AVAudioEngine. Lock screen controls should show up and play/pause buttons should update properly in command center/lock screen even without a single property set to MPNowPlayingInfoCenter.default().nowPlayingInfo
.
MPRemoteCommandCenter setup:
func setupRemoteControl() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
try? self.audioEngine.start()
return .success
}
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.audioEngine.stop()
return .success
}
}
How to store current audio information in bottom panel (AVAudioPlayer, Swift)
Possible Duplicate, Check this answer.
In case the link breaks, it seems you need to enable remote control events with:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
This should enable the play/pause buttons to send controls to your application. You'll need to set the now playing information manually...
// Swift code, will need converting
var mpic = MPNowPlayingInfoCenter.defaultCenter()
mpic.nowPlayingInfo = [
MPMediaItemPropertyTitle:"This Is a Test",
MPMediaItemPropertyArtist:"Matt Neuburg"
]
You will also need to act on the control events received:
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self playOrStop: nil];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self previousTrack: nil];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self nextTrack: nil];
break;
default:
break;
}
}
}
Audio playback lock screen controls not displaying on iPhone
The issue turned out to be this line...
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)
Once I changed it to
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
everything worked fine. So it seems that passing in any argument for AVAudioSessionCategoryPlaybackOptions causes the lock screen controls to not display. I also tried passing in .mixWithOthers
an that too caused the lock screen controls to not be displayed
Related Topics
Where Is the Official Documentation for Cvopenglestexture Method Types
Change the Alpha Value of the Navigation Bar
Is the Current Location/Compass Heading Button Available in the iOS Sdk
Using Generic In-App Purchase Items for a Dynamic Range of Digital Products
Property Cannot Be Found in Forward Class Object
Ios11 Causing Cors Issues in All Mobile Browsers
Apple Watch, Watchkit Extension and Main Application
How to Set Mdm Payload's "Identity" in Ipcu
How to Install the "App Store" in an iOS Simulator
How to Dynamically Format a Number to Have Commas in a Uitextfield Entry
How to Determine If the Sim/Phone Number Has Changed
Detect Uiimageview Touch in Swift
Not Receiving Any Push Notification in Iphone
Override a Method in Objective C via Category
Get Current View Controller from the App Delegate (Modal Is Possible)