Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Added Speed Property to mediaelement #1570

Merged
merged 8 commits into from
Aug 11, 2021
63 changes: 45 additions & 18 deletions samples/XCT.Sample/Pages/Views/MediaElementPage.xaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<pages:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.MediaElementPage">
<?xml version="1.0" encoding="UTF-8" ?>
<pages:BasePage
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.MediaElementPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
<pages:BasePage.Resources>
<xct:TimeSpanToDoubleConverter x:Key="TimeSpanConverter"/>
<xct:TimeSpanToDoubleConverter x:Key="TimeSpanConverter" />
</pages:BasePage.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<xct:MediaElement
x:Name="mediaElement"
Source="https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4"
ShowsPlaybackControls="True"
MediaOpened="OnMediaOpened"
MediaFailed="OnMediaFailed"
MediaEnded="OnMediaEnded"
HorizontalOptions="Fill"
SeekCompleted="OnSeekCompleted" />
<Slider Grid.Row="1" BindingContext="{x:Reference mediaElement}" Value="{Binding Position, Converter={StaticResource TimeSpanConverter}}" Maximum="{Binding Duration, Converter={StaticResource TimeSpanConverter}}"/>
<Button Grid.Row="2" Text="Reset Source (Set Null)" Clicked="OnResetClicked" />
MediaEnded="OnMediaEnded"
MediaFailed="OnMediaFailed"
MediaOpened="OnMediaOpened"
SeekCompleted="OnSeekCompleted"
ShowsPlaybackControls="True"
Source="https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4" />
<Slider
Grid.Row="1"
BindingContext="{x:Reference mediaElement}"
Maximum="{Binding Duration, Converter={StaticResource TimeSpanConverter}}"
Value="{Binding Position, Converter={StaticResource TimeSpanConverter}}" />
<Button
Grid.Row="2"
Clicked="OnResetClicked"
Text="Reset Source (Set Null)" />
<Label Grid.Row="3" Margin="20,10">
<Label.FormattedText>
<FormattedString>
<Span Text="Speed:" />
<Span Text="{Binding Source={x:Reference mediaElement}, Path=Speed}" />
</FormattedString>
</Label.FormattedText>
</Label>
<Slider
x:Name="MainSlider"
Grid.Row="4"
Margin="20"
DragCompleted="Slider_DragCompleted"
Maximum="2"
Minimum=".5"
ThumbColor="Blue"
Value="{Binding Source={x:Reference mediaElement}, Path=Speed}" />
</Grid>
</pages:BasePage>
6 changes: 6 additions & 0 deletions samples/XCT.Sample/Pages/Views/MediaElementPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ public partial class MediaElementPage
void OnSeekCompleted(object? sender, EventArgs e) => Console.WriteLine("Seek completed.");

void OnResetClicked(object? sender, EventArgs e) => mediaElement.Source = null;


private void Slider_DragCompleted(object sender, EventArgs e)
{
mediaElement.Speed = MainSlider.Value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ protected virtual void OnElementPropertyChanged(object? sender, PropertyChangedE
case nameof(MediaElement.Volume):
UpdateVolume();
break;
case nameof(MediaElement.Speed):
UpdateSpeed();
break;
}

ElementPropertyChanged?.Invoke(this, e);
Expand Down Expand Up @@ -372,6 +375,15 @@ protected void UpdateVolume()

mediaPlayer?.SetVolume((float)MediaElement.Volume, (float)MediaElement.Volume);
}
protected void UpdateSpeed()
{
if (MediaElement == null || mediaPlayer == null)
return;

var playbackParams = new PlaybackParams();
playbackParams.SetSpeed((float)MediaElement.Speed);
mediaPlayer.PlaybackParams = playbackParams;
}

protected string ResolveMsAppDataUri(Uri uri)
{
Expand Down Expand Up @@ -411,6 +423,7 @@ void MediaPlayer.IOnPreparedListener.OnPrepared(MediaPlayer? mp)

mediaPlayer = mp;
UpdateVolume();
UpdateSpeed();
mp.Looping = MediaElement.IsLooping;
mp.SeekTo(0);

Expand Down Expand Up @@ -546,4 +559,4 @@ void OnMpBufferingUpdate(object? sender, MediaPlayer.BufferingUpdateEventArgs e)
Controller.BufferingProgress = e.Percent / 100f;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public class MediaElement : View, IMediaElementController

public static readonly BindableProperty VolumeProperty =
BindableProperty.Create(nameof(Volume), typeof(double), typeof(MediaElement), 1.0, BindingMode.TwoWay, new BindableProperty.ValidateValueDelegate(ValidateVolume));

public static readonly BindableProperty SpeedProperty =
BindableProperty.Create(nameof(Speed), typeof(double), typeof(MediaElement), 1.0, BindingMode.OneWay);

public Aspect Aspect
{
Expand Down Expand Up @@ -117,6 +120,11 @@ public double Volume
get => (double)GetValue(VolumeProperty);
set => SetValue(VolumeProperty, value);
}
public double Speed
{
get => (double)GetValue(SpeedProperty);
set => SetValue(SpeedProperty, value);
}

internal event EventHandler<SeekRequested>? SeekRequested;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected virtual void ObserveRate(NSObservedChange e)
Controller.CurrentState = MediaElementState.Paused;
break;

case 1.0f:
default:
Controller.CurrentState = MediaElementState.Playing;
break;
}
Expand Down Expand Up @@ -177,7 +177,6 @@ protected void ObserveStatus(NSObservedChange e)

case AVPlayerStatus.ReadyToPlay:
var duration = avPlayerViewController.Player.CurrentItem.Duration;

if (duration.IsIndefinite)
Controller.Duration = TimeSpan.Zero;
else
Expand Down Expand Up @@ -258,6 +257,9 @@ protected override void OnElementPropertyChanged(object? sender, System.Componen
case nameof(ToolKitMediaElement.Volume):
UpdateVolume();
break;
case nameof(ToolKitMediaElement.Speed):
UpdateSpeed();
break;
}
}

Expand Down Expand Up @@ -298,6 +300,7 @@ protected virtual void Play()
{
avPlayerViewController.Player.Play();
Controller.CurrentState = MediaElementState.Playing;
UpdateSpeed();
}

if (Element.KeepScreenOn)
Expand All @@ -309,6 +312,11 @@ void UpdateVolume()
if (avPlayerViewController.Player != null)
avPlayerViewController.Player.Volume = (float)Element.Volume;
}
void UpdateSpeed()
{
if (avPlayerViewController.Player != null)
avPlayerViewController.Player.Rate = (float)Element.Speed;
}

void MediaElementStateRequested(object? sender, StateRequested e)
{
Expand Down