Hopefully unstuck the player

This commit is contained in:
chmanie 2023-06-08 20:42:25 +02:00
parent 606f9e6516
commit d782adb506
5 changed files with 36 additions and 52 deletions

View File

@ -5,46 +5,39 @@ use audio_player::{Player, PlayerMessage};
#[tokio::main]
async fn main() {
let player = Player::default();
let messages = player.messages.clone();
// Make sure we read all the messages in time
thread::spawn(move || loop {
match messages.recv() {
Ok(PlayerMessage::Playing) => {
println!("PLAYING NEW TRACK");
}
Ok(PlayerMessage::Duration { duration }) => {
println!("DURATION: {:?}", duration);
}
Ok(PlayerMessage::Elapsed {
duration: _,
elapsed,
}) => {
player
.play("https://www2.cs.uic.edu/~i101/SoundFiles/gettysburg10.wav")
.await
.unwrap();
loop {
match player.messages.recv_async().await {
Ok(PlayerMessage::Elapsed { duration, elapsed }) => {
println!("ELAPSED: {:?}", elapsed);
}
Ok(PlayerMessage::Stopped) => {
println!("STOPPED");
Ok(PlayerMessage::EndOfStream) => {
println!("END OF STREAM");
player
.play("https://www2.cs.uic.edu/~i101/SoundFiles/preamble10.wav")
.await
.unwrap();
break;
}
_ => {}
}
});
player
.play("https://www2.cs.uic.edu/~i101/SoundFiles/CantinaBand60.wav")
.await
.unwrap();
tokio::time::sleep(Duration::from_secs(10)).await;
player.seek_to(Duration::from_secs(20)).await.unwrap();
tokio::time::sleep(Duration::from_secs(10)).await;
player
.play("https://www2.cs.uic.edu/~i101/SoundFiles/PinkPanther60.wav")
.await
.unwrap();
tokio::time::sleep(Duration::from_secs(60)).await;
}
loop {
match player.messages.recv_async().await {
Ok(PlayerMessage::Elapsed { duration, elapsed }) => {
println!("ELAPSED: {:?}", elapsed);
}
Ok(PlayerMessage::EndOfStream) => {
println!("END OF STREAM 2");
break;
}
_ => {}
}
}
}

View File

@ -69,10 +69,6 @@ impl SymphoniaDecoder {
}
}
pub fn into_inner(self) -> MediaSourceStream {
self.format.into_inner()
}
fn init(
mss: MediaSourceStream,
hint: Hint,

View File

@ -1,15 +1,16 @@
use flume::Sender;
use std::path::Path;
use std::sync::atomic::AtomicU64;
use std::thread;
use std::time::Duration;
use std::{fs::File, sync::atomic::Ordering};
use symphonia::core::probe::Hint;
use tracing::warn;
use tracing::{debug, warn};
use url::Url;
use crate::decoder::{MediaInfo, SymphoniaDecoder};
use anyhow::{anyhow, Result};
use rodio::{OutputStream, Sink, Source};
use rodio::{OutputStream, OutputStreamHandle, Sink, Source};
use stream_download::StreamDownload;
use symphonia::core::io::{MediaSource, MediaSourceStream, MediaSourceStreamOptions};
use thiserror::Error;
@ -64,6 +65,7 @@ pub struct PlayerEngine {
sink: Sink,
// We need to keep the stream around as it will stop playing when it's dropped
_stream: OutputStream,
_handle: OutputStreamHandle,
tx_engine: Sender<PlayerEngineCommand>,
tx_player: Sender<PlayerMessage>,
}
@ -81,6 +83,7 @@ impl PlayerEngine {
elapsed: Duration::default(),
sink,
_stream,
_handle: handle,
tx_engine,
tx_player,
})
@ -90,9 +93,7 @@ impl PlayerEngine {
let tx_player = self.tx_player.clone();
let tx_engine = self.tx_engine.clone();
if !self.sink.empty() {
self.reset();
}
let (source, hint) = self.get_source(source_str)?;
let mss = MediaSourceStream::new(source, MediaSourceStreamOptions::default());
@ -196,7 +197,7 @@ impl PlayerEngine {
}
pub fn is_stopped(&self) -> bool {
self.sink.len() == 0
self.sink.empty()
}
pub fn duration(&self) -> Result<Duration> {
@ -248,7 +249,7 @@ impl PlayerEngine {
self.elapsed = Duration::default();
self.current_source = None;
self.sink.pause();
self.sink.clear();
self.sink.stop();
}
fn get_source(&self, source_str: &str) -> Result<(Box<dyn MediaSource>, Hint)> {

View File

@ -61,7 +61,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
playback.run();
info!("playback started");
let addr = "[::1]:50051".parse()?;
let addr = "0.0.0.0:50051".parse()?;
Server::builder()
.add_service(CrabidyServiceServer::new(crabidy_service))
.serve(addr)

View File

@ -467,9 +467,6 @@ impl Playback {
error!("{:?}", err)
}
}
if let Err(err) = self.player.stop().await {
error!("{:?}", err)
};
if let Err(err) = self.player.play(&urls[0]).await {
error!("{:?}", err)
};
@ -510,9 +507,6 @@ impl Playback {
error!("{:?}", err)
}
}
if let Err(err) = self.player.stop().await {
error!("{:?}", err)
};
if let Err(err) = self.player.play(&urls[0]).await {
error!("{:?}", err)
}