Skip to content

Commit

Permalink
fix Cannot play flac audio#19
Browse files Browse the repository at this point in the history
  • Loading branch information
betta-cyber committed Mar 15, 2022
1 parent 87edc8a commit 8068570
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ simple-logging = "2.0.2"
log = "0.4"
log-panics = "2.0.0"
dirs = "2.0.2"
rodio = { version = "0.13.0", features = ["mp3"] }
cpal = "0.13.4"
rodio = { version = "0.15.0"}
tempfile = "3.1.0"
tokio = { version = "0.2", features = ["macros"] }
futures = "0.3.1"
Expand Down
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ impl App {
Some(api) => {
let id = track.id.unwrap().to_string();
let song = api.get_song_url(&id).unwrap();
info!("{:#?}", song);
match song.url {
Some(url) => {
let url = url.to_string();
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn main() -> Result<(), failure::Error> {
let mut app = App::new();
let mut is_first_render = true;


let cloud_music = app.cloud_music.to_owned().unwrap();
let profile = match cloud_music.login_status()? {
Some(profile) => {
Expand Down
13 changes: 7 additions & 6 deletions src/player/player.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use futures::channel::oneshot;
use std::path::PathBuf;
use tempfile::NamedTempFile;
use super::fetch::fetch_data;
use super::track::Track;
use std::time;

use std::thread;
use std::fs;
Expand Down Expand Up @@ -50,6 +50,7 @@ impl Player {
// where
// F: FnOnce() -> Box<dyn Sink> + Send + 'static,
{
// let (_, stream_handle) = rodio::OutputStream::try_default().unwrap();
let (stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&stream_handle).unwrap();
// let endpoint =
Expand Down Expand Up @@ -86,8 +87,7 @@ impl Player {

let buffer = NamedTempFile::new().unwrap();
let path = buffer.path().to_string_lossy().to_string();
debug!("{:#?}", path);
let pathbuf = PathBuf::from(path);
// let pathbuf = PathBuf::from(path);

let (ptx, mut prx) = oneshot::channel::<String>();

Expand All @@ -100,7 +100,7 @@ impl Player {
Ok(p) => {
match p {
Some(_) => {
match Track::load(pathbuf) {
match Track::load(path) {
Ok(track) => {
let mut track = track;
self.load_track(track.clone(), start_playing);
Expand All @@ -117,14 +117,15 @@ impl Player {
}
Err(_) => {}
}
let t = time::Duration::from_millis(250);
thread::sleep(t);
}
}
}

pub fn load_track(&mut self, track: Track, playing: bool) {
if playing {
let path = track.file.to_string_lossy().to_string();
let f = std::fs::File::open(&path).unwrap();
let f = std::fs::File::open(&track.file).unwrap();
let source = rodio::Decoder::new(std::io::BufReader::new(f)).unwrap();

self.sink.play();
Expand Down
22 changes: 15 additions & 7 deletions src/player/track.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::convert::AsRef;
use std::path::{Path, PathBuf};
use std::time::Duration;
use rodio::{Decoder, source::Source};

#[derive(Copy, Clone, Eq, PartialEq, Debug, PartialOrd, Ord)]
pub enum Status {
Expand Down Expand Up @@ -45,7 +45,7 @@ pub struct Track {
/// Duration of the song
pub duration: Duration,
/// File path to the song
pub file: PathBuf,
pub file: String,
/// Elapsed time of song playing or Start time
pub status: Status,
}
Expand Down Expand Up @@ -74,12 +74,20 @@ impl Track {
self.status.is_stopped()
}
/// Returns the path of the song
pub fn file(&self) -> &Path {
pub fn file(&self) -> &str {
&self.file
}

pub fn load(file: PathBuf) -> Result<Self, failure::Error > {
let duration = ::mp3_duration::from_path(&file).unwrap();
pub fn load(file: String) -> Result<Self, failure::Error > {
let f = std::fs::File::open(&file).unwrap();
let source = Decoder::new(std::io::BufReader::new(f)).unwrap();
let duration = match source.total_duration() {
Some(d) => d,
None => mp3_duration::from_path(&file).unwrap(),
};
// Ok(d) => d,
// Err(_) => std::time::Duration::new(100, 0)
// };
Ok(Self {
duration,
file,
Expand All @@ -88,8 +96,8 @@ impl Track {
}
}

impl AsRef<Path> for Track {
fn as_ref(&self) -> &Path {
impl AsRef<String> for Track {
fn as_ref(&self) -> &String {
&self.file
}
}

0 comments on commit 8068570

Please sign in to comment.