Browse Source

Properly handle game pause in VideoPlayer

kobewi 3 years ago
parent
commit
e69f3d527c
2 changed files with 35 additions and 0 deletions
  1. 34 0
      scene/gui/video_stream_player.cpp
  2. 1 0
      scene/gui/video_stream_player.h

+ 34 - 0
scene/gui/video_stream_player.cpp

@@ -174,6 +174,28 @@ void VideoStreamPlayer::_notification(int p_notification) {
 			Size2 s = expand ? get_size() : texture->get_size();
 			Size2 s = expand ? get_size() : texture->get_size();
 			draw_texture_rect(texture, Rect2(Point2(), s), false);
 			draw_texture_rect(texture, Rect2(Point2(), s), false);
 		} break;
 		} break;
+
+		case NOTIFICATION_PAUSED: {
+			if (is_playing() && !is_paused()) {
+				paused_from_tree = true;
+				if (playback.is_valid()) {
+					playback->set_paused(true);
+					set_process_internal(false);
+				}
+				last_audio_time = 0;
+			}
+		} break;
+
+		case NOTIFICATION_UNPAUSED: {
+			if (paused_from_tree) {
+				paused_from_tree = false;
+				if (playback.is_valid()) {
+					playback->set_paused(false);
+					set_process_internal(true);
+				}
+				last_audio_time = 0;
+			}
+		} break;
 	}
 	}
 }
 }
 
 
@@ -255,6 +277,10 @@ void VideoStreamPlayer::play() {
 	playback->play();
 	playback->play();
 	set_process_internal(true);
 	set_process_internal(true);
 	last_audio_time = 0;
 	last_audio_time = 0;
+
+	if (!can_process()) {
+		_notification(NOTIFICATION_PAUSED);
+	}
 }
 }
 
 
 void VideoStreamPlayer::stop() {
 void VideoStreamPlayer::stop() {
@@ -281,6 +307,14 @@ bool VideoStreamPlayer::is_playing() const {
 
 
 void VideoStreamPlayer::set_paused(bool p_paused) {
 void VideoStreamPlayer::set_paused(bool p_paused) {
 	paused = p_paused;
 	paused = p_paused;
+	if (!p_paused && !can_process()) {
+		paused_from_tree = true;
+		return;
+	} else if (p_paused && paused_from_tree) {
+		paused_from_tree = false;
+		return;
+	}
+
 	if (playback.is_valid()) {
 	if (playback.is_valid()) {
 		playback->set_paused(p_paused);
 		playback->set_paused(p_paused);
 		set_process_internal(!p_paused);
 		set_process_internal(!p_paused);

+ 1 - 0
scene/gui/video_stream_player.h

@@ -60,6 +60,7 @@ class VideoStreamPlayer : public Control {
 	int wait_resampler_limit = 2;
 	int wait_resampler_limit = 2;
 
 
 	bool paused = false;
 	bool paused = false;
+	bool paused_from_tree = false;
 	bool autoplay = false;
 	bool autoplay = false;
 	float volume = 1.0;
 	float volume = 1.0;
 	double last_audio_time = 0.0;
 	double last_audio_time = 0.0;