Skip to main content

app_tauri_plugin_player/
lib.rs

1//! Tauri plugin for audio player control and media session management.
2//!
3//! This plugin provides platform-specific audio playback control for `MoosicBox` applications,
4//! supporting both desktop and mobile platforms with native media controls.
5//!
6//! # Features
7//!
8//! * Control playback state (play, pause, seek)
9//! * Manage playlists and track navigation
10//! * Handle platform media control events
11//! * Cross-platform support (desktop and mobile)
12//!
13//! # Usage
14//!
15//! Initialize the plugin in your Tauri application:
16//!
17//! ```rust,ignore
18//! use tauri_plugin_player::PlayerExt;
19//!
20//! tauri::Builder::default()
21//!     .plugin(tauri_plugin_player::init())
22//!     .setup(|app| {
23//!         // Access player through the extension trait
24//!         let player = app.player();
25//!         Ok(())
26//!     })
27//!     .run(tauri::generate_context!())
28//!     .expect("error while running tauri application");
29//! ```
30//!
31//! # Platform Support
32//!
33//! The plugin adapts to the target platform:
34//!
35//! * **Desktop**: Provides a lightweight player interface
36//! * **Mobile** (iOS/Android): Integrates with native media session APIs
37//!
38//! # Main Types
39//!
40//! * [`PlayerExt`] - Extension trait for accessing player functionality
41//! * [`init()`] - Plugin initialization function
42//! * [`Track`] - Music track with metadata
43//! * [`Playlist`] - Collection of tracks
44//! * [`UpdateState`] - Request for updating player state
45//! * [`Error`] - Error types for plugin operations
46
47#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
48#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
49#![allow(clippy::multiple_crate_versions)]
50
51use tauri::{
52    Manager, Runtime,
53    plugin::{Builder, TauriPlugin},
54};
55
56pub use models::*;
57
58#[cfg(desktop)]
59mod desktop;
60#[cfg(mobile)]
61mod mobile;
62
63mod commands;
64mod error;
65mod models;
66
67pub use error::{Error, Result};
68
69#[cfg(desktop)]
70use desktop::Player;
71#[cfg(mobile)]
72use mobile::Player;
73
74/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the player APIs.
75pub trait PlayerExt<R: Runtime> {
76    /// Gets a reference to the player instance.
77    #[must_use]
78    fn player(&self) -> &Player<R>;
79}
80
81impl<R: Runtime, T: Manager<R>> crate::PlayerExt<R> for T {
82    fn player(&self) -> &Player<R> {
83        self.state::<Player<R>>().inner()
84    }
85}
86
87/// Initializes the plugin.
88#[must_use]
89pub fn init<R: Runtime>() -> TauriPlugin<R> {
90    Builder::new("player")
91        .invoke_handler(tauri::generate_handler![commands::update_state])
92        .setup(|app, api| {
93            #[cfg(mobile)]
94            let player = mobile::init(app, &api)?;
95            #[cfg(desktop)]
96            let player = desktop::init(app, &api);
97            app.manage(player);
98            Ok(())
99        })
100        .build()
101}