I’m utilizing the CommunityToolkit MediaElement in my .NET MAUI software to play audio. It really works high-quality on Android, however on iOS, if I play an audio file, let it full, after which attempt to restart the identical audio, it doesn’t play. Nevertheless, the slider progress updates on the UI, as if the audio is taking part in.
Code Implementation
XAML:
C# Code Play/Pause logic:
public async void PlayPauseTapped(object sender, EventArgs args)
{
strive
{
if (stopExistingAudio)
{
stopExistingAudio = false;
if (Utility.isAudioPlaying)
{
DRAudioPopUpPage.MymediaElement.Cease();
}
}
if (!string.IsNullOrWhiteSpace(mAudioURL))
{
if (!isPlaying)
{
isPlaying = true;
pause_icon.Supply = "ic_audio_pause_icon_xx.png";
MymediaElement.Play();
Utility.isAudioPlaying = true;
timer.Begin(); // Begin updating the slider
}
else
{
audiotimer_label.Textual content = FormatTime(MymediaElement.Period);
isPlaying = false;
MymediaElement.Pause();
pause_icon.Supply = "ic_audio_play_icon_xx.png";
Utility.isAudioPlaying = false;
timer.Cease(); // Cease updating the slider
}
}
}
catch (Exception ex)
{
Debug.WriteLine("iOSOnPlayPauseTappedException:>" + ex);
}
}
Slider Replace Logic
personal void UpdateSlider()
{
strive
{
if (MymediaElement != null && MymediaElement.Period != null)
{
audioSlider.Most = MymediaElement.Period.TotalSeconds;
audiotimer_label.Textual content =
FormatTime(TimeSpan.FromSeconds(MymediaElement.Place.TotalSeconds));
int totalSeconds = (int)MymediaElement.Period.TotalSeconds;
int currentSeconds = (int)MymediaElement.Place.TotalSeconds;
if (totalSeconds == currentSeconds)
{
isPlaying = false;
pause_icon.Supply = "ic_audio_play_icon_xx.png";
Utility.isAudioPlaying = false;
MymediaElement.Supply = mAudioURL; // Reset Supply
timer.Cease();
Debug.WriteLine("**accomplished**");
}
}
audioSlider.Worth = MymediaElement.Place.TotalSeconds;
}
catch (Exception ex)
{
Debug.WriteLine("UpdateSliderException:>" + ex);
}
}
Platform:
-
Android:
Works as anticipated. After audio completes, clicking play once more performs the audio from the start.
-
iOS:
After the primary playback, when the audio completes and I click on play once more, the UI updates (slider strikes), however there is no such thing as a sound.
Atmosphere:
.NET MAUI Model: .NET 8
.NET MAUI Neighborhood Toolkit Model: “6.1.0”
CommunityToolkit.Maui.MediaElement Model: “2.0.0”
How can I resolve this challenge?