MPNowPlayingInfoCenter 未显示,但后台播放正常
MPNowPlayingInfoCenter is not showing but background playing does work
我制作了 MPNowPlayingInfoCenter,但它不起作用。后台播放正常,但 MPNowPlayingCenter 没有出现。我就是这样做的。
import UIKit
import AVFoundation
import MediaPlayer
class AudioPlayViewController: UIViewController, AVAudioPlayerDelegate {
var player: AVAudioPlayer!
var updater: CADisplayLink! = nil
@IBOutlet weak var playButton: UIButton!
var url_2 = URL(string: "")
var selectedFileName:String = ""
override func viewDidLoad() {
super.viewDidLoad()
let path = selectedFileName
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let url = documentsURL.appendingPathComponent(path)
url_2 = url
do {
player = try AVAudioPlayer(contentsOf: url)
updater = CADisplayLink(target: self, selector: #selector(self.trackAudio))
updater.preferredFramesPerSecond = 1
updater.add(to: RunLoop.current, forMode: RunLoop.Mode.common)
player.prepareToPlay()
player.delegate = self
catch {
print(error)
}
let session = AVAudioSession.sharedInstance()
do{
try session.setCategory(AVAudioSession.Category.playback)
}
catch{
}
}
func play(){
player.play()
MPNowPlayingInfoCenter.default().nowPlayingInfo = [
MPMediaItemPropertyTitle: selectedFileName,
MPMediaItemPropertyArtist: "Ariana",
MPMediaItemPropertyLyrics: "test"
]
}
}
我在 Targets > Signing&Capabilities 中设置了背景模式 > 音频、Airplay 和画中画。
我通过添加
解决了它
func setupNowPlayingInfoCenter(){
UIApplication.shared.beginReceivingRemoteControlEvents()
MPRemoteCommandCenter.shared().playCommand.addTarget {event in
self.play()
return .success
}
MPRemoteCommandCenter.shared().pauseCommand.addTarget {event in
self.pause()
return .success
}
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget {event in
self.goForward()
return .success
}
MPRemoteCommandCenter.shared().previousTrackCommand.addTarget {event in
self.goBackward()
return .success
}
}
我制作了 MPNowPlayingInfoCenter,但它不起作用。后台播放正常,但 MPNowPlayingCenter 没有出现。我就是这样做的。
import UIKit
import AVFoundation
import MediaPlayer
class AudioPlayViewController: UIViewController, AVAudioPlayerDelegate {
var player: AVAudioPlayer!
var updater: CADisplayLink! = nil
@IBOutlet weak var playButton: UIButton!
var url_2 = URL(string: "")
var selectedFileName:String = ""
override func viewDidLoad() {
super.viewDidLoad()
let path = selectedFileName
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let url = documentsURL.appendingPathComponent(path)
url_2 = url
do {
player = try AVAudioPlayer(contentsOf: url)
updater = CADisplayLink(target: self, selector: #selector(self.trackAudio))
updater.preferredFramesPerSecond = 1
updater.add(to: RunLoop.current, forMode: RunLoop.Mode.common)
player.prepareToPlay()
player.delegate = self
catch {
print(error)
}
let session = AVAudioSession.sharedInstance()
do{
try session.setCategory(AVAudioSession.Category.playback)
}
catch{
}
}
func play(){
player.play()
MPNowPlayingInfoCenter.default().nowPlayingInfo = [
MPMediaItemPropertyTitle: selectedFileName,
MPMediaItemPropertyArtist: "Ariana",
MPMediaItemPropertyLyrics: "test"
]
}
}
我在 Targets > Signing&Capabilities 中设置了背景模式 > 音频、Airplay 和画中画。
我通过添加
解决了它func setupNowPlayingInfoCenter(){
UIApplication.shared.beginReceivingRemoteControlEvents()
MPRemoteCommandCenter.shared().playCommand.addTarget {event in
self.play()
return .success
}
MPRemoteCommandCenter.shared().pauseCommand.addTarget {event in
self.pause()
return .success
}
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget {event in
self.goForward()
return .success
}
MPRemoteCommandCenter.shared().previousTrackCommand.addTarget {event in
self.goBackward()
return .success
}
}