可以正常播放

This commit is contained in:
2025-08-17 22:20:39 +08:00
parent 90e4a01e62
commit a02c37277e
2 changed files with 100 additions and 61 deletions

View File

@@ -38,11 +38,15 @@ pub fn run() {
let player_state_for_load = player_state.clone();
tauri::async_runtime::spawn(async move {
let mut candidates: Vec<std::path::PathBuf> = Vec::new();
// Add current working directory
if let Ok(cwd) = std::env::current_dir() {
log::info!("🔍 Searching in current working directory: {}", cwd.display());
candidates.push(cwd.join("video.mp4"));
candidates.push(cwd.join("public").join("video.mp4"));
}
// Add executable directory and its ancestors
if let Ok(exe_path) = std::env::current_exe() {
log::info!("📁 Executable path: {}", exe_path.display());
let mut ancestor_opt = exe_path.parent();
@@ -63,57 +67,66 @@ pub fn run() {
}
for candidate in candidates {
if tokio::fs::metadata(&candidate).await.is_ok() {
let path_string = candidate.to_string_lossy().to_string();
log::info!("✅ Found video file: {}", path_string);
// Update backend player state
{
let mut state = player_state_for_load.lock().await;
let title = candidate.file_name().and_then(|n| n.to_str()).unwrap_or("video.mp4").to_string();
let video_info = VideoInfo {
path: path_string.clone(),
title,
duration: None,
size: None,
format: None,
};
state.load_video(video_info);
// 默认启用循环并开始播放
state.set_loop(true);
state.play();
match tokio::fs::metadata(&candidate).await {
Ok(metadata) => {
if metadata.is_file() {
let path_string = candidate.to_string_lossy().to_string();
log::info!("✅ Found video file: {}", path_string);
// Update backend player state
{
let mut state = player_state_for_load.lock().await;
let title = candidate.file_name().and_then(|n| n.to_str()).unwrap_or("video.mp4").to_string();
let video_info = VideoInfo {
path: path_string.clone(),
title,
duration: None,
size: None,
format: None,
};
state.load_video(video_info);
state.set_loop(true);
state.play();
}
// Send commands to frontend with increasing delays for reliability
let app_handle_clone = app_handle_load.clone();
let path_for_load = path_string.clone();
// Immediate load
tauri::async_runtime::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let _ = app_handle_clone.emit("player-command", serde_json::json!({
"type": "loadVideo",
"path": "video.mp4"
}));
});
// Set loop after load
let app_handle_clone2 = app_handle_load.clone();
tauri::async_runtime::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
let _ = app_handle_clone2.emit("player-command", serde_json::json!({
"type": "setLoop",
"enabled": true
}));
});
// Play after load and loop are set
let app_handle_clone3 = app_handle_load.clone();
tauri::async_runtime::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
let _ = app_handle_clone3.emit("player-command", serde_json::json!({
"type": "play"
}));
});
return; // Exit early since we found a video
}
}
Err(e) => {
log::debug!("❌ File not found or inaccessible: {} - {}", candidate.display(), e);
}
// Notify frontend to load and play the video (use absolute path; frontend will add file:// if needed)
let _ = app_handle_load.emit("player-command", serde_json::json!({
"type": "loadVideo",
"path": path_string
}));
let _ = app_handle_load.emit("player-command", serde_json::json!({
"type": "setLoop",
"enabled": true
}));
let _ = app_handle_load.emit("player-command", serde_json::json!({
"type": "play"
}));
// Re-emit after a short delay to ensure frontend listeners are ready in dev mode
let app_handle_clone = app_handle_load.clone();
let delayed_path = path_string.clone();
tauri::async_runtime::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(800)).await;
let _ = app_handle_clone.emit("player-command", serde_json::json!({
"type": "loadVideo",
"path": delayed_path
}));
let _ = app_handle_clone.emit("player-command", serde_json::json!({
"type": "setLoop",
"enabled": true
}));
let _ = app_handle_clone.emit("player-command", serde_json::json!({
"type": "play"
}));
});
return; // Exit early since we found a video
}
}