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

Prevent external audio stopping #25

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions Sources/SwiftFortuneWheel/Utils/Audio/AudioPlayable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ extension AudioPlayable {
guard let _audioFile = audioFile else { return }
playSound(audioFile: _audioFile)
}

/// Pause sound(s)
func pauseAllSound() {
audioPlayerManager.pauseAll()
Copy link
Owner

Choose a reason for hiding this comment

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

Since sound playback is optional, it should be guarded first, check if the sound is playable or not before calling the pause method. rename the method so that it is immediately clear that it is optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. I guess I got your point. Let's revisit this when I've spare time.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ class AudioPlayerManager {
}
}

func pauseAll() {
// according to `AVAudioEngine` documentation: https://developer.apple.com/documentation/avfaudio/avaudioengine
// pause/stop needs to be done on player & engine both
// we will stop the player(s) but keep our engine paused, because it's highly likely that the engine is going to be used again in a moment
players.synchronizedArray.forEach { $0.node.stop() }
engine.pause()
}

// MARK: - Internal Functions

private func performSoundPlayback(_ sound: SoundIdentifier, allowOverlap: Bool) {
Expand Down