gpt4 book ai didi

ios - 在iOS锁定屏幕上将播放器控件用于远程控制应用程序

转载 作者:行者123 更新时间:2023-12-01 21:59:12 25 4
gpt4 key购买 nike

我正在尝试在用于控制PC上音乐播放器的远程控制应用程序的iOS锁定屏幕上实现播放器控件。
因此,基本上我需要在iOS设备之外控制播放。
我已在功能(音频)中启用了背景模式,并试图设置MPRemoteCommandCenter锁定屏幕控件,但未显示它们。这是代码:

class RemoteCommandHandler: NSObject {
static let shared = RemoteCommandHandler()

private let player = AVPlayer()
private let audioSession = AVAudioSession.sharedInstance()
private let commandCenter = MPRemoteCommandCenter.shared()

func initRemote() {
//UIApplication.shared.beginReceivingRemoteControlEvents()

do {
if #available(iOS 10.0, *) {
try audioSession.setCategory(.playback, mode: .default, options: [])
} else {
// Fallback on earlier versions
try audioSession.setCategory(.playback)
}
} catch {
NSLog("\(error)")
}

do {
try audioSession.setActive(true)
NSLog("AVSession is active")
} catch {
NSLog("\(error)")
}

setupRemoteTransportControls()
setupNowPlaying()
}

func setupRemoteTransportControls() {
commandCenter.togglePlayPauseCommand.isEnabled = true
commandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(controlPlayPause))
}

func setupNowPlaying() {
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = NowPlayingInfo.getValue(.Title)

if let image = UIImage(named: "DemoArtwork") {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: image)
}
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = TimeInterval(exactly: 10)
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = NowPlayingInfo.getDurationMs() / 1000
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1.0

MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
}

甚至有可能做我想做的事吗?我做错了什么?

最佳答案

锁定屏幕上的控件似乎唯一可行的方法是启动应用中真实音频文件的播放。
我在应用程序启动时做了一个非常短的静默文件播放的解决方法,现在我可以访问这些控件。

请记住,应启用背景模式(音频),以便在您的应用程序不在屏幕上时查看控件。

另一个限制是,音量控制是硬连线到设备音量的,因此您不能单独更改应用程序的音量。

以下是启用控件的最少代码:

import MediaPlayer
import AVFoundation

class RemoteCommandHandler: NSObject {
static let shared = RemoteCommandHandler()

private var player: AVPlayer!
private var playerItem: AVPlayerItem!
private let audioSession = AVAudioSession.sharedInstance()
private let commandCenter = MPRemoteCommandCenter.shared()

func initRemote() {
let sound = Bundle.main.path(forResource: "SilenceAAC", ofType: "m4a")
playerItem = AVPlayerItem(url: URL(fileURLWithPath: sound!))
player = AVPlayer(playerItem: playerItem)

do {
if #available(iOS 10.0, *) {
try audioSession.setCategory(.playback, mode: .default, options: [])
} else {
// Fallback on earlier versions
try audioSession.setCategory(.playback)
}

try audioSession.setActive(true)
NSLog("AVSession is active - \(audioSession)")
} catch {
NSLog("\(error)")
}

setupNowPlaying()
setupRemoteTransportControls()

player.play()
}

func setupRemoteTransportControls() {
commandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(controlPlayPause))
}

func setupNowPlaying() {
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = NowPlayingInfo.getValue(.Title)

MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
}

关于ios - 在iOS锁定屏幕上将播放器控件用于远程控制应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60568375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com