简体   繁体   English

iOS 12锁定屏幕控制音乐应用程序

[英]iOS 12 Lock screen controls for music app

In iOS 11 my music app would show the lock screen controls when I locked my iPhone. 在iOS 11中,当我锁定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... 但是在升级到Xcode 10 / iOS 12时,我再也看不到锁定屏幕只控制日期和时间......

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 在背景模式中

模式

I have updated my code to the following: 我已将代码更新为以下内容:

Called in my viewDidLoad 在我的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 我把断点放在do try它不打印任何打印功能并跳过try

Did I convert my code wrong? 我的代码转换错了吗?

Don't forget setting up the MPRemoteCommandCenter : 不要忘记设置MPRemoteCommandCenter

import MediaPlayer

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

You may setup the AVPlayer in viewDidLoad 您可以在viewDidLoad设置AVPlayer

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): 设置MPNowPlayingInfoCenter (根据您的代码自定义):

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 : 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM