Skip to content

Commit

Permalink
fix: Properly display PosterSource for MediaPlayerElement on all plat…
Browse files Browse the repository at this point in the history
…forms
  • Loading branch information
agneszitte authored and jeromelaban committed Jun 22, 2023
1 parent 2eb2b28 commit 9f0a507
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 33 deletions.
6 changes: 2 additions & 4 deletions src/AddIns/Uno.UI.MediaPlayer.Skia.Gtk/GTKMediaPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -77,10 +75,10 @@ public string Source
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()
{
Expand Down
11 changes: 1 addition & 10 deletions src/AddIns/Uno.UI.MediaPlayer.Skia.Gtk/GTKMediaPlayer.events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ private void UpdateMedia()
media.Parse(MediaParseOptions.ParseNetwork);
_mediaPlayer.Media = media;
AddMediaEvents();
Duration = (double)(_videoView?.MediaPlayer?.Media?.Duration / 1000 ?? 0);
OnSourceLoaded?.Invoke(this, EventArgs.Empty);

UpdateVideoStretch();
Expand Down Expand Up @@ -431,11 +432,6 @@ private void AddMediaEvents()
media.MetaChanged += OnStaticMetaChanged;
media.StateChanged += OnStaticStateChanged;
media.ParsedChanged += OnStaticParsedChanged;

Duration = (double)(_videoView?.MediaPlayer?.Media?.Duration / 1000 ?? 0);
OnSourceLoaded?.Invoke(this, EventArgs.Empty);

UpdateVideoStretch();
}
else
{
Expand Down Expand Up @@ -587,11 +583,6 @@ private void OnMediaPlayerTimeChange(object? sender, MediaPlayerTimeChangedEvent
}
}

private void OnMediaPlayerTimeChangeIsMediaParse(object? sender, MediaPlayerTimeChangedEventArgs el)
{
AddMediaEvents();
}

private void OnEndReached()
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public void InitializeSource()
throw new InvalidOperationException("Unsupported media source type");
}
ApplyVideoSource();
Events?.RaiseMediaOpened();
Events?.RaiseSourceChanged();

// Set the player back to the paused state, so that the
Expand Down Expand Up @@ -337,6 +336,8 @@ public TimeSpan TimelineControllerPositionOffset

public double AudioBalance { get; set; }

public bool? IsVideo { get; set; }

public void SetTransportControlsBounds(Rect bounds)
{
_player?.SetTransportControlsBounds(bounds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void OnPrepared(object? sender, object what)
{
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));
Expand Down Expand Up @@ -66,6 +68,11 @@ public void OnPrepared(object? sender, object what)
}
}
}

if (Events is not null)
{
Events?.RaiseMediaOpened();
}
}

public void OnError(object? sender, object what)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public bool CanPause
public bool CanSeek
=> true;

public bool? IsVideo { get; set; }

public MediaPlayerAudioDeviceType AudioDeviceType { get; set; }

public MediaPlayerAudioCategory AudioCategory { get; set; }
Expand Down Expand Up @@ -301,7 +303,6 @@ public void InitializeSource()
}

ApplyVideoSource();
Events?.RaiseMediaOpened();
Events?.RaiseSourceChanged();
}
catch (global::System.Exception ex)
Expand Down Expand Up @@ -480,6 +481,8 @@ public void OnPrepared(object? sender, object what)

NaturalDuration = TimeSpan.FromSeconds(_player.Duration);

IsVideo = _player.IsVideo;

if (mp.IsVideo && Events is not null)
{
try
Expand Down Expand Up @@ -509,6 +512,11 @@ public void OnPrepared(object? sender, object what)

_isPlayerPrepared = true;
}

if (Events is not null)
{
Events?.RaiseMediaOpened();
}
}

void OnError(object? sender, object what)
Expand Down
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ private void OnMediaOpened(Windows.Media.Playback.MediaPlayer session, object ar
{
_ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
TogglePosterImage(false);
// Always show the poster source for audio content.
if (session.IsVideo)
{
TogglePosterImage(false);
}
});
}

Expand Down Expand Up @@ -326,11 +330,15 @@ private protected override void OnLoaded()
&& 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)
Expand Down Expand Up @@ -365,10 +373,8 @@ protected override void OnApplyTemplate()
_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)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UWP/Media/Playback/IMediaPlayerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public interface IMediaPlayerExtension : IDisposable
/// </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>
Expand Down
23 changes: 15 additions & 8 deletions src/Uno.UWP/Media/Playback/MediaPlayer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ protected virtual void InitializeSource()
SetVideoSource(uri);

_player.PrepareAsync();

MediaOpened?.Invoke(this, null);
}
catch (global::System.Exception ex)
{
Expand Down Expand Up @@ -270,13 +268,17 @@ private void OnPlaying()

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)
{
Expand All @@ -293,7 +295,10 @@ public void OnPrepared(AndroidMediaPlayer mp)
}
}

_isPlayerPrepared = true;
_isPlayerPrepared = true;

MediaOpened?.Invoke(this, null);
}
}

public bool OnError(AndroidMediaPlayer mp, MediaError what, int extra)
Expand Down Expand Up @@ -383,6 +388,8 @@ public virtual TimeSpan Position
}
}

public bool IsVideo { get; set; }

internal void UpdateVideoStretch(VideoStretch stretch)
{
_currentStretch = stretch;
Expand Down
12 changes: 9 additions & 3 deletions src/Uno.UWP/Media/Playback/MediaPlayer.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,6 @@ protected virtual void InitializeSource()

// Adapt pitch to prevent "metallic echo" when changing playback rate
_player.CurrentItem.AudioTimePitchAlgorithm = AVAudioTimePitchAlgorithm.TimeDomain;

MediaOpened?.Invoke(this, null);

}
catch (Exception ex)
{
Expand Down Expand Up @@ -358,6 +355,8 @@ private void OnStatusChanged()
{
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();
Expand All @@ -377,6 +376,11 @@ private void OnStatusChanged()
_player.Play();
}
}

if (_player.Status == AVPlayerStatus.ReadyToPlay)
{
MediaOpened?.Invoke(this, null);
}
}
}

Expand Down Expand Up @@ -473,6 +477,8 @@ public TimeSpan Position
}
}

public bool IsVideo { get; set; }

private void OnSeekCompleted(bool finished)
{
if (finished)
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UWP/Media/Playback/MediaPlayer.others.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public TimeSpan Position
}
}

public bool IsVideo => _extension?.IsVideo ?? false;

public void SetUriSource(global::System.Uri value)
=> _extension?.SetUriSource(value);

Expand Down

0 comments on commit 9f0a507

Please sign in to comment.