Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Feature: Ability to seek track to specific millisecond #27

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions Source/Jukebox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ extension Jukebox {
- parameter shouldPlay: pass true if playback should be resumed after seeking
*/
public func seek(toSecond second: Int, shouldPlay: Bool = false) {
guard let player = player, let item = currentItem else {return}
player.seekToTime(CMTimeMake(Int64(second), 1))
item.update()
if shouldPlay {
player.play()
if state != .Playing {
state = .Playing
}
}
delegate?.jukeboxPlaybackProgressDidChange(self)
seekCurrentItem(toMillisecond: second * 1000, shouldPlay: shouldPlay)
}

/**
Seeks to a certain millisecond within the current AVPlayerItem and starts playing

- parameter millisecond: the millisecond to seek to
- parameter shouldPlay: pass true if playback should be resumed after seeking
*/
public func seek(toMillisecond millisecond: Int, shouldPlay: Bool = false) {
seekCurrentItem(toMillisecond: millisecond, shouldPlay: shouldPlay)
}

/**
Expand Down Expand Up @@ -382,6 +382,20 @@ public class Jukebox: NSObject, JukeboxItemDelegate {
updateInfoCenter()
}

private func seekCurrentItem(toMillisecond millisecond: Int, shouldPlay: Bool) {
guard let player = player, let item = currentItem else {return}

player.seekToTime(CMTimeMake(Int64(millisecond), 1000))
item.update()
if shouldPlay {
player.play()
if state != .Playing {
state = .Playing
}
}
delegate?.jukeboxPlaybackProgressDidChange(self)
}

// MARK: Items related

private func assignQueuedItems (items: [JukeboxItem]) {
Expand Down
9 changes: 9 additions & 0 deletions Source/JukeboxItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@ protocol JukeboxItemDelegate : class {
public class JukeboxItem: NSObject {

public struct Meta {
/// The duration of the item in seconds.
private(set) public var duration: Double?

/// The title of the item.
private(set) public var title: String?

/// The album the item belongs to.
private(set) public var album: String?

/// The artist of the item.
private(set) public var artist: String?

/// The artwork of the item.
private(set) public var artwork: UIImage?
}

Expand Down