Created
August 11, 2025 21:40
-
-
Save haydenholligan/0cdccde1d44d2fffcfdca84945318451 to your computer and use it in GitHub Desktop.
determine whether audio is playing
This file contains hidden or 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
| #!/usr/bin/env swift | |
| import Foundation | |
| func isAudioPlaying() -> Bool { | |
| let task = Process() | |
| task.launchPath = "/usr/bin/pmset" | |
| task.arguments = ["-g", "assertions"] | |
| let pipe = Pipe() | |
| task.standardOutput = pipe | |
| task.launch() | |
| task.waitUntilExit() | |
| let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
| if let output = String(data: data, encoding: .utf8) { | |
| // Check for coreaudiod assertions that indicate audio is playing | |
| // These are created when audio is actively being output | |
| let audioIndicators = [ | |
| "com.apple.audio.", | |
| "coreaudiod", | |
| "PreventUserIdleSystemSleep" | |
| ] | |
| // Look for coreaudiod preventing sleep (indicates audio is playing) | |
| // When audio plays, coreaudiod creates PreventUserIdleSystemSleep assertions | |
| let lines = output.components(separatedBy: "\n") | |
| for line in lines { | |
| if line.contains("coreaudiod") && | |
| line.contains("PreventUserIdleSystemSleep") { | |
| // Audio is actively playing | |
| return true | |
| } | |
| } | |
| } | |
| return false | |
| } | |
| // Check if audio is playing | |
| let isPlaying = isAudioPlaying() | |
| // Print result | |
| if isPlaying { | |
| print("YES - Audio is currently playing") | |
| exit(0) | |
| } else { | |
| print("NO - No audio is playing") | |
| exit(1) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment