setup now playing info

488 views Asked by At

I'm trying to build a music app. Music is playing perfectly. Now I want to setup now playing info in my app that will show music control center.

So how can I setup this into my below code?

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()
        }
    }

}

Use of this class

 MusicPlayer.player.playMusic(totalAlbumList[myIndex].audiofile)
0

There are 0 answers