Skip to content

Commit

Permalink
apply PlayMode to PlayListPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
nerd0geek1 committed Aug 4, 2016
1 parent 5464c97 commit 30666b3
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions PlayListPlayer/classes/player/PlayListPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ import Foundation
import AVFoundation

public class PlayListPlayer: PlayListPlayerType {

//MARK: - public properties

public static let sharedInstance: PlayListPlayer = PlayListPlayer()

public var didFinishPlayingTrack:(() -> Void)?
public var didFinishPlayingPlayList:(() -> Void)?

public var playMode: PlayMode = .RepeatPlayList

//MARK: - private properties

private let player: AVPlayer = AVPlayer()

private(set) var playListURLs: [NSURL] = []
private(set) var currentIndex: Int = 0


//MARK: - update PlayListPlayer properties

public func setPlayList(urls: [NSURL]) {
Expand Down Expand Up @@ -84,16 +92,21 @@ public class PlayListPlayer: PlayListPlayerType {
}

public func skipToNextTrack() {
let nextIndex: Int = currentIndex + 1
let isLastTrack: Bool = currentIndex == lastTrackIndex()
let nextIndex: Int = isLastTrack ? 0 : currentIndex + 1

if isValidIndex(nextIndex) {
switch playMode {
case .RepeatPlayList:
setCurrentIndex(nextIndex)
player.play()
return
play()
case .RepeatItem:
seekToTrackBeginning()
case .NoRepeat:
setCurrentIndex(nextIndex)
if isLastTrack {
pause()
}
}

player.pause()
seekToTrackBeginning()
}

public func beginRewinding() {
Expand All @@ -105,24 +118,24 @@ public class PlayListPlayer: PlayListPlayerType {
}

public func jumpToPreviousTrack() {
let previousIndex: Int = currentIndex - 1

if isValidIndex(previousIndex) {
setCurrentIndex(previousIndex)
play()
return
let isFirstTrack: Bool = currentIndex == 0
let previousIndex: Int = isFirstTrack ? 0 : currentIndex - 1

switch playMode {
case .RepeatPlayList, .NoRepeat:
if isFirstTrack {
seekToTrackBeginning()
} else {
setCurrentIndex(previousIndex)
}
case .RepeatItem:
seekToTrackBeginning()
}

seekToTrackBeginning()
play()
}

//MARK: - private

private func setupPlayerItem() {
if !hasPlayList() {
return
}
if !isValidIndex(currentIndex) {
return
}
Expand All @@ -143,7 +156,11 @@ public class PlayListPlayer: PlayListPlayerType {
}

private func isValidIndex(index: Int) -> Bool {
return 0 <= index && index <= playListURLs.count - 1
return hasPlayList() && 0 <= index && index <= lastTrackIndex()
}

private func lastTrackIndex() -> Int {
return playListURLs.count == 0 ? 0 : playListURLs.count - 1
}

private func resetPlayerRate() {
Expand All @@ -160,8 +177,10 @@ public class PlayListPlayer: PlayListPlayerType {
private func playerDidFinishTrackPlaying(notification: NSNotification) {
didFinishPlayingTrack?()

if currentIndex == playListURLs.count - 1 {
if currentIndex == lastTrackIndex() {
didFinishPlayingPlayList?()
}

skipToNextTrack()
}
}

0 comments on commit 30666b3

Please sign in to comment.