Merge pull request #365 from alexheretic/try-more-devices

OutputStream::try_default: Fallback to non-default devices
This commit is contained in:
est31 2021-05-03 02:15:47 +02:00 committed by GitHub
commit 95813b8104
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -38,11 +38,26 @@ impl OutputStream {
}
/// Return a new stream & handle using the default output device.
///
/// On failure will fallback to trying any non-default output devices.
pub fn try_default() -> Result<(Self, OutputStreamHandle), StreamError> {
let device = cpal::default_host()
let default_device = cpal::default_host()
.default_output_device()
.ok_or(StreamError::NoDevice)?;
Self::try_from_device(&device)
let default_stream = Self::try_from_device(&default_device);
default_stream.or_else(|original_err| {
// default device didn't work, try other ones
let mut devices = match cpal::default_host().output_devices() {
Ok(d) => d,
Err(_) => return Err(original_err),
};
devices
.find_map(|d| Self::try_from_device(&d).ok())
.ok_or(original_err)
})
}
}