0

In my .NET MAUI Application, I need to implement an audio progress slider similar to the one shown in the attached image.

Image:

enter image description here

The progress bar should:

  • Fill dynamically as the audio plays.

  • Pause the progress when the audio is paused

  • Sync with the current playback position of the audio.

I am using toolkit:MediaElement to play the audio. How can I achieve this functionality in .NET MAUI?

Environment:

.NET MAUI Version: .NET 8

.NET MAUI Community Toolkit Version: "6.1.0"

CommunityToolkit.Maui.MediaElement Version: "2.0.0"

Platform: Android and iOS

1
  • 1
    Great! Seems you have solved your question. Here is an official GitHub sample which uses a slider to control the MediaElement. You may refer to it. Commented Mar 5 at 8:58

2 Answers 2

1

Slider maximum value we need to set based on the audio file duration like below:

<Slider 
    x:Name="audioSlider" 
    Grid.Row="0"
    Minimum="0" 
    VerticalOptions="Center"
    HorizontalOptions="Fill" 
    ThumbColor="Transparent" 
    MaximumTrackColor="Gray" 
    MinimumTrackColor="White" 
    InputTransparent="True"
    Maximum="{Binding Source={x:Reference MymediaElement}, Path=Duration.TotalSeconds}"
    Value="{Binding Source={x:Reference MymediaElement}, Path=Position.TotalSeconds, Mode=OneWay}"> 
    <Slider.WidthRequest>
        <OnIdiom x:TypeArguments="x:Double">
            <OnIdiom.Phone>400</OnIdiom.Phone>
            <OnIdiom.Tablet>980</OnIdiom.Tablet>
            <OnIdiom.Desktop>400</OnIdiom.Desktop>
        </OnIdiom>
    </Slider.WidthRequest>
</Slider>

Then Initialize the Timer like below:

private IDispatcherTimer timer;
// Initialize Timer
timer = Application.Current.Dispatcher.CreateTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Tick += (s, e) => UpdateSlider();

When we start playing the audio update the slider like below:

private void UpdateSlider()
{
    try
    {
        if (MymediaElement != null && MymediaElement.Duration != null)
        {
            if (MymediaElement.Duration != null)
            {
                audioSlider.Maximum = MymediaElement.Duration.TotalSeconds;
            }
            audioSlider.Value = MymediaElement.Position.TotalSeconds;
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("DRAudioUpdateSliderException:>" + ex);
    }
}

Added the MediaElement from code behind like below:

public static MediaElement MymediaElement;

MymediaElement = new MediaElement();
MymediaElement.WidthRequest = 1;
MymediaElement.HeightRequest = 1;
MymediaElement.ShouldAutoPlay = false;
MymediaElement.Source = audioUrl;
myGrid.Add(MymediaElement);
Sign up to request clarification or add additional context in comments.

Comments

0

You should take a look at this tutorial. You can find this piece of code:

<StackLayout>
    <toolkit:MediaElement ShouldAutoPlay="False"
                          Source="{StaticResource AdvancedAsync}" />
    <Slider Maximum="1.0"
            Minimum="0.0"
            Value="{Binding Volume}"
            Rotation="270"
            WidthRequest="100" />
</StackLayout>

In this example, the Slider data binds its Value property to the Volume property of the MediaElement. This is possible because the Volume property uses a TwoWay binding. Therefore, changing the Value property will result in the Volume property changing.

You can check all the properties annouced by MediaElement. There you can find

Position: Describes the current progress through the media's playback time. This is a read-only, bindable property. If you want to set the Position use the SeekTo() method.

So you could follow the same approach as the Volume. But taking into account your Binded property for progress should use SeekTo()

When it comes to the progress bar styling, I recommend you take a look at Julian's Blog where he describes in detail how you should create a custom progress bar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.