diff --git a/Sources/Sound.swift b/Sources/Sound.swift index a470a1e..7809a56 100644 --- a/Sources/Sound.swift +++ b/Sources/Sound.swift @@ -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 { @@ -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 } diff --git a/Tests/SwiftySoundTests/SwiftySoundTests.swift b/Tests/SwiftySoundTests/SwiftySoundTests.swift index c6aac4d..27ebd44 100644 --- a/Tests/SwiftySoundTests/SwiftySoundTests.swift +++ b/Tests/SwiftySoundTests/SwiftySoundTests.swift @@ -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 @@ -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) + } + } }