Skip to content

Commit

Permalink
Update success sound
Browse files Browse the repository at this point in the history
  • Loading branch information
dankimio committed Apr 19, 2021
1 parent 272a9a9 commit fcbfb72
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
12 changes: 12 additions & 0 deletions FlipTimer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
374A674725791BAB00F56C72 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 374A674625791BAB00F56C72 /* Preview Assets.xcassets */; };
3783EA59257991470058C69E /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3783EA58257991470058C69E /* SettingsView.swift */; };
37A33CBA257C19A2003F2B96 /* TimerMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37A33CB9257C19A2003F2B96 /* TimerMode.swift */; };
37A3975B262E0507006E8906 /* success.m4a in Resources */ = {isa = PBXBuildFile; fileRef = 37A3975A262E04E9006E8906 /* success.m4a */; };
37ACBD9F25842D0000AD7702 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37ACBD9E25842D0000AD7702 /* ContentView.swift */; };
37BC0AB9257ABE5900238884 /* TimerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37BC0AB8257ABE5900238884 /* TimerViewModel.swift */; };
37C155702581353B000349BA /* SettingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C1556F2581353B000349BA /* SettingsViewModel.swift */; };
Expand All @@ -35,6 +36,7 @@
374A674825791BAB00F56C72 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3783EA58257991470058C69E /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
37A33CB9257C19A2003F2B96 /* TimerMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerMode.swift; sourceTree = "<group>"; };
37A3975A262E04E9006E8906 /* success.m4a */ = {isa = PBXFileReference; lastKnownFileType = file; path = success.m4a; sourceTree = "<group>"; };
37ACBD9E25842D0000AD7702 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
37BC0AB8257ABE5900238884 /* TimerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerViewModel.swift; sourceTree = "<group>"; };
37BEE116257ADD3F00F1F787 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
Expand Down Expand Up @@ -85,6 +87,7 @@
374A673E25791BAA00F56C72 /* FlipTimer */ = {
isa = PBXGroup;
children = (
37A3975E262E0546006E8906 /* Sounds */,
37A33CB8257C1999003F2B96 /* Models */,
37BC0AB7257ABE4900238884 /* View Models */,
37088869257AB46A0037C671 /* Views */,
Expand Down Expand Up @@ -113,6 +116,14 @@
path = Models;
sourceTree = "<group>";
};
37A3975E262E0546006E8906 /* Sounds */ = {
isa = PBXGroup;
children = (
37A3975A262E04E9006E8906 /* success.m4a */,
);
path = Sounds;
sourceTree = "<group>";
};
37BC0AB7257ABE4900238884 /* View Models */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -179,6 +190,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
37A3975B262E0507006E8906 /* success.m4a in Resources */,
374A674725791BAB00F56C72 /* Preview Assets.xcassets in Resources */,
374A674425791BAB00F56C72 /* Assets.xcassets in Resources */,
);
Expand Down
Binary file added FlipTimer/Sounds/success.m4a
Binary file not shown.
16 changes: 15 additions & 1 deletion FlipTimer/View Models/TimerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ final class TimerViewModel: ObservableObject {
// Combine
private var cancellable = Set<AnyCancellable>()

// Audio
private var audioPlayer: AVAudioPlayer?

init() {
$timerMode
.sink { (timerMode) in
Expand Down Expand Up @@ -157,7 +160,8 @@ final class TimerViewModel: ObservableObject {
}

// Play the sound when the timer ends
AudioServicesPlayAlertSound(SystemSoundID(1025))
// AudioServicesPlayAlertSound(SystemSoundID(1025))
playSuccessSound()
}

private var secondsSinceStartedAt: Int {
Expand Down Expand Up @@ -210,4 +214,14 @@ final class TimerViewModel: ObservableObject {
let z = accelerometerData.acceleration.z
return z > 0.9 && z < 1.1
}

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

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

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

0 comments on commit fcbfb72

Please sign in to comment.