diff --git a/Sources/Sound.swift b/Sources/Sound.swift index 334b424..2a58f7b 100644 --- a/Sources/Sound.swift +++ b/Sources/Sound.swift @@ -275,6 +275,22 @@ open class Sound { } } } + + /// Set volume with duration i.e. fade to a new volume over a duration + @available(iOS 10.0, tvOS 10.0, macOS 10.12, *) + public func setVolume(_ volume: Float, fadeDuration duration: TimeInterval) { + for player in players { + player.setVolume(volume, fadeDuration: duration) + } + } + + /// Current time. + /// If the sound is playing, currentTime is the offset of the current playback position, measured in seconds from the start of the sound. + public var currentTime: TimeInterval { + get { + return players[counter].currentTime + } + } /// Stop playing sound for given sound file. /// @@ -319,6 +335,10 @@ public protocol Player: class { /// Resume playing. func resume() + + /// Set volume with duration i.e. fade to a new volume over a duration + @available(iOS 10.0, tvOS 10.0, macOS 10.12, *) + func setVolume(_ volume: Float, fadeDuration duration: TimeInterval) /// Prepare the sound. func prepareToPlay() -> Bool @@ -331,6 +351,9 @@ public protocol Player: class { /// Duration of the sound. var duration: TimeInterval { get } + /// Current time. + var currentTime: TimeInterval { get set } + /// Sound volume. var volume: Float { get set } diff --git a/Tests/SwiftySoundTests/SwiftySoundTests.swift b/Tests/SwiftySoundTests/SwiftySoundTests.swift index c6aac4d..fefb2fd 100644 --- a/Tests/SwiftySoundTests/SwiftySoundTests.swift +++ b/Tests/SwiftySoundTests/SwiftySoundTests.swift @@ -15,6 +15,7 @@ extension String: Error {} final class MockPlayer: Player { var duration: TimeInterval = 1 + var currentTime: TimeInterval = 1 var volume: Float = 1 var isPlaying: Bool = false @@ -180,10 +181,25 @@ class SwiftySoundTests: XCTestCase { } } + func testVolumeWithDuration() { + if let url = bundle.url(forResource: "dog", withExtension: "wav"), let sound = Sound(url: url) { + sound.setVolume(0.5, fadeDuration: 0) + XCTAssertEqual(sound.volume, 0.5) + sound.setVolume(0.0, fadeDuration: 0) + XCTAssertEqual(sound.volume, 0.0) + sound.setVolume(1.0, fadeDuration: 0) + XCTAssertEqual(sound.volume, 1.0) + } + } + func testDuration() { XCTAssert(dogSound.duration > 0) } + func testCurrentTime() { + XCTAssert(dogSound.currentTime > 0) + } + func testNotEnabledPlayback() { Sound.enabled = false let result = dogSound.play()