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:
Guillaume B 2019-05-16 21:35:46 +02:00 committed by GitHub
parent c8ed85f5fd
commit e955d84123
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 2 deletions

View file

@ -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)
}
}
}

View file

@ -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>

View file

@ -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

View file

@ -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>