Fix Bevy crashing if no audio device is found (#2269)
Fixes https://github.com/bevyengine/bevy/issues/850
This commit is contained in:
parent
7835c92647
commit
ac04c71d97
@ -1,6 +1,7 @@
|
|||||||
use crate::{Audio, AudioSource, Decodable};
|
use crate::{Audio, AudioSource, Decodable};
|
||||||
use bevy_asset::{Asset, Assets};
|
use bevy_asset::{Asset, Assets};
|
||||||
use bevy_ecs::world::World;
|
use bevy_ecs::world::World;
|
||||||
|
use bevy_utils::tracing::warn;
|
||||||
use rodio::{OutputStream, OutputStreamHandle, Sink};
|
use rodio::{OutputStream, OutputStreamHandle, Sink};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
@ -9,8 +10,8 @@ pub struct AudioOutput<P = AudioSource>
|
|||||||
where
|
where
|
||||||
P: Decodable,
|
P: Decodable,
|
||||||
{
|
{
|
||||||
_stream: OutputStream,
|
_stream: Option<OutputStream>,
|
||||||
stream_handle: OutputStreamHandle,
|
stream_handle: Option<OutputStreamHandle>,
|
||||||
phantom: PhantomData<P>,
|
phantom: PhantomData<P>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,12 +20,19 @@ where
|
|||||||
P: Decodable,
|
P: Decodable,
|
||||||
{
|
{
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let (stream, stream_handle) = OutputStream::try_default().unwrap();
|
if let Ok((stream, stream_handle)) = OutputStream::try_default() {
|
||||||
|
Self {
|
||||||
Self {
|
_stream: Some(stream),
|
||||||
_stream: stream,
|
stream_handle: Some(stream_handle),
|
||||||
stream_handle,
|
phantom: PhantomData,
|
||||||
phantom: PhantomData,
|
}
|
||||||
|
} else {
|
||||||
|
warn!("No audio device found.");
|
||||||
|
Self {
|
||||||
|
_stream: None,
|
||||||
|
stream_handle: None,
|
||||||
|
phantom: PhantomData,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,9 +44,11 @@ where
|
|||||||
<<P as Decodable>::Decoder as Iterator>::Item: rodio::Sample + Send + Sync,
|
<<P as Decodable>::Decoder as Iterator>::Item: rodio::Sample + Send + Sync,
|
||||||
{
|
{
|
||||||
fn play_source(&self, audio_source: &P) {
|
fn play_source(&self, audio_source: &P) {
|
||||||
let sink = Sink::try_new(&self.stream_handle).unwrap();
|
if let Some(stream_handle) = &self.stream_handle {
|
||||||
sink.append(audio_source.decoder());
|
let sink = Sink::try_new(&stream_handle).unwrap();
|
||||||
sink.detach();
|
sink.append(audio_source.decoder());
|
||||||
|
sink.detach();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_play_queued(&self, audio_sources: &Assets<P>, audio: &mut Audio<P>) {
|
fn try_play_queued(&self, audio_sources: &Assets<P>, audio: &mut Audio<P>) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user