提交 efe20c33 编写于 作者: R Rafael Rosa

chore: fix plaback state when autoplay not work

上级 42df67b3
......@@ -47,6 +47,17 @@ partial class HtmlMediaPlayer
=> throw new NotSupportedException();
#endif
#if USE_JSIMPORT
[JSImport("globalThis.Uno.UI.Media.HtmlMediaPlayer.getPaused")]
#endif
internal static partial bool GetPaused(nint htmlId);
#if !USE_JSIMPORT
internal static partial bool GetPaused(nint htmlId)
=> throw new NotSupportedException();
#endif
#if USE_JSIMPORT
[JSImport("globalThis.Uno.UI.Media.HtmlMediaPlayer.setCurrentPosition")]
#endif
......
......@@ -12,6 +12,7 @@ using System.Globalization;
using Windows.UI.Xaml.Controls.Maps;
using Windows.UI.Xaml.Media;
using Windows.UI.Notifications;
using System.Numerics;
namespace Uno.UI.Media;
......@@ -68,6 +69,7 @@ internal partial class HtmlMediaPlayer : Border
StatusPlayChanged += OnHtmlStatusPlayChanged;
StatusPauseChanged += OnHtmlStatusPauseChanged;
SourceFailed += OnHtmlSourceFailed;
SourceSuspend += OnHtmlSourceSuspend;
SourceEnded += OnHtmlSourceEnded;
MetadataLoaded += OnHtmlMetadataLoaded;
......@@ -84,6 +86,7 @@ internal partial class HtmlMediaPlayer : Border
StatusPlayChanged -= OnHtmlStatusPlayChanged;
StatusPauseChanged -= OnHtmlStatusPauseChanged;
SourceFailed -= OnHtmlSourceFailed;
SourceSuspend -= OnHtmlSourceSuspend;
SourceEnded -= OnHtmlSourceEnded;
MetadataLoaded -= OnHtmlMetadataLoaded;
TimeUpdated -= OnHtmlTimeUpdated;
......@@ -297,6 +300,23 @@ internal partial class HtmlMediaPlayer : Border
}
}
/// <summary>
/// Occurs when there is an Suspend associated with video. To fix the autoplay validation
/// </summary>
event EventHandler<HtmlCustomEventArgs> SourceSuspend
{
add
{
_htmlVideo.RegisterHtmlCustomEventHandler("suspend", value, isDetailJson: false);
_htmlAudio.RegisterHtmlCustomEventHandler("suspend", value, isDetailJson: false);
}
remove
{
_htmlVideo.UnregisterHtmlCustomEventHandler("suspend", value);
_htmlAudio.UnregisterHtmlCustomEventHandler("suspend", value);
}
}
private void OnHtmlTimeUpdated(object sender, EventArgs e)
{
OnTimeUpdate?.Invoke(this, EventArgs.Empty);
......@@ -345,6 +365,7 @@ internal partial class HtmlMediaPlayer : Border
Duration = NativeMethods.GetDuration(_activeElement.HtmlId);
}
OnSourceLoaded?.Invoke(this, EventArgs.Empty);
OnStatusChanged?.Invoke(this, EventArgs.Empty);
}
private void OnHtmlStatusPlayChanged(object sender, EventArgs e)
......@@ -368,9 +389,27 @@ internal partial class HtmlMediaPlayer : Border
OnStatusChanged?.Invoke(this, EventArgs.Empty);
}
private void OnHtmlSourceSuspend(object sender, HtmlCustomEventArgs e)
{
if (this.Log().IsEnabled(Uno.Foundation.Logging.LogLevel.Debug))
{
this.Log().Error($"{_activeElementName} source Suspend: [{Source}]");
}
if (_activeElement != null)
{
IsPause = NativeMethods.GetPaused(_activeElement.HtmlId);
}
if (IsPause)
{
IsPause = true;
_isPlaying = false;
OnStatusChanged?.Invoke(this, EventArgs.Empty);
}
}
private void OnHtmlSourceFailed(object sender, HtmlCustomEventArgs e)
{
TimeUpdated += OnHtmlTimeUpdated;
TimeUpdated -= OnHtmlTimeUpdated;
if (_activeElement != null)
{
_activeElement.SetCssStyle("visibility", "hidden");
......@@ -379,6 +418,7 @@ internal partial class HtmlMediaPlayer : Border
{
this.Log().Error($"{_activeElementName} source failed: [{Source}]");
}
IsPause = true;
OnSourceFailed?.Invoke(this, e.Detail);
}
......
......@@ -3,6 +3,7 @@ declare namespace Uno.UI.Media {
static videoWidth(htmlId: number): number;
static videoHeight(htmlId: number): number;
static getCurrentPosition(htmlId: number): number;
static getPaused(htmlId: number): number;
static setCurrentPosition(htmlId: number, currentTime: number): void;
static setAttribute(htmlId: number, name: string, value: string): void;
static removeAttribute(htmlId: number, name: string): void;
......
......@@ -15,6 +15,9 @@ var Uno;
static getCurrentPosition(htmlId) {
return document.getElementById(htmlId.toString()).currentTime;
}
static getPaused(htmlId) {
return document.getElementById(htmlId.toString()).paused;
}
static setCurrentPosition(htmlId, currentTime) {
document.getElementById(htmlId.toString()).currentTime = currentTime;
}
......
......@@ -13,6 +13,10 @@ namespace Uno.UI.Media {
return document.getElementById(htmlId.toString()).currentTime;
}
public static getPaused(htmlId: number): number {
return document.getElementById(htmlId.toString()).paused;
}
public static setCurrentPosition(htmlId: number, currentTime: number) {
document.getElementById(htmlId.toString()).currentTime = currentTime;
}
......
......@@ -320,7 +320,7 @@ namespace Windows.UI.Xaml.Controls
private void OnPointerExited(object sender, PointerRoutedEventArgs e)
{
_isScrubbing = false;
if (_wasPlaying)
if (_wasPlaying && _mediaPlayer != null && !_mediaPlayer.PlaybackSession.IsPlaying)
{
_mediaPlayer?.Play();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册