diff --git a/src-tauri/src/player/mod.rs b/src-tauri/src/player/mod.rs index 74502cf60..8034fedda 100644 --- a/src-tauri/src/player/mod.rs +++ b/src-tauri/src/player/mod.rs @@ -454,36 +454,13 @@ impl Player { } }; - // Initialize audio device with retry logic - // PipeWire/ALSA may need time to initialize at app startup + // Initialize audio device lazily on first playback to avoid idle CPU usage. let mut current_device_name = device_name.clone(); let mut stream_opt: Option<(OutputStream, rodio::OutputStreamHandle)> = None; const MAX_INIT_RETRIES: u32 = 5; const RETRY_DELAY_MS: u64 = 500; - for attempt in 1..=MAX_INIT_RETRIES { - log::info!("Audio device initialization attempt {}/{}", attempt, MAX_INIT_RETRIES); - - stream_opt = init_device(¤t_device_name, &thread_state); - - if stream_opt.is_some() { - log::info!("Audio device initialized successfully on attempt {}", attempt); - thread_state.set_stream_error(false); - break; - } - - if attempt < MAX_INIT_RETRIES { - log::warn!("Audio init failed, retrying in {}ms...", RETRY_DELAY_MS); - std::thread::sleep(Duration::from_millis(RETRY_DELAY_MS)); - } - } - - if stream_opt.is_none() { - log::error!("Failed to initialize audio after {} attempts. Audio will not work until device is reinitialized.", MAX_INIT_RETRIES); - thread_state.set_stream_error(true); - } - let mut current_sink: Option = None; // Store audio data for seeking (we need to re-decode from the beginning) let mut current_audio_data: Option> = None; @@ -503,6 +480,36 @@ impl Player { AudioCommand::Play { data, track_id, duration_secs } => { log::info!("Audio thread: playing track {}", track_id); + if stream_opt.is_none() { + for attempt in 1..=MAX_INIT_RETRIES { + log::info!( + "Audio device init on playback attempt {}/{}", + attempt, + MAX_INIT_RETRIES + ); + *stream_opt = init_device(current_device_name, &thread_state); + if stream_opt.is_some() { + thread_state.set_stream_error(false); + break; + } + if attempt < MAX_INIT_RETRIES { + log::warn!( + "Audio init failed, retrying in {}ms...", + RETRY_DELAY_MS + ); + std::thread::sleep(Duration::from_millis(RETRY_DELAY_MS)); + } + } + if stream_opt.is_none() { + log::error!( + "Failed to initialize audio after {} attempts.", + MAX_INIT_RETRIES + ); + thread_state.set_stream_error(true); + return; + } + } + let Some(ref stream) = *stream_opt else { log::error!("Audio thread: no audio device available"); return; @@ -612,6 +619,8 @@ impl Player { thread_state.position.store(0, Ordering::SeqCst); thread_state.playback_start_millis.store(0, Ordering::SeqCst); thread_state.position_at_start.store(0, Ordering::SeqCst); + // Drop the stream to release the device and stop background CPU use. + drop(stream_opt.take()); log::info!("Audio thread: stopped"); } AudioCommand::SetVolume(volume) => {