Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
stefano fasce
Courses Plus Student 74 PointsMp3 Music Player - AVQueuePlayer - Play multiple items - Skip songs - Swift 2 Xcode 7
Hi! I'm building a mp3 player but I'm stuck. I was able to play a song, with play/pause stop button, show title track and timer, but to play multiple items I read I have to use AVQueuePlayer. There are some problems with using AVQueuePlayer, AVPlayer and AVAudioPlayer. Also I tried to skip songs or have a song start when one finishes but no success. Any help would be very appreciated! Thank you
import UIKit
import AVFoundation
class ViewController: UIViewController {
var timer:NSTimer!
var player: AVAudioPlayer = AVAudioPlayer()
var myplayer: AVPlayer = AVQueuePlayer()
@IBOutlet var playedTime: UILabel!
@IBOutlet var trackTitle: UILabel!
@IBOutlet var slider: UISlider!
@IBAction func play(sender: AnyObject) {
player.play()
timer = NSTimer.scheduledTimerWithTimeInterval(0.0, target: self, selector: "updateTime", userInfo: nil, repeats: true)
}
@IBAction func pause(sender: AnyObject) {
player.pause()
}
@IBAction func adjustVolume(sender: AnyObject) {
player.volume = slider.value
}
@IBAction func stopMusic(sender: AnyObject) {
player.stop()
player.currentTime = 0
}
func updateTime() {
let currentTime = Int(player.currentTime)
let minutes = currentTime/60
let seconds = currentTime - minutes * 60
playedTime.text = NSString(format: "%02d:%02d", minutes,seconds) as String
}
@IBAction func skip(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
trackTitle.text = "Warriors - Steve Curno"
let url3 = NSBundle.mainBundle().pathForResource("Sport9", ofType: "mp3")!
do {
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: url3))
} catch {
trackTitle.text = "Sport Emotive - Steve Curno"
let url2 = NSBundle.mainBundle().pathForResource("Sport5", ofType: "mp3")!
do {
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: url2))
} catch {
trackTitle.text = "Limitless - Steve Curno"
let url1 = NSBundle.mainBundle().pathForResource("Sport4Redux", ofType: "mp3")!
do {
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: url1))
} catch {
let item1 = AVPlayerItem(URL: NSURL(fileURLWithPath: url1))
let item2 = AVPlayerItem(URL: NSURL(fileURLWithPath: url2))
let item3 = AVPlayerItem(URL: NSURL(fileURLWithPath: url3))
let itemsToPlay = [item1, item2, item3]
_ = AVQueuePlayer(items: itemsToPlay)
func playplay(url: NSURL) {
let item = AVPlayerItem(URL: url)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item)
let player = AVPlayer(playerItem: item)
player.play()
}
func playerDidFinishPlaying(note: NSNotification) {
// Your code here
}
// Process error here
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "finishedPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: player)
}
}