feat: playback of liked tracks with audio engine, queue and player bar
- ym-api: Track/Stream models, liked tracks, signed download URLs (full MP3 over AAC over previews, highest bitrate) and in-memory fetch - Audio thread on rodio, driven by commands and reporting through an iced subscription; generation ids drop stale events - Opens the default output with a buffer clamped to the device's range: rodio's 2048-frame default fails on Bluetooth headphones that cap at 1024 - Queue with shuffle, repeat off/all/one, back/advance and jump; player state machine with prefetch of the next track, replay from memory, and skipping unplayable tracks (stops after three in a row) - Working player bar (seek, volume, mute, quality badge), queue drawer, Collection screen with a lazily built liked-tracks list - audio_probe example lists and test-opens output devices
This commit is contained in:
1 parent
43e8f86a5d
commit
b73af1f870
20 files changed
+2141
-202
No files matched your search
@@ -1,5 +1,8 @@
|
||||
use crate::Result;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::auth::Credentials;
|
||||
use crate::models::{Stream, Track, best_variant};
|
||||
use crate::{Error, Result};
|
||||
|
||||
/// The signed-in user as the UI needs them.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -55,7 +58,7 @@ impl YmClient {
|
||||
let inner = yamuse::Client::builder().token(&credentials.access_token).build()?;
|
||||
let status = inner.refresh_account().await?;
|
||||
let account = Account::from_status(status)
|
||||
.ok_or_else(|| crate::Error::Unauthorized("account status has no user".into()))?;
|
||||
.ok_or_else(|| Error::Unauthorized("account status has no user".into()))?;
|
||||
Ok((Self { inner }, account))
|
||||
}
|
||||
|
||||
@@ -64,11 +67,62 @@ impl YmClient {
|
||||
Ok(Self { inner: yamuse::Client::anonymous()? })
|
||||
}
|
||||
|
||||
/// The user's liked tracks, newest like first, with full metadata.
|
||||
pub async fn liked_tracks(&self) -> Result<Vec<Track>> {
|
||||
let list = self.inner.liked_tracks(None).await?;
|
||||
let ids: Vec<String> = list
|
||||
.tracks
|
||||
.into_iter()
|
||||
.filter_map(|short| {
|
||||
let id = match short.id? {
|
||||
yamuse::models::Id::Number(n) => n.to_string(),
|
||||
yamuse::models::Id::Text(s) => s,
|
||||
};
|
||||
Some(match short.album_id {
|
||||
Some(album) if !id.contains(':') => format!("{id}:{album}"),
|
||||
_ => id,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
self.tracks(ids).await
|
||||
}
|
||||
|
||||
/// Full metadata for `ids` (`"trackId"` or `"trackId:albumId"`), in the given order.
|
||||
pub async fn tracks(&self, ids: Vec<String>) -> Result<Vec<Track>> {
|
||||
if ids.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let tracks = self.inner.tracks(&ids).await?;
|
||||
Ok(tracks.into_iter().filter_map(Track::from_yamuse).collect())
|
||||
}
|
||||
|
||||
/// Resolves a signed, short-lived audio URL for a track.
|
||||
pub async fn stream(&self, track_id: &str) -> Result<Stream> {
|
||||
let variants = self.inner.track_download_info(track_id).await?;
|
||||
let variant = best_variant(&variants)
|
||||
.ok_or_else(|| Error::Api(format!("no playable variant for track {track_id}")))?;
|
||||
let url = variant.direct_link(self.inner.transport()).await?;
|
||||
Ok(Stream {
|
||||
url,
|
||||
codec: variant.codec.clone().unwrap_or_default(),
|
||||
bitrate_kbps: variant.bitrate_in_kbps.unwrap_or(0).max(0) as u32,
|
||||
preview: variant.preview.unwrap_or(false),
|
||||
})
|
||||
}
|
||||
|
||||
/// Downloads a whole stream into memory (a 320 kbps track is about 10 MB).
|
||||
pub async fn fetch(&self, stream: &Stream) -> Result<Arc<[u8]>> {
|
||||
let bytes = yamuse::download::Downloader::new(self.inner.transport(), [stream.url.clone()])
|
||||
.to_bytes()
|
||||
.await?;
|
||||
Ok(bytes.into())
|
||||
}
|
||||
|
||||
/// Re-reads the account, e.g. after a subscription may have changed.
|
||||
pub async fn account(&self) -> Result<Account> {
|
||||
let status = self.inner.refresh_account().await?;
|
||||
Account::from_status(status)
|
||||
.ok_or_else(|| crate::Error::Unauthorized("account status has no user".into()))
|
||||
.ok_or_else(|| Error::Unauthorized("account status has no user".into()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user