-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add audio devices event source
- Loading branch information
Showing
8 changed files
with
86 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Cocoa | ||
import SimplyCoreAudio | ||
|
||
class AudioSource: EventSource { | ||
let coreAudio = SimplyCoreAudio() | ||
var name = "audio" | ||
var listener: EventListener? | ||
var lastScreens: [String]? | ||
var updating = false | ||
|
||
func emitAll(kind: String, devices: [AudioDevice]) { | ||
var names: [String] = [] | ||
for device in devices where !names.contains(device.name) { | ||
emit(kind: kind, target: device.name) | ||
names.append(device.name) | ||
} | ||
} | ||
|
||
@objc | ||
func handleDeviceListChanged(notification: Notification) { | ||
let addedDevices = notification.userInfo?["addedDevices"] as? [AudioDevice] | ||
if addedDevices != nil && !addedDevices!.isEmpty { | ||
emitAll(kind: "connected", devices: addedDevices!) | ||
} | ||
|
||
let removedDevices = notification.userInfo?["removedDevices"] as? [AudioDevice] | ||
if removedDevices != nil && !removedDevices!.isEmpty { | ||
emitAll(kind: "disconnected", devices: removedDevices!) | ||
} | ||
} | ||
|
||
func subscribe() { | ||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(self.handleDeviceListChanged), | ||
name: Notification.Name.deviceListChanged, | ||
object: nil | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters