Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions Sources/Sound.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, *)
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.
///
Expand Down Expand Up @@ -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, *)
func setVolume(_ volume: Float, fadeDuration duration: TimeInterval)

/// Prepare the sound.
func prepareToPlay() -> Bool
Expand All @@ -331,6 +351,9 @@ public protocol Player: class {
/// Duration of the sound.
var duration: TimeInterval { get }

/// Current time.
var currentTime: TimeInterval { get set }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setter is not implemented for this property


/// Sound volume.
var volume: Float { get set }

Expand Down
16 changes: 16 additions & 0 deletions Tests/SwiftySoundTests/SwiftySoundTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down