diff --git a/OpenRA.Game/Sound.cs b/OpenRA.Game/Sound.cs index fede85fde1..2ed68e0039 100644 --- a/OpenRA.Game/Sound.cs +++ b/OpenRA.Game/Sound.cs @@ -78,13 +78,25 @@ namespace OpenRA Play(name, pos); } - public static void PlayVideoSoundtrack(byte[] raw) + public static void PlayVideo(byte[] raw) { rawSource = LoadSoundRaw(raw); video = soundEngine.Play2D(rawSource, false, true, float2.Zero, SoundVolume); } - public static void StopVideoSoundtrack() + public static void PlayVideo() + { + if (video != null) + soundEngine.PauseSound(video, false); + } + + public static void PauseVideo() + { + if (video != null) + soundEngine.PauseSound(video, true); + } + + public static void StopVideo() { if (video != null) soundEngine.StopSound(video); @@ -153,6 +165,11 @@ namespace OpenRA get { return (music != null)? music.SeekPosition : 0; } } + public static float VideoSeekPosition + { + get { return (video != null)? video.SeekPosition : 0; } + } + // Returns true if it played a phrase public static bool PlayVoice(string phrase, Actor voicedUnit) { @@ -412,8 +429,8 @@ namespace OpenRA get { float pos; - Al.alGetSourcef(source, Al.AL_SEC_OFFSET, out pos); - return pos; + Al.alGetSourcef(source, Al.AL_SAMPLE_OFFSET, out pos); + return pos/22050f; } } } diff --git a/OpenRA.Game/Widgets/Delegates/VideoPlayerDelegate.cs b/OpenRA.Game/Widgets/Delegates/VideoPlayerDelegate.cs index a705faf653..142bda39e0 100644 --- a/OpenRA.Game/Widgets/Delegates/VideoPlayerDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/VideoPlayerDelegate.cs @@ -21,12 +21,21 @@ namespace OpenRA.Widgets.Delegates { var bg = Widget.RootWidget.GetWidget("VIDEOPLAYER_MENU"); var player = bg.GetWidget("VIDEOPLAYER"); - bg.GetWidget("BUTTON_PLAY").OnMouseUp = mi => + + var pp = bg.GetWidget("BUTTON_PLAYPAUSE"); + pp.OnMouseUp = mi => { - player.Play(); + if (player.Paused) + player.Play(); + else + player.Pause(); + return true; }; + pp.GetWidget("PLAY").IsVisible = () => player.Paused; + pp.GetWidget("PAUSE").IsVisible = () => !player.Paused; + bg.GetWidget("BUTTON_STOP").OnMouseUp = mi => { player.Stop(); diff --git a/OpenRA.Game/Widgets/VqaPlayerWidget.cs b/OpenRA.Game/Widgets/VqaPlayerWidget.cs index 3f0d9b09a2..2faef53b72 100644 --- a/OpenRA.Game/Widgets/VqaPlayerWidget.cs +++ b/OpenRA.Game/Widgets/VqaPlayerWidget.cs @@ -24,21 +24,24 @@ namespace OpenRA.Widgets float invLength; float2 videoOrigin, videoSize; int[,] overlay; - public bool DrawOverlay = true; + bool stopped; + bool paused; + public bool Paused { get { return paused; } } + + public bool DrawOverlay = true; public void Load(string filename) { if (filename == cachedVideo) return; - if (playing) - { - playing = false; - Sound.StopVideoSoundtrack(); - } - + stopped = true; + paused = true; + Sound.StopVideo(); + cachedVideo = filename; video = new VqaReader(FileSystem.Open(filename)); + invLength = video.Framerate*1f/video.Frames; var size = Math.Max(video.Width, video.Height); @@ -63,16 +66,14 @@ namespace OpenRA.Widgets overlaySprite.sheet.Texture.SetData(overlay); } - bool playing = false; - Stopwatch sw = new Stopwatch(); public override void DrawInner(World world) { if (video == null) return; - if (playing) + if (!(stopped || paused)) { - var nextFrame = (int)float2.Lerp(0, video.Frames, (float)(sw.ElapsedTime()*invLength)); + var nextFrame = (int)float2.Lerp(0, video.Frames, Sound.VideoSeekPosition*invLength); if (nextFrame > video.Frames) { Stop(); @@ -86,6 +87,7 @@ namespace OpenRA.Widgets videoSprite.sheet.Texture.SetData(video.FrameData); } } + Game.Renderer.RgbaSpriteRenderer.DrawSprite(videoSprite, videoOrigin, "chrome", videoSize); if (DrawOverlay) @@ -94,21 +96,34 @@ namespace OpenRA.Widgets public void Play() { - if (playing || video == null) + if (video == null) return; - playing = true; - sw.Reset(); - Sound.PlayVideoSoundtrack(video.AudioData); + if (stopped) + Sound.PlayVideo(video.AudioData); + else + Sound.PlayVideo(); + + stopped = paused = false; + } + + public void Pause() + { + if (stopped || paused || video == null) + return; + + paused = true; + Sound.PauseVideo(); } public void Stop() { - if (!playing || video == null) + if (stopped || video == null) return; - playing = false; - Sound.StopVideoSoundtrack(); + stopped = true; + paused = true; + Sound.StopVideo(); video.Reset(); videoSprite.sheet.Texture.SetData(video.FrameData); } diff --git a/mods/cnc/chrome/videoplayer.yaml b/mods/cnc/chrome/videoplayer.yaml index 39608e87b2..1a29c6666a 100644 --- a/mods/cnc/chrome/videoplayer.yaml +++ b/mods/cnc/chrome/videoplayer.yaml @@ -38,21 +38,25 @@ Container@ROOT: X:2 Y:0 Visible:false - Button@BUTTON_PLAY: - Id:BUTTON_PLAY - X:590 - WIDTH + Button@BUTTON_PLAYPAUSE: + Id:BUTTON_PLAYPAUSE + X:600 - WIDTH - 10 Y:460 Width:25 Height:25 Children: - Image@IMAGE_PLAY: - Id:IMAGE_PLAY - X:0 - Y:0 + Image@PLAY: + Id:PLAY Width:25 Height:25 ImageCollection:music ImageName:play + Image@PAUSE: + Id:PAUSE + Width:25 + Height:25 + ImageCollection:music + ImageName:pause Button@BUTTON_STOP: Id:BUTTON_STOP X:610 @@ -60,10 +64,7 @@ Container@ROOT: Width:25 Height:25 Children: - Image@IMAGE_STOP: - Id:IMAGE_STOP - X:0 - Y:0 + Image: Width:25 Height:25 ImageCollection:music diff --git a/mods/ra/chrome/videoplayer.yaml b/mods/ra/chrome/videoplayer.yaml index 39608e87b2..1a29c6666a 100644 --- a/mods/ra/chrome/videoplayer.yaml +++ b/mods/ra/chrome/videoplayer.yaml @@ -38,21 +38,25 @@ Container@ROOT: X:2 Y:0 Visible:false - Button@BUTTON_PLAY: - Id:BUTTON_PLAY - X:590 - WIDTH + Button@BUTTON_PLAYPAUSE: + Id:BUTTON_PLAYPAUSE + X:600 - WIDTH - 10 Y:460 Width:25 Height:25 Children: - Image@IMAGE_PLAY: - Id:IMAGE_PLAY - X:0 - Y:0 + Image@PLAY: + Id:PLAY Width:25 Height:25 ImageCollection:music ImageName:play + Image@PAUSE: + Id:PAUSE + Width:25 + Height:25 + ImageCollection:music + ImageName:pause Button@BUTTON_STOP: Id:BUTTON_STOP X:610 @@ -60,10 +64,7 @@ Container@ROOT: Width:25 Height:25 Children: - Image@IMAGE_STOP: - Id:IMAGE_STOP - X:0 - Y:0 + Image: Width:25 Height:25 ImageCollection:music