How to check for sound output device

When the audio player attempts to play a sound on a client with no audio output device, Ignition throws this error:
image

Details: java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED 22050.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian, and buffers of 80764 to 80764 bytes is supported…

What would be a good way to check whether an output device is available ahead of time to disable sound playback and avoid this error message?

1 Like

I don’t have anything here without audio, so I can’t test it, but this should be pretty quick and dirty:

def soundCardExists():
	from javax.sound.sampled import AudioSystem
	try:
		l = AudioSystem.getSourceDataLine(None)
		return True
	except:
		return False

soundCardExists()
3 Likes

Thanks @JordanCClark, your code works as expected on a device without audio (as well as on devices with it). My initial post was incorrect in that the error is generated when the sound player component loads soundDatanot when it tries to play it. A binding like the one below on ‘soundData’ avoids the error. A function like yours sets the client tag in a client startup script and sound bytes are stored in a tag.

// Clear sound data on clients with no sound output device to avoid errors.
if(!{[Client]isSoundOutputDeviceAvailable}, None, {Ignition/sounds}[3,1])
1 Like

Can you tell me how you stored the sound bytes in the dataset?

It’s been a while, but I believe I used the browse button on soundData property of the sound player component to load a sound into it and then copied the soundData string that showed up in that property to the dataset.