Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getter and setter for currentTime #63

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Sources/Sound.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ open class Sound {
}
}

public var currentTime: TimeInterval {
get { players[counter].currentTime }
set { players[counter].currentTime = newValue }
}

/// Sound volume.
/// A value in the range 0.0 to 1.0, with 0.0 representing the minimum volume and 1.0 representing the maximum volume.
public var volume: Float {
Expand Down Expand Up @@ -328,6 +333,10 @@ public protocol Player: AnyObject {
/// - Parameter url: sound url.
init(contentsOf url: URL) throws

/// - If the sound **is** playing, currentTime is the offset into the sound of the current playback position.
/// - If the sound **is not** playing, currentTime is the offset into the sound where playing would start.
var currentTime: TimeInterval { get set }

/// Duration of the sound.
var duration: TimeInterval { get }

Expand Down
7 changes: 7 additions & 0 deletions Tests/SwiftySoundTests/SwiftySoundTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extension String: Error {}

final class MockPlayer: Player {

var currentTime: TimeInterval = 0
var duration: TimeInterval = 1
var volume: Float = 1
var isPlaying: Bool = false
Expand Down Expand Up @@ -227,4 +228,10 @@ class SwiftySoundTests: XCTestCase {
XCTAssert(dogSound.resume())
}

func testTimeSeek() {
if let url = bundle.url(forResource: "cat", withExtension: "wav"), let sound = Sound(url: url) {
sound.currentTime = 0.5
XCTAssertEqual(sound.currentTime, 0.5, accuracy: 0.01)
}
}
}