使用 M1 在 macOS 上运行 iOS 应用程序时,MPNowPlayingInfoCenter 不会设置播放器控件

问题描述

我想通过 M1 在 Mac 上使用 iOS 应用程序。它主要在“专为 iPad 设计”模式下按预期工作,除了播放器控件不会像在 iOS 上那样出现在控制中心。 以下是在 iOS 上完美运行的代码的重要部分:

import AVFoundation
import MediaPlayer

class RemoteCommandHandler: NSObject {
    static let shared = RemoteCommandHandler()
    
    weak var delegate: MainVCDelegate?
    
    private var player: AVAudioPlayer?
    private let audioSession = AVAudioSession.sharedInstance()
    private let commandCenter = MPRemoteCommandCenter.shared()
    private var NowPlayingInfo = [String: Any]()
    
    private var obs: NSkeyvalueObservation?

    func initPlayback() {
        if obs == nil {       
            do {
                try audioSession.setCategory(.playback,mode: .default,options: [])
                
                try audioSession.setActive(true)
            } catch {
                //error
            }
            
            obs = audioSession.observe( \.outputVolume ) { avSession,_ in
                self.delegate?.setVolume(avSession.outputVolume)
            }
            
            setupRemoteTransportControls()

            if player == nil {
            do {
                try player = AVAudioPlayer(contentsOf:
                    URL(fileURLWithPath: Bundle.main.path(forResource: "TestTrack",ofType: "m4a")!))
                player?.delegate = self
                player?.volume = 0
                player?.numberOfLoops = 1
            } catch let error as NSError {
                //error
            }
        }
        }
    }
    
    func setupNowPlaying() {
            NowPlayingInfo[MPMediaItemPropertyTitle] = TrackInfo.title.userFriendlyString()
            NowPlayingInfo[MPMediaItemPropertyArtist] = TrackInfo.artist.userFriendlyString()
            NowPlayingInfo[MPMediaItemPropertyAlbumTitle] = TrackInfo.album.userFriendlyString()
            
            let image = NowPlayingInfo.getArtwork()
            var artwork: MPMediaItemArtwork
            artwork = MPMediaItemArtwork(boundsSize: image.size) { _ in image }
            NowPlayingInfo[MPMediaItemPropertyArtwork] = artwork

            NowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = PlayerStatusInfo.isPlaying() ? 1 : 0
            NowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = NowPlayingInfo.getDurationMs() / 1000
                            NowPlayingInfo[MPNowPlayingInfoPropertyElapsedplaybackTime] = NowPlayingInfo.currentMs / 1000
            NowPlayingInfo[MPNowPlayingInfoPropertyPlaybackProgress] = NowPlayingInfo.calculateProgress()
            
            MPNowPlayingInfoCenter.default().NowPlayingInfo = NowPlayingInfo
    }
}

非常感谢有关如何使其在 Mac 上运行的任何建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...