Skip to content
This repository has been archived by the owner on Mar 16, 2020. It is now read-only.

Commit

Permalink
Add simple loop
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeplum committed Dec 6, 2015
1 parent fec6c12 commit e79f9fb
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions rycooder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ class RyCooder {
queue: NSOperationQueue.mainQueue()) { _ in
self.handleInput()
}

NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification,
object: nil,
queue: NSOperationQueue.mainQueue()) { notification in
guard let song = notification.object as? AVPlayerItem,
let index = self.songs.indexOf(song) else {
return
}

let nextIndex = index + 1
if nextIndex == self.songs.count {
self.handlePlayBackFinishing()
} else {
self.currentSongIndex = nextIndex
}
}
runLoop.run()
}

Expand All @@ -49,34 +65,44 @@ extension RyCooder {
if let inputNumber = Int(input) {
if inputNumber > 0 && inputNumber <= songs.count {
jumpToSongAtIndex(inputNumber - 1)
player.play()
}

return
} else {
switch input {
case "s", "start":
logStart()
player.play()
guard player.rate == 0 else {
return
}

jumpToSongAtIndex(0)

case "n", "next":
player.advanceToNextItem()
player.play()
var next = currentSongIndex + 1
if next == songs.count {
next = 0
}

currentSongIndex++
jumpToSongAtIndex(next)

case "p", "previous":
jumpToPreviousSong()
player.play()
var previous = currentSongIndex - 1
if previous < 0 {
previous = songs.count - 1
}

currentSongIndex--
jumpToSongAtIndex(previous)

default:
print("Unrecognized command \"\(input)\" 😦 .")
}
}
}

private func handlePlayBackFinishing() {
jumpToSongAtIndex(0)
}

private func jumpToSongAtIndex(index: Int) {
player.removeAllItems()

Expand All @@ -89,6 +115,8 @@ extension RyCooder {
player.advanceToNextItem()
}

player.play()

currentSongIndex = index
}

Expand Down

0 comments on commit e79f9fb

Please sign in to comment.