Skip to content

Commit

Permalink
Fix Bevy crashing if no audio device is found (bevyengine#2269)
Browse files Browse the repository at this point in the history
  • Loading branch information
forbjok authored and ostwilkens committed Jul 27, 2021
1 parent 079b711 commit 72af27b
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions crates/bevy_audio/src/audio_output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Audio, AudioSource, Decodable};
use bevy_asset::{Asset, Assets};
use bevy_ecs::world::World;
use bevy_utils::tracing::warn;
use rodio::{OutputStream, OutputStreamHandle, Sink};
use std::marker::PhantomData;

Expand All @@ -9,8 +10,8 @@ pub struct AudioOutput<P = AudioSource>
where
P: Decodable,
{
_stream: OutputStream,
stream_handle: OutputStreamHandle,
_stream: Option<OutputStream>,
stream_handle: Option<OutputStreamHandle>,
phantom: PhantomData<P>,
}

Expand All @@ -19,12 +20,19 @@ where
P: Decodable,
{
fn default() -> Self {
let (stream, stream_handle) = OutputStream::try_default().unwrap();

Self {
_stream: stream,
stream_handle,
phantom: PhantomData,
if let Ok((stream, stream_handle)) = OutputStream::try_default() {
Self {
_stream: Some(stream),
stream_handle: Some(stream_handle),
phantom: PhantomData,
}
} else {
warn!("No audio device found.");
Self {
_stream: None,
stream_handle: None,
phantom: PhantomData,
}
}
}
}
Expand All @@ -36,9 +44,11 @@ where
<<P as Decodable>::Decoder as Iterator>::Item: rodio::Sample + Send + Sync,
{
fn play_source(&self, audio_source: &P) {
let sink = Sink::try_new(&self.stream_handle).unwrap();
sink.append(audio_source.decoder());
sink.detach();
if let Some(stream_handle) = &self.stream_handle {
let sink = Sink::try_new(&stream_handle).unwrap();
sink.append(audio_source.decoder());
sink.detach();
}
}

fn try_play_queued(&self, audio_sources: &Assets<P>, audio: &mut Audio<P>) {
Expand Down

0 comments on commit 72af27b

Please sign in to comment.