-
Notifications
You must be signed in to change notification settings - Fork 297
Add with_error_callback to OutputStreamBuilder #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac17ea9
Add with_error_callback to OutputStreamBuilder
will3942 d90b842
Migrate to FnMut using Box
will3942 11085b5
Remove Box and use generic
will3942 7bb6a9a
Merge branch 'master' into with_error_callback
will3942 a8e3b29
Improve example to show mpsc sender/receiver
will3942 a5bb784
Fix build
will3942 d154870
Explain more about the desired error handling
will3942 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| use cpal::traits::HostTrait; | ||
| use rodio::source::SineWave; | ||
| use rodio::Source; | ||
| use std::error::Error; | ||
| use std::time::Duration; | ||
|
|
||
| fn main() -> Result<(), Box<dyn Error>> { | ||
| // You can use any other output device that can be queried from CPAL. | ||
| let default_device = cpal::default_host() | ||
| .default_output_device() | ||
| .ok_or("No default audio output device is found.")?; | ||
|
|
||
| let (tx, rx) = std::sync::mpsc::channel(); | ||
|
|
||
| let stream_handle = rodio::OutputStreamBuilder::from_device(default_device)? | ||
| .with_error_callback(move |err| { | ||
| // Filter for where err is a DeviceNotAvailable error. | ||
| if let cpal::StreamError::DeviceNotAvailable = err { | ||
| if let Err(e) = tx.send(err) { | ||
| eprintln!("Error emitting StreamError: {}", e); | ||
| } | ||
| } | ||
| }) | ||
| .open_stream_or_fallback()?; | ||
|
|
||
| let mixer = stream_handle.mixer(); | ||
|
|
||
| let wave = SineWave::new(740.0) | ||
| .amplify(0.1) | ||
| .take_duration(Duration::from_secs(30)); | ||
| mixer.add(wave); | ||
|
|
||
| if let Ok(err) = rx.recv_timeout(Duration::from_secs(30)) { | ||
| // Here we print the error that was emitted by the error callback. | ||
| // but in a real application we may want to destroy the stream and | ||
| // try to reopen it, either with the same device or a different one. | ||
| eprintln!("Error with stream {}", err); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.