问题描述
此环境为iOS 13.6和Swift5。我有一个非常简单的应用程序,可以成功在前景或背景中播放MP3文件。我添加了MPRemoteCommandCenter播放和暂停命令处理程序。我在前台播放声音文件,然后暂停它。
当我从锁定屏幕上点击“播放”按钮时,我的代码将调用audioPlayer.play(),该返回true。我听到声音再次开始播放,但是播放器的currentTime不会前进。之后,锁定屏幕上的播放和暂停按钮什么也不做。当我再次显示应用程序前景时,播放按钮将从我进入锁定屏幕之前的位置播放。
这是我的AudioPlayer类:
import AVFoundation
import MediaPlayer
class AudioPlayer: RemoteAudioCommandDelegate {
var audioPlayer = AVAudioPlayer()
let remoteCommandHandler = RemoteAudioCommandHandler()
var timer:Timer!
func play(title: String) {
let path = Bundle.main.path(forResource: title,ofType: "mp3")!
let url = URL(fileURLWithPath: path)
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOf: url)
remoteCommandHandler.delegate = self
remoteCommandHandler.enableDisableRemoteCommands(true)
timer = Timer.scheduledTimer(timeInterval: 1.0,target: self,selector: #selector(updateNowPlayingInfo),userInfo: nil,repeats: true)
} catch let error as NSError {
print("error = \(error)")
}
}
func play() {
print ("audioPlayer.play() returned \(audioPlayer.play())")
}
func pause() {
audioPlayer.pause()
}
func stop() {
audioPlayer.stop()
}
func currentTime() -> TimeInterval {
return audioPlayer.currentTime
}
func setCurrentTime(_ time:TimeInterval) {
audioPlayer.currentTime = time
}
@objc func updateNowPlayingInfo() {
// Hard-code the nowPlayingInfo since this is a simple test app
var nowPlayingDict =
[MPMediaItemPropertyTitle: "Tin Man",MPMediaItemPropertyAlbumTitle: "The Complete Greatest Hits",MPMediaItemPropertyAlbumTrackNumber: NSNumber(value: UInt(10) as UInt),MPMediaItemPropertyArtist: "America",MPMediaItemPropertyPlaybackDuration: 208,MPNowPlayingInfoPropertyPlaybackRate: NSNumber(value: 1.0 as Float)] as [String : Any]
nowPlayingDict[MPNowPlayingInfoPropertyElapsedPlaybackTime] = NSNumber(value: audioPlayer.currentTime as Double)
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingDict
}
}
这是我的RemoteCommandHandler类:
import Foundation
import MediaPlayer
protocol RemoteAudioCommandDelegate: class {
func play()
func pause()
}
class RemoteAudioCommandHandler: NSObject {
weak var delegate: RemoteAudioCommandDelegate?
var remoteCommandCenter = MPRemoteCommandCenter.shared()
var playTarget: Any? = nil
var pauseTarget: Any? = nil
func enableDisableRemoteCommands(_ enabled: Bool) {
print("Called with enabled = \(enabled)")
remoteCommandCenter.playCommand.isEnabled = enabled
remoteCommandCenter.pauseCommand.isEnabled = enabled
if enabled {
addRemoteCommandHandlers()
} else {
removeRemoteCommandHandlers()
}
}
fileprivate func addRemoteCommandHandlers() {
print( "Entered")
if playTarget == nil {
print( "Adding playTarget")
playTarget = remoteCommandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
print("addRemoteCommandHandlers calling delegate play")
self.delegate?.play()
return .success
}
}
if pauseTarget == nil {
print( "Adding pauseTarget")
pauseTarget = remoteCommandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
print("addRemoteCommandHandlers calling delegate pause")
self.delegate?.pause()
return .success
}
}
}
fileprivate func removeRemoteCommandHandlers() {
print( "Entered")
if playTarget != nil {
print( "Removing playTarget")
remoteCommandCenter.playCommand.removeTarget(playTarget)
playTarget = nil
}
if pauseTarget != nil {
print( "Removing pauseTarget")
remoteCommandCenter.pauseCommand.removeTarget(pauseTarget)
pauseTarget = nil
}
}
}
我很乐意提供进一步的必要信息,因为我对为什么这种相对简单的代码(在我看来)不起作用感到困惑。
非常感谢您的帮助!
解决方法
经过更多调试后,我发现AVAudioPlayer开始从锁定屏幕播放声音,但不久后又停止了播放。
我通过添加计时器来缓解此问题。计时器检查用户的最后命令是否正在播放,但声音没有播放。当用户选择暂停或歌曲自然停止播放时,我也会更改状态。
对于这个问题的实际解决方法,我仍然不知所措。