我正在尝试构建音乐应用。音乐播放完美。现在我想设置现在在我的应用程序中播放信息,将显示音乐控制中心。
那么如何在下面的代码中设置它?
class MusicPlayer: NSObject {
static let player = MusicPlayer()
var player : AVPlayer?
var playerItem:AVPlayerItem?
func initPlayer(){
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
print("AVAudioSession is Active")
} catch let error as NSError {
print(error.localizedDescription)
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
func playMusic(_ musicUrl: String?) {
if let mm = musicUrl {
var url: URL!
url = URL(string: mm)
playerItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem)
player?.volume = 1.0
player?.rate = 1.0
player?.play()
}
}
}
使用此课程
MusicPlayer.player.playMusic(totalAlbumList[myIndex].audiofile)