mirror of
https://github.com/MonitorControl/MonitorControl.git
synced 2026-05-16 06:05:52 -06:00
Add sound feedback (#91)
Close #84 The system sound is played only when "Play feedback when volume is changed" in Sound Preferences is enabled
This commit is contained in:
parent
c8ed85f5fd
commit
e955d84123
4 changed files with 42 additions and 2 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import AVFoundation
|
||||
import Cocoa
|
||||
import DDC
|
||||
import os.log
|
||||
|
||||
class Display {
|
||||
let identifier: CGDirectDisplayID
|
||||
|
|
@ -12,6 +14,7 @@ class Display {
|
|||
var ddc: DDC?
|
||||
|
||||
private let prefs = UserDefaults.standard
|
||||
private var audioPlayer: AVAudioPlayer?
|
||||
|
||||
init(_ identifier: CGDirectDisplayID, name: String, isEnabled: Bool = true) {
|
||||
self.identifier = identifier
|
||||
|
|
@ -67,6 +70,7 @@ class Display {
|
|||
|
||||
self.hideDisplayOsd()
|
||||
self.showOsd(command: .audioSpeakerVolume, value: value)
|
||||
self.playVolumeChangedSound()
|
||||
}
|
||||
|
||||
if let slider = volumeSliderHandler?.slider {
|
||||
|
|
@ -158,4 +162,26 @@ class Display {
|
|||
totalChiclets: UInt32(maxValue / step),
|
||||
locked: false)
|
||||
}
|
||||
|
||||
private func playVolumeChangedSound() {
|
||||
let soundPath = "/System/Library/LoginPlugins/BezelServices.loginPlugin/Contents/Resources/volume.aiff"
|
||||
let soundUrl = URL(fileURLWithPath: soundPath)
|
||||
|
||||
// Check if user has enabled "Play feedback when volume is changed" in Sound Preferences
|
||||
guard let preferences = Utils.getSystemPreferences(),
|
||||
let hasSoundEnabled = preferences["com.apple.sound.beep.feedback"] as? Int,
|
||||
hasSoundEnabled == 1 else {
|
||||
os_log("sound not enabled", type: .info)
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
self.audioPlayer = try AVAudioPlayer(contentsOf: soundUrl)
|
||||
self.audioPlayer?.volume = 1
|
||||
self.audioPlayer?.prepareToPlay()
|
||||
self.audioPlayer?.play()
|
||||
} catch {
|
||||
os_log("%{public}@", type: .error, error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.5.1</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>495</string>
|
||||
<string>509</string>
|
||||
<key>LSApplicationCategoryType</key>
|
||||
<string>public.app-category.utilities</string>
|
||||
<key>LSMinimumSystemVersion</key>
|
||||
|
|
|
|||
|
|
@ -94,6 +94,20 @@ class Utils: NSObject {
|
|||
return
|
||||
}
|
||||
|
||||
static func getSystemPreferences() -> [String: AnyObject]? {
|
||||
var propertyListFormat = PropertyListSerialization.PropertyListFormat.xml
|
||||
let plistPath = NSString(string: "~/Library/Preferences/.GlobalPreferences.plist").expandingTildeInPath
|
||||
guard let plistXML = FileManager.default.contents(atPath: plistPath) else {
|
||||
return nil
|
||||
}
|
||||
do {
|
||||
return try PropertyListSerialization.propertyList(from: plistXML, options: .mutableContainersAndLeaves, format: &propertyListFormat) as? [String: AnyObject]
|
||||
} catch {
|
||||
os_log("Error reading system prefs plist: %{public}@", type: .info, error.localizedDescription)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Display Infos
|
||||
|
||||
/// Get the name of a display
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.5.1</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>495</string>
|
||||
<string>509</string>
|
||||
<key>LSApplicationCategoryType</key>
|
||||
<string>public.app-category.utilities</string>
|
||||
<key>LSBackgroundOnly</key>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue