Search code examples
xamarin.formsplugin.media.crossmedia

How to Play MP3 file from certian position in Xamarin.Forms using CrossMediaManager


I am downloading files to the local storage in my Xamarin Forms application. I am using CrossMediaManager to play those stored local mp3 files. The problem is the player doesn't start playing audio from certain position. It always starts playing form zero.

I am using below code but didn't help. I have also tried to queue media files and play them but didn't help.

if (Helper.FileExists(filePath))
   {
      var position = TimeSpan.FromSeconds(AyasAudios[AyaNumber].StartingPoint);
      Debug.WriteLine("starting point: " + AyasAudios[AyaNumber].StartingPoint + " position: " + position);
      await CrossMediaManager.Current.Play(new FileInfo(filePath));
      await CrossMediaManager.Current.SeekTo(position);
   }

Solution

  • That can not invoke CrossMediaManager.Current.SeekTo immediately after CrossMediaManager.Current.Play in CrossMediaManager .

    That means we should better invoke SeekTo after player is IsPlaying status .

    Therefore can separate them to invoke them as follow :

    private async void Button_Clicked_Play(object sender, EventArgs e)
    {
        CrossMediaManager.Current.Play("https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3");
    }
    
    private async void Button_Clicked_SeekTo(object sender, EventArgs e)
    {
        if (CrossMediaManager.Current.IsPlaying())
        {
            await CrossMediaManager.Current.SeekTo(TimeSpan.FromSeconds(60));
        }
    }