简体   繁体   中英

iOS Swift avplayer previous or next button

I Have an Array with songobjects. I use avplayer to play those .mp3 files. How can I use the buttons to play the next song or the previous song? I can stop the song or going back and choose another one, but I cant manually switch the songs when playing.

func playSong(url: NSURL){

let playerController = AVPlayerViewController()

 playerController.player = AVPlayer(URL: url.absoluteURL)


NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MusicListViewController.playerDidFinishPlaying(_:)),
                                                 name: AVPlayerItemDidPlayToEndTimeNotification, object: playerController.player!.currentItem)

self.presentViewController(playerController, animated: true, completion: nil)

playerController.player?.play();   

}

Instead of AVPlayer you should use AVQueuePlayer .

Code would look like this, where you could add all of your AVPlayerItem objects:

let item1 = AVPlayerItem(url: URL(string: "1")!)
let item2 = AVPlayerItem(url: URL(string: "2")!)
let item3 = AVPlayerItem(url: URL(string: "3")!)

let queuePlayer = AVQueuePlayer(items: [item1, item2, item3])

After you've done this, just add the player to AVPlayerViewController as usual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM