提交 9f0a5076 编写于 作者: A agneszitte 提交者: Jerome Laban

fix: Properly display PosterSource for MediaPlayerElement on all platforms

上级 2eb2b282
......@@ -33,8 +33,6 @@ public partial class GtkMediaPlayer : FrameworkElement
private double _playbackRate;
private Rect _transportControlsBounds;
private Windows.UI.Xaml.Media.Stretch _stretch = Windows.UI.Xaml.Media.Stretch.Uniform;
private readonly ImmutableArray<string> audioTagAllowedFormats = ImmutableArray.Create(".MP3", ".WAV");
private readonly ImmutableArray<string> videoTagAllowedFormats = ImmutableArray.Create(".MP4", ".WEBM", ".OGG");
private readonly MediaPlayerPresenter _owner;
public GtkMediaPlayer(MediaPlayerPresenter owner)
......@@ -77,10 +75,10 @@ public partial class GtkMediaPlayer : FrameworkElement
public double VideoRatio { get; set; }
public bool IsVideo
=> videoTagAllowedFormats.Contains(Path.GetExtension(Source), StringComparer.OrdinalIgnoreCase);
=> _mediaPlayer?.Media?.Tracks?.Any(x => x.TrackType == TrackType.Video) == true;
public bool IsAudio
=> audioTagAllowedFormats.Contains(Path.GetExtension(Source), StringComparer.OrdinalIgnoreCase);
=> _mediaPlayer?.Media?.Tracks?.Any(x => x.TrackType == TrackType.Video) == true;
public void Play()
{
......
......@@ -327,6 +327,7 @@ public partial class GtkMediaPlayer
media.Parse(MediaParseOptions.ParseNetwork);
_mediaPlayer.Media = media;
AddMediaEvents();
Duration = (double)(_videoView?.MediaPlayer?.Media?.Duration / 1000 ?? 0);
OnSourceLoaded?.Invoke(this, EventArgs.Empty);
UpdateVideoStretch();
......@@ -431,11 +432,6 @@ public partial class GtkMediaPlayer
media.MetaChanged += OnStaticMetaChanged;
media.StateChanged += OnStaticStateChanged;
media.ParsedChanged += OnStaticParsedChanged;
Duration = (double)(_videoView?.MediaPlayer?.Media?.Duration / 1000 ?? 0);
OnSourceLoaded?.Invoke(this, EventArgs.Empty);
UpdateVideoStretch();
}
else
{
......@@ -587,11 +583,6 @@ public partial class GtkMediaPlayer
}
}
private void OnMediaPlayerTimeChangeIsMediaParse(object? sender, MediaPlayerTimeChangedEventArgs el)
{
AddMediaEvents();
}
private void OnEndReached()
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
......
......@@ -193,7 +193,6 @@ public partial class MediaPlayerExtension : IMediaPlayerExtension
throw new InvalidOperationException("Unsupported media source type");
}
ApplyVideoSource();
Events?.RaiseMediaOpened();
Events?.RaiseSourceChanged();
// Set the player back to the paused state, so that the
......@@ -337,6 +336,8 @@ public partial class MediaPlayerExtension : IMediaPlayerExtension
public double AudioBalance { get; set; }
public bool? IsVideo { get; set; }
public void SetTransportControlsBounds(Rect bounds)
{
_player?.SetTransportControlsBounds(bounds);
......
......@@ -37,6 +37,8 @@ public partial class MediaPlayerExtension : IMediaPlayerExtension
{
if (_player is not null)
{
IsVideo = _player.IsVideo;
if (mp.IsVideo && Events is not null)
{
Events?.RaiseVideoRatioChanged(global::System.Math.Max(1, (double)mp.VideoRatio));
......@@ -66,6 +68,11 @@ public partial class MediaPlayerExtension : IMediaPlayerExtension
}
}
}
if (Events is not null)
{
Events?.RaiseMediaOpened();
}
}
public void OnError(object? sender, object what)
......
......@@ -147,6 +147,8 @@ public partial class MediaPlayerExtension : IMediaPlayerExtension
public bool CanSeek
=> true;
public bool? IsVideo { get; set; }
public MediaPlayerAudioDeviceType AudioDeviceType { get; set; }
public MediaPlayerAudioCategory AudioCategory { get; set; }
......@@ -301,7 +303,6 @@ public partial class MediaPlayerExtension : IMediaPlayerExtension
}
ApplyVideoSource();
Events?.RaiseMediaOpened();
Events?.RaiseSourceChanged();
}
catch (global::System.Exception ex)
......@@ -480,6 +481,8 @@ public partial class MediaPlayerExtension : IMediaPlayerExtension
NaturalDuration = TimeSpan.FromSeconds(_player.Duration);
IsVideo = _player.IsVideo;
if (mp.IsVideo && Events is not null)
{
try
......@@ -509,6 +512,11 @@ public partial class MediaPlayerExtension : IMediaPlayerExtension
_isPlayerPrepared = true;
}
if (Events is not null)
{
Events?.RaiseMediaOpened();
}
}
void OnError(object? sender, object what)
......
......@@ -3294,6 +3294,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MediaPlayerElement\MediaPlayerElement_Mp3_Extension.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MediaPlayerElement\MediaPlayerElement_Mov_Extension.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
......@@ -7150,6 +7154,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MediaPlayerElement\MediaPlayerElement_Mkv_Extension.xaml.cs">
<DependentUpon>MediaPlayerElement_Mkv_Extension.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MediaPlayerElement\MediaPlayerElement_Mp3_Extension.xaml.cs">
<DependentUpon>MediaPlayerElement_Mp3_Extension.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MediaPlayerElement\MediaPlayerElement_Mov_Extension.xaml.cs">
<DependentUpon>MediaPlayerElement_Mov_Extension.xaml</DependentUpon>
</Compile>
......
<Page x:Class="UITests.Shared.Windows_UI_Xaml_Controls.MediaPlayerElement.MediaPlayerElement_Mp3_Extension"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<MediaPlayerElement Source="https://uno-assets.platform.uno/tests/audio/Getting_Started_with_Uno_Platform_and_Visual_Studio_Code.mp3"
PosterSource="https://uno-assets.platform.uno/tests/thumbnails/Getting_Started_with_Uno_Platform_and_Visual_Studio_Code.png"
AreTransportControlsEnabled="True"
AutoPlay="True" />
</Page>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace UITests.Shared.Windows_UI_Xaml_Controls.MediaPlayerElement
{
[SampleControlInfo("MediaPlayerElement", "Using .mp3 (Audio only)", description: "MediaPlayerElement test using .mp3 (Audio only) with PosterSource")]
public sealed partial class MediaPlayerElement_Mp3_Extension : Page
{
public MediaPlayerElement_Mp3_Extension()
{
this.InitializeComponent();
}
}
}
......@@ -249,7 +249,11 @@ namespace Windows.UI.Xaml.Controls
{
_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
TogglePosterImage(false);
// Always show the poster source for audio content.
if (session.IsVideo)
{
TogglePosterImage(false);
}
});
}
......@@ -326,11 +330,15 @@ namespace Windows.UI.Xaml.Controls
&& AutoPlay)
{
MediaPlayer.Play();
TogglePosterImage(false);
}
}
}
// The PosterSource is displayed in the following situations:
// - When a valid source is not set.For example, Source is not set, Source was set to Null, or the source is invalid (as is the case when a MediaFailed event fires).
// - While media is loading. For example, a valid source is set, but the MediaOpened event has not fired yet.
// - When media is streaming to another device.
// - When the media is audio only.
private void TogglePosterImage(bool showPoster)
{
if (PosterSource != null)
......@@ -365,10 +373,8 @@ namespace Windows.UI.Xaml.Controls
_mediaPlayerPresenter?.ApplyStretch();
}
if (!IsLoaded && MediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.None)
{
TogglePosterImage(true);
}
// For video content, show the poster source until it is ready to be displayed.
TogglePosterImage(true);
if (!_isTransportControlsBound)
{
......
......@@ -89,6 +89,11 @@ namespace Uno.Media.Playback
/// </summary>
TimeSpan Position { get; set; }
/// <summary>
/// Determines if the current media content is video
/// </summary>
bool? IsVideo { get; }
/// <summary>
/// Sets the transport controls bounds so that video can be displayed around controls
/// </summary>
......
......@@ -155,8 +155,6 @@ namespace Windows.Media.Playback
SetVideoSource(uri);
_player.PrepareAsync();
MediaOpened?.Invoke(this, null);
}
catch (global::System.Exception ex)
{
......@@ -270,13 +268,17 @@ namespace Windows.Media.Playback
public void OnPrepared(AndroidMediaPlayer mp)
{
PlaybackSession.NaturalDuration = TimeSpan.FromMilliseconds(_player.Duration);
if (mp is not null)
{
PlaybackSession.NaturalDuration = TimeSpan.FromMilliseconds(_player.Duration);
VideoRatioChanged?.Invoke(this, (double)mp.VideoWidth / global::System.Math.Max(mp.VideoHeight, 1));
VideoRatioChanged?.Invoke(this, (double)mp.VideoWidth / global::System.Math.Max(mp.VideoHeight, 1));
if (PlaybackSession.PlaybackState == MediaPlaybackState.Opening)
{
UpdateVideoStretch(_currentStretch);
IsVideo = mp.GetTrackInfo()?.Any(x => x.TrackType == MediaTrackType.Video) == true;
if (PlaybackSession.PlaybackState == MediaPlaybackState.Opening)
{
UpdateVideoStretch(_currentStretch);
if (_isPlayRequested)
{
......@@ -293,7 +295,10 @@ namespace Windows.Media.Playback
}
}
_isPlayerPrepared = true;
_isPlayerPrepared = true;
MediaOpened?.Invoke(this, null);
}
}
public bool OnError(AndroidMediaPlayer mp, MediaError what, int extra)
......@@ -383,6 +388,8 @@ namespace Windows.Media.Playback
}
}
public bool IsVideo { get; set; }
internal void UpdateVideoStretch(VideoStretch stretch)
{
_currentStretch = stretch;
......
......@@ -246,9 +246,6 @@ namespace Windows.Media.Playback
// Adapt pitch to prevent "metallic echo" when changing playback rate
_player.CurrentItem.AudioTimePitchAlgorithm = AVAudioTimePitchAlgorithm.TimeDomain;
MediaOpened?.Invoke(this, null);
}
catch (Exception ex)
{
......@@ -358,6 +355,8 @@ namespace Windows.Media.Playback
{
if (_player?.CurrentItem != null)
{
IsVideo = _player.CurrentItem.Tracks?.Any(x => x.AssetTrack.FormatDescriptions.Any(x => x.MediaType == CMMediaType.Video)) == true;
if (_player.CurrentItem.Status == AVPlayerItemStatus.Failed || _player.Status == AVPlayerStatus.Failed)
{
OnMediaFailed();
......@@ -377,6 +376,11 @@ namespace Windows.Media.Playback
_player.Play();
}
}
if (_player.Status == AVPlayerStatus.ReadyToPlay)
{
MediaOpened?.Invoke(this, null);
}
}
}
......@@ -473,6 +477,8 @@ namespace Windows.Media.Playback
}
}
public bool IsVideo { get; set; }
private void OnSeekCompleted(bool finished)
{
if (finished)
......
......@@ -160,6 +160,8 @@ namespace Windows.Media.Playback
}
}
public bool IsVideo => _extension?.IsVideo ?? false;
public void SetUriSource(global::System.Uri value)
=> _extension?.SetUriSource(value);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册