Forked from ankahathara/Next and Previous Implementation
Created
February 11, 2016 07:30
-
-
Save tobitech/d1df8cca70c2cfe15b23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import StreamingKit | |
private let singletonInstance = NineInchAudio() | |
class NineInchAudio { | |
class var manager: NineInchAudio { | |
return singletonInstance | |
} | |
var audioPlayer:STKAudioPlayer | |
var songsArray = [Song]() | |
var startIndex = 0 | |
var artistImage = "1" | |
var currentPlayingSongName = "" | |
init() { | |
self.audioPlayer = STKAudioPlayer() | |
} | |
func addSongsToPlay(songs:[Song]){ | |
self.songsArray.removeAll(keepCapacity: false) | |
self.songsArray += songs | |
println("\(self.songsArray)") | |
} | |
func damnPlayThisSong(url: String) { | |
var songUrl = url | |
println("\(self.songsArray)") | |
self.audioPlayer.play(songUrl) | |
} | |
func startPlaying(index: Int){ | |
self.startIndex = index | |
if self.audioPlayer.currentlyPlayingQueueItemId() != nil { | |
println("Currntly Playing Item : \(self.audioPlayer.currentlyPlayingQueueItemId())") | |
return | |
} | |
for var i = startIndex; i < songsArray.count; i++ { | |
self.audioPlayer.queue(self.songsArray[i].songTrack) | |
println("\(self.songsArray[i].songTrack)") | |
} | |
} | |
func startPlaying(){ | |
if self.audioPlayer.currentlyPlayingQueueItemId() != nil { | |
println("Currntly Playing Item : \(self.audioPlayer.currentlyPlayingQueueItemId())") | |
return | |
} | |
for var i = startIndex; i < songsArray.count; i++ { | |
self.audioPlayer.queue(self.songsArray[i].songTrack) | |
println("\(self.songsArray[i].songTrack)") | |
} | |
} | |
// Play Next Song and the rest | |
func startPlayingNextSong() { | |
startIndex = startIndex + 1 | |
for var i = startIndex; i < songsArray.count; i++ { | |
self.audioPlayer.queue(self.songsArray[i].songTrack) | |
} | |
} | |
// Play Previous Song and the rest | |
func startPlayingPreviousSong() { | |
if startIndex > 0 { | |
startIndex = startIndex - 1 | |
for var i = startIndex; i < songsArray.count; i++ { | |
self.audioPlayer.queue(self.songsArray[i].songTrack) | |
} | |
} | |
} | |
func stopPlaying() -> Bool { | |
self.audioPlayer.stop() | |
return true | |
} | |
func pausePlaying() { | |
self.audioPlayer.pause() | |
} | |
func resumePlaying() { | |
self.audioPlayer.resume() | |
} | |
// MARK: Helpers | |
/* | |
* Format the float time values like duration | |
* to format with minutes and seconds | |
*/ | |
func timeFormat(value: Float) -> String { | |
var minutes = Float(floor(Float(lroundf(value)/60))) | |
var seconds = Float(lroundf(value)) - (minutes * 60) | |
var roundedSeconds = lroundf(seconds) | |
var roundedMinutes = lroundf(minutes) | |
let numberOfDecimal = "02" | |
var time = "\(roundedMinutes.format(numberOfDecimal)):\(roundedSeconds.format(numberOfDecimal))" | |
return time | |
} | |
func setCurrentAudioTime(value: Float) { | |
self.audioPlayer.seekToTime(Double(value)) | |
} | |
func setCurrentAudioTime(value: Double) { | |
self.audioPlayer.seekToTime(value) | |
} | |
func getCurrentAudioTime() -> NSTimeInterval { | |
return self.audioPlayer.progress | |
} | |
func getAudioDuration() -> Float { | |
return Float(self.audioPlayer.duration) | |
} | |
func getPlayerState() -> Int { | |
var state: STKAudioPlayerState? | |
var value = 1234567890 | |
if self.audioPlayer.state.value == STKAudioPlayerStateBuffering.value { | |
println("Buffering Value : \(STKAudioPlayerStateBuffering.value)") | |
state = STKAudioPlayerStateBuffering | |
value = Int(STKAudioPlayerStateBuffering.value) | |
} else if self.audioPlayer.state.value == STKAudioPlayerStateDisposed.value { | |
println("Disposed Value : \(STKAudioPlayerStateDisposed.value)") | |
state = STKAudioPlayerStateDisposed | |
value = Int(STKAudioPlayerStateDisposed.value) | |
} else if self.audioPlayer.state.value == STKAudioPlayerStateError.value { | |
println("Error value : \(STKAudioPlayerStateDisposed.value)") | |
state = STKAudioPlayerStateError | |
value = Int(STKAudioPlayerStateError.value) | |
} else if self.audioPlayer.state.value == STKAudioPlayerStatePaused.value { | |
println("Paused value: \(STKAudioPlayerStatePaused.value)") | |
state = STKAudioPlayerStatePaused | |
value = Int(STKAudioPlayerStatePaused.value) | |
} else if self.audioPlayer.state.value == STKAudioPlayerStatePlaying.value { | |
println("Playing Value ; \(STKAudioPlayerStatePlaying.value)") | |
println("Song Name : \(self.audioPlayer.currentlyPlayingQueueItemId())") | |
println() | |
for var i = 0; i < songsArray.count; i++ { | |
if self.audioPlayer.currentlyPlayingQueueItemId() == self.songsArray[i].songTrack { | |
self.currentPlayingSongName = self.songsArray[i].songTitle | |
} | |
} | |
state = STKAudioPlayerStatePlaying | |
value = Int(STKAudioPlayerStatePlaying.value) | |
} else if self.audioPlayer.state.value == STKAudioPlayerStateReady.value { | |
println("Ready Value : \(STKAudioPlayerStateReady.value)") | |
state = STKAudioPlayerStateReady | |
value = Int(STKAudioPlayerStateReady.value) | |
} else if self.audioPlayer.state.value == STKAudioPlayerStateRunning.value { | |
println("Running Value : \(STKAudioPlayerStateRunning.value)") | |
state = STKAudioPlayerStateRunning | |
value = Int(STKAudioPlayerStateRunning.value) | |
} else if self.audioPlayer.state.value == STKAudioPlayerStateStopped.value { | |
println("Stopped Value : \(STKAudioPlayerStateStopped.value)") | |
state = STKAudioPlayerStateStopped | |
value = Int(STKAudioPlayerStateStopped.value) | |
} | |
println("Value -------------------------- \(value)") | |
return value | |
} | |
} | |
// Ref: http://stackoverflow.com/a/24055762 | |
extension Int { | |
func format(f: String) -> String { | |
return NSString(format: "%\(f)d", self) as String | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment