问题描述
我想通过 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 (将#修改为@)