ios - AVAudioEngine 和 AVAudioPlayerNode:播放/暂停未在 iOS 命令中心正确更新
问题描述
我试过了MPNowPlayingInfoPropertyElapsedPlaybackTime
,MPNowPlayingInfoPropertyDefaultPlaybackRate
和MPNowPlayingInfoPropertyPlaybackRate
。
我已经更新了苹果演示
苹果演示可以在 iOS 命令中心显示播放/暂停更新,与AVPlayer
这里使用AVAudioEngine
and AVAudioPlayerNode
,AVAudioPlayerNode
播放音频 scheduleBuffer
。
这是相关的iOS命令中心代码:
func show(mediaInfo time: TimeInterval){
let artistName = "test"
guard let duration = length else {
return
}
var isPlaying: Double = 0
var pInfo: [String: Any] = [MPMediaItemPropertyArtist : artistName,
MPMediaItemPropertyTitle : artistName ]
if let img = UIImage(systemName: "music.note.list"){
let artwork = MPMediaItemArtwork(boundsSize: img.size, requestHandler: { (_) -> UIImage in
return img
})
pInfo[MPMediaItemPropertyArtwork] = artwork
}
if streamer.state == .playing{
isPlaying = 1
pInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = time
pInfo[MPMediaItemPropertyPlaybackDuration] = duration
}
pInfo[MPMediaItemPropertyPersistentID] = artistName
pInfo[MPNowPlayingInfoPropertyPlaybackRate] = NSNumber(floatLiteral: isPlaying)
pInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = NSNumber(floatLiteral: isPlaying)
pInfo[MPNowPlayingInfoPropertyMediaType] = NSNumber(value: MPNowPlayingInfoMediaType.audio.rawValue)
MPNowPlayingInfoCenter.default().nowPlayingInfo = pInfo
}
这是示例代码
对应的音频会话:
func setupAudioSession() {
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(.playback, mode: .default, policy: .default, options: [.allowBluetoothA2DP,.defaultToSpeaker])
try session.setActive(true)
} catch {
os_log("Failed to activate audio session: %@", log: ViewController.logger, type: .default, #function, #line, error.localizedDescription)
}
}
也试过
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
audioSession.requestRecordPermission({ (isGranted: Bool) in
})
} catch {}
解决方案
您可以尝试停止 AVAudioEngine。像这样:
engine.stop()
推荐阅读
- javascript - 如何在 fullcalendar 中添加新列
- javascript - 使用 Flask 中的外部 javascript 文件在 Google Charts 函数中传递参数
- python - 如何计算二项式和负二项式分布置信区间
- rust - 分支迭代器而不深度复制其元素
- typescript - Redux 工具包 + Typescript:extraReducers
- java - Android密码哈希
- npm - i18n-js 基于语言环境返回翻译
- java - 扫描所有的 Aerospike Java 客户端失败
- javascript - React + Tailwind 组件库导入问题
- snakemake - 如何避免 Snakemake 规则使用来自其他规则的不完整输出文件