1
votes

While I am Trying to get a duration of Audio its showing value of 0.

I am using MediaManager Plugin using xamarin forms How to get a duration?. Please guys help me.

Here it is mycode.How can I get a Duration of the song. Please Let me Know.

private void CircleButton_ClickedAsync(object sender,PlayingChangedEventArgs e)
{

             media = new MediaFile
            {
                Url=https://ia800605.us.archive.org/32/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3,
                Type =MediaFileType.Audio,
                Availability=ResourceAvailability.Remote,
                MetadataExtracted=false               
            };


            if (play == false)
            {
                play = true;

                await CrossMediaManager.Current.Play(media);              

                long lng;

                lng = media.Metadata.Duration;
                TimeSpan ts = new TimeSpan(lng).Duration();


                string getTime = lng.ToString();

                Debug.WriteLine("get Time ---",getTime);           

                _countdown.StartTime = TimeSpan.Zero;
                _countdown.EndTime = e.Duration;
                _countdown.IsRunning = true;

                _countdown.Start();

                _countdown.Ticked += OnCountdownTicked;
                _countdown.Completed += OnCountdownCompleted;
                img.Source = "Pause.png";
                play = true;

            }
            else
            {
                await CrossMediaManager.Current.Pause();
                img.Source = "Play.png";
                play = false;
            }
}

Here I am Using Playingchangedevet when user click the Button Then its play.

1
It would have been great if you added the relevant code - FreakyAli
using this line you can get the total duration of your current playing song int SongDuration => kMediaPlayer.Duration.TotalSeconds > 0 ? Convert.ToInt32(kMediaPlayer.Duration.TotalSeconds) : 0; - Prasanth
Hi @G.hakim i added that my code - Muthukumar c
Hi @Prasanth showing The Duration of the audio 00:00:00 - Muthukumar c
try this one e.Duration.Second instead of using lng = media.Metadata.Duration; - Prasanth

1 Answers

0
votes

While I am Trying to get a duration of Audio its showing value of 0.

Have a try this way to play audio,adding a PlayingChanged to get duration:

CrossMediaManager.Current.PlayingChanged += (sender, e) =>
{
    Device.BeginInvokeOnMainThread(() =>
    {
        ProgressBar.Progress = e.Progress;
        Duration.Text = "" + e.Duration.TotalSeconds / 1000 + " seconds";            
    });
};

And with a Button to start to play:

CrossMediaManager.Current.Play("https://ia800605.us.archive.org/32/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3", MediaFileType.Audio);

Out is : 270.242 seconds ==> 4:30