1

我正在尝试构建一个音乐应用程序。音乐播放完美。现在我想在我的应用程序中设置现在播放信息,将显示音乐控制中心。

那么如何将其设置到我的以下代码中?

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)
4

0 回答 0