Skip to content

Commit e955d84

Browse files
Add sound feedback (MonitorControl#91)
Close MonitorControl#84 The system sound is played only when "Play feedback when volume is changed" in Sound Preferences is enabled
1 parent c8ed85f commit e955d84

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

MonitorControl/Display.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import AVFoundation
12
import Cocoa
23
import DDC
4+
import os.log
35

46
class Display {
57
let identifier: CGDirectDisplayID
@@ -12,6 +14,7 @@ class Display {
1214
var ddc: DDC?
1315

1416
private let prefs = UserDefaults.standard
17+
private var audioPlayer: AVAudioPlayer?
1518

1619
init(_ identifier: CGDirectDisplayID, name: String, isEnabled: Bool = true) {
1720
self.identifier = identifier
@@ -67,6 +70,7 @@ class Display {
6770

6871
self.hideDisplayOsd()
6972
self.showOsd(command: .audioSpeakerVolume, value: value)
73+
self.playVolumeChangedSound()
7074
}
7175

7276
if let slider = volumeSliderHandler?.slider {
@@ -158,4 +162,26 @@ class Display {
158162
totalChiclets: UInt32(maxValue / step),
159163
locked: false)
160164
}
165+
166+
private func playVolumeChangedSound() {
167+
let soundPath = "/System/Library/LoginPlugins/BezelServices.loginPlugin/Contents/Resources/volume.aiff"
168+
let soundUrl = URL(fileURLWithPath: soundPath)
169+
170+
// Check if user has enabled "Play feedback when volume is changed" in Sound Preferences
171+
guard let preferences = Utils.getSystemPreferences(),
172+
let hasSoundEnabled = preferences["com.apple.sound.beep.feedback"] as? Int,
173+
hasSoundEnabled == 1 else {
174+
os_log("sound not enabled", type: .info)
175+
return
176+
}
177+
178+
do {
179+
self.audioPlayer = try AVAudioPlayer(contentsOf: soundUrl)
180+
self.audioPlayer?.volume = 1
181+
self.audioPlayer?.prepareToPlay()
182+
self.audioPlayer?.play()
183+
} catch {
184+
os_log("%{public}@", type: .error, error.localizedDescription)
185+
}
186+
}
161187
}

MonitorControl/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>1.5.1</string>
2121
<key>CFBundleVersion</key>
22-
<string>495</string>
22+
<string>509</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSMinimumSystemVersion</key>

MonitorControl/Support/Utils.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ class Utils: NSObject {
9494
return
9595
}
9696

97+
static func getSystemPreferences() -> [String: AnyObject]? {
98+
var propertyListFormat = PropertyListSerialization.PropertyListFormat.xml
99+
let plistPath = NSString(string: "~/Library/Preferences/.GlobalPreferences.plist").expandingTildeInPath
100+
guard let plistXML = FileManager.default.contents(atPath: plistPath) else {
101+
return nil
102+
}
103+
do {
104+
return try PropertyListSerialization.propertyList(from: plistXML, options: .mutableContainersAndLeaves, format: &propertyListFormat) as? [String: AnyObject]
105+
} catch {
106+
os_log("Error reading system prefs plist: %{public}@", type: .info, error.localizedDescription)
107+
return nil
108+
}
109+
}
110+
97111
// MARK: - Display Infos
98112

99113
/// Get the name of a display

MonitorControlHelper/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>1.5.1</string>
2121
<key>CFBundleVersion</key>
22-
<string>495</string>
22+
<string>509</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSBackgroundOnly</key>

0 commit comments

Comments
 (0)