Program freezes if network audio device becomes unavailable while playing audio

87 Views Asked by At

I have a python program that loops, playing audio files using python-sounddevice on an output device connected using dante virtual soundcard.

While playing the audio, if the network connection is suddenly lost, the program becomes completely unresponsive and freezes (the computer no longer has the output device available).

I have tried using a try/except to handle/catch a possible exception, but it seems that if this occurs during the audio playing, it does not raise any exception.

EDIT: Also, if reconnected to the network, the computer detects the output device again, but it doesn't help the issue.

Code:

import os
import sounddevice as sd
import soundfile as sf
import numpy
import time
import errno

if os.path.isfile(path) and os.access(path, os.R_OK):
    try:        
        data, fs = sf.read(path, dtype='float32')
        print('START')
        sd.play(data, fs, device = device_id)
        sd.wait()
        print('END') #never gets to this point
    except BaseException as err:
        print(err.args) 
else:
    raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)
0

There are 0 best solutions below