3

I want to display video title in control center of iPhone. But, the title does not appear. What should I do? Xcode 14.0, Swift 5.7, iPhone8 iOS 16.0

import SwiftUI
import AVKit
import MediaPlayer

struct ContentView: View {
  var body: some View {
    let playerClass = PlayerClass()
    VideoPlayer(player: playerClass.aVPlayer)
      .onAppear {
        playerClass.aVPlayer.play()
      }
  }
}

class PlayerClass {
  var aVPlayer: AVPlayer
  init() {
    let fileUrl = Bundle.main.url(forResource: "sample",
                   withExtension: "mp4")!
    aVPlayer = AVPlayer(url: fileUrl)
    setNowPlayingInfo()
    setMPRemoteCommandCenter()
  }

  func setNowPlayingInfo() {
    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = "my title"
    nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = "my album"
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
  }
   
  func setMPRemoteCommandCenter() {
    let commandCenter = MPRemoteCommandCenter.shared()
    commandCenter.playCommand.addTarget { [unowned self] event in
      print("play")
      return .success
    }
    commandCenter.pauseCommand.addTarget { [unowned self] event in
      print("pause")
      return .success
    }
  }
}

enter image description here

I expect the video title appears in control center.enter image description here

0 Answers0