Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 12 Lock screen controls for music app

Tags:

ios

swift

ios12

In iOS 11 my music app would show the lock screen controls when I locked my iPhone. I was able to see the currently playing song and play/pause skip forward and backwards. However in upgrading to Xcode 10/iOS 12 I can no longer see the lock screen controls just the date and time...

However, if I swipe up and get that widget screen (where you can turn on airplane mode etc) I CAN see the now playing info.

Here is what I have

In the Background Modes

Modes

I have updated my code to the following:

Called in my viewDidLoad

do {
  try AVAudioSession.sharedInstance().setCategory(.soloAmbient, mode: .default, options: .allowAirPlay)
print("Playback OK")
  try AVAudioSession.sharedInstance().setActive(true)
  print("Session is Active")
} catch {
  print(error)
}


UIApplication.shared.beginReceivingRemoteControlEvents()
self.becomeFirstResponder()

I did not previously have the following code in the last working version but I added it because I found similar posts suggesting I do it

if let songInfo = self.mediaPlayer.nowPlayingItem {
  nowPlayingInfoCenter.nowPlayingInfo = [
    MPMediaItemPropertyTitle: songInfo.title ?? "",
    MPMediaItemPropertyArtist: songInfo.artist ?? "",
    MPMediaItemPropertyArtwork : songInfo.artwork?.image(at: CGSize(width: 400, height: 400)) ?? #imageLiteral(resourceName: "emptyArtworkImage")]
}

I put breakpoints on the do try it does not print either of the print functions and skips the try

Did I convert my code wrong?

like image 318
RubberDucky4444 Avatar asked Sep 21 '18 21:09

RubberDucky4444


People also ask

How do I control Apple music from my lock screen?

At your home screen, click the gear icon (settings) to visit device settings. Then what you have to do is open the notifications tab. You have to find and click on Music and slide the notification toggle to disable it. Restart your iPhone after doing so and the music app lock screen problem should be fixed easily.


1 Answers

Don't forget setting up the MPRemoteCommandCenter:

import MediaPlayer

//Use an AVPlayer
var player: AVPlayer!
var playerItem: AVPlayerItem!

You may setup the AVPlayer in viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    let path = Bundle.main.path(forResource: "My Heart Will Go On", ofType:"mp3")!
    let url = URL(fileURLWithPath: path)
    playerItem = AVPlayerItem(url: url)
    player = AVPlayer(playerItem: playerItem)
    setupAudioSession()
}

Set the audio session like so:

func setupAudioSession() {
    do {
        try AVAudioSession.sharedInstance().setCategory(.soloAmbient, mode: .default, options: .allowAirPlay)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print("Error setting the AVAudioSession:", error.localizedDescription)
    }
}

Play the audio file

func play() {
    player.play()
    setupNowPlaying()
    setupRemoteCommandCenter()
}

Which sets up the MPNowPlayingInfoCenter (customize this to your code):

func setupNowPlaying() {
    // Define Now Playing Info
    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = "My Song"

    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playerItem.currentTime().seconds
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = playerItem.asset.duration.seconds
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate

    // Set the metadata
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
    MPNowPlayingInfoCenter.default().playbackState = .playing
}

And the MPRemoteCommandCenter:

func setupRemoteCommandCenter() {
    let commandCenter = MPRemoteCommandCenter.shared();
    commandCenter.playCommand.isEnabled = true
    commandCenter.playCommand.addTarget {event in
        self.player.play()
        return .success
    }
    commandCenter.pauseCommand.isEnabled = true
    commandCenter.pauseCommand.addTarget {event in
        self.player.pause()
        return .success
    }
}
like image 50
ielyamani Avatar answered Oct 16 '22 08:10

ielyamani