From a26d0fcc0a30a28ce0e458ea275fc0787c693bc6 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 8 Mar 2022 16:39:55 +0100 Subject: Save youtube watch progress and resume next time the video is played --- src/QuickMedia.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index b9dd0f0..0da90f9 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3034,7 +3034,9 @@ namespace QuickMedia { bool video_loaded = false; double video_time_pos = 0.0; // Time in media in seconds. Updates every 5 seconds and when starting to watch the video and when seeking. + double video_duration = 0.0; // Time in seconds. 0 if unknown bool update_time_pos = false; + bool update_duration = false; mgl::Clock video_time_pos_clock; std::string youtube_video_id_dummy; @@ -3066,8 +3068,7 @@ namespace QuickMedia { if(!subtitle_data.url.empty()) video_player->add_subtitle(subtitle_data.url, subtitle_data.title, "eng"); - if(video_page->is_local()) - update_time_pos = true; + update_time_pos = true; }; std::unique_ptr youtube_video_media_proxy; @@ -3109,7 +3110,8 @@ namespace QuickMedia { for(int i = 0; i < num_retries; ++i) { bool cancelled = false; TaskResult load_result = run_task_with_loading_screen([&]() { - if(video_page->load(new_title, channel_url, media_chapters, err_str) != PluginResult::OK) + video_duration = 0.0; + if(video_page->load(new_title, channel_url, video_duration, media_chapters, err_str) != PluginResult::OK) return false; std::string ext; @@ -3328,18 +3330,17 @@ namespace QuickMedia { } else if(strcmp(event_name, "playback-restart") == 0) { //video_player->set_paused(false); } else if(strcmp(event_name, "start-file") == 0) { + update_duration = true; added_recommendations = false; time_watched_timer.restart(); video_loaded = true; - if(video_page->is_local()) - update_time_pos = true; + update_time_pos = true; } else if(strcmp(event_name, "file-loaded") == 0) { video_loaded = true; } else if(strcmp(event_name, "video-reconfig") == 0 || strcmp(event_name, "audio-reconfig") == 0) { video_loaded = true; } else if(strcmp(event_name, "seek") == 0) { - if(video_page->is_local()) - update_time_pos = true; + update_time_pos = true; } else if(strcmp(event_name, "fullscreen") == 0 && args.size() == 1) { window_set_fullscreen(disp, window.get_system_handle(), args[0] == "yes" ? WindowFullscreenState::SET : WindowFullscreenState::UNSET); } @@ -3465,6 +3466,8 @@ namespace QuickMedia { XSync(disp, False); show_notification("QuickMedia", "Failed to get related pages", Urgency::CRITICAL); } else if(related_pages_result == TaskResult::TRUE && !related_pages.empty()) { + video_page->set_watch_progress(video_time_pos, video_duration); + bool page_changed = false; double resume_start_time = 0.0; page_loop(related_pages, video_page->get_related_pages_first_tab(), [&](const std::vector &new_tabs) { @@ -3654,6 +3657,13 @@ namespace QuickMedia { update_time_pos = false; video_player->get_time_in_file(&video_time_pos); } + + if(update_duration) { + update_duration = false; + double file_duration = 0.0; + video_player->get_duration_in_file(&file_duration); + video_duration = std::max(video_duration, file_duration); + } } if(video_player_window) { @@ -3679,8 +3689,7 @@ namespace QuickMedia { window_size.x = window_size_u.x; window_size.y = window_size_u.y; - if(video_page->is_local()) - video_page->set_watch_progress(video_time_pos); + video_page->set_watch_progress(video_time_pos, video_duration); } void Program::select_episode(BodyItem *item, bool start_from_beginning) { @@ -7525,7 +7534,8 @@ namespace QuickMedia { std::string channel_url; std::vector chapters; filename.clear(); - if(youtube_video_page->load(filename, channel_url, chapters, err_str) != PluginResult::OK) + double duration; + if(youtube_video_page->load(filename, channel_url, duration, chapters, err_str) != PluginResult::OK) return false; std::string ext; -- cgit v1.2.3