rustaudiometadatatauri

How can I get the currently playing media metadata in rust


I have tried several crates but don't seem to get it. For more information, i'm trying to use it in Tauri, I'm not very good at Rust as I'm a Javascript dev

// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use playerctl::{PlayerCtl, TrackMetadata};
use serde::{Deserialize, Serialize};
use tauri::{AppHandle, Manager};

#[derive(Serialize)]
struct Metadata {
    artist: String,
    title: String,
    album: String,
    // Add other fields as needed
}

#[tauri::command]
 fn get_current_media_metadata(app_handle: AppHandle) -> Result<Metadata, String> {
    let track_metadata = PlayerCtl::metadata();

    let metadata = Metadata {
        artist: track_metadata.artist,
        title: track_metadata.title,
        album: track_metadata.album,
        // Add other fields as needed
    };

    Ok(metadata)
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![get_current_media_metadata])
        .run(tauri::generate_context!())
        .expect("failed to run app");
}


All I get as error is:

thread 'main' panicked at /home/n3rd/.cargo/registry/src/index.crates.io-6f17d22bba15001f/playerctl-0.1.0/src/lib.rs:40:29:
Failed to execute command 'playerctl metadata title'
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Solution

  • As @cafce25 pointed out you'd need to install playerctl on the target system.

    the proper way to do it would be to wrap C/C++ library code with the Rust wrapper and call it inside your code. Someone already did an attempt on it: playerctl-sys so this might work better for you. And it should compile the code for your platform, and your user won't need to install it separately.

    Checkout their tests source code to get a better understanding how to get metadata for your purpose.