Skip to content

Commit

Permalink
Duck other sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
dankimio committed Apr 19, 2021
1 parent fcbfb72 commit 5b15d92
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions FlipTimer/View Models/TimerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class TimerViewModel: ObservableObject {

// Audio
private var audioPlayer: AVAudioPlayer?
private let audioPlayerDelegate = AudioPlayerDelegate()

init() {
$timerMode
Expand Down Expand Up @@ -215,13 +216,22 @@ final class TimerViewModel: ObservableObject {
return z > 0.9 && z < 1.1
}

// TODO: extract
private func playSuccessSound() {
try! AVAudioSession.sharedInstance().setCategory(.ambient)
try! AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
try! AVAudioSession.sharedInstance().setCategory(.ambient, options: .duckOthers)
try! AVAudioSession.sharedInstance().setActive(true)

let url = Bundle.main.url(forResource: "success", withExtension: "m4a")!

audioPlayer = try! AVAudioPlayer(contentsOf: url)
audioPlayer?.play()
audioPlayer?.delegate = audioPlayerDelegate
}
}

// TODO: extract
class AudioPlayerDelegate: NSObject, AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
try? AVAudioSession.sharedInstance().setActive(false)
}
}

0 comments on commit 5b15d92

Please sign in to comment.