简体   繁体   English

如何在Xamarin中使用MediaPlayer获得跟踪持续时间

[英]How do I get duration of track using MediaPlayer in Xamarin

I would like to find the duration of an audio track using Xamarin C# Android. 我想找到使用Xamarin C#的Android音频轨道的持续时间。 I am using MediaPlayer to play my audio files. 我正在使用MediaPlayer播放音频文件。

Most other answers on SO and the official documentation point me to a simple method MediaPlayer.GetDuration() in the MediaPlayer class 关于SO和官方文档的大多数其他答案都将我指向MediaPlayer类中的简单方法MediaPlayer.GetDuration()

However, intellisense is not bringing this up as an option. 但是,intellisense并未将此作为选项。 Get Track Info appears which might be useful though? 出现“获取跟踪信息”可能有用吗?

在哪里得到持续?

Full code below 完整代码如下

public void StartAudio()
{
    if (player == null)
    {
        player = new MediaPlayer();
        player.Completion += PlayNextTrack;              
    }
    else
    {
        player.Reset();
    }

    player.Looping = false;
    AssetFileDescriptor descriptor = Assets.OpenFd(currentTrack.FileName.ToString());
    player.SetDataSource(descriptor.FileDescriptor,descriptor.StartOffset,descriptor.Length);         
    player.Prepare();
    player.Start();

    if (currentTrack.TrackType == TrackType.Pause)
    {
        int timerLength = 0;
        int duration = player.GetDuration();
        timerLength = duration - 10; // or something...
        StartAlarmClockTimer(timerLength); // Special Case. Attach a countdown timer to this track
    }
    Toast.MakeText(this, "Playing Track " + currentTrack.TrackNumber.ToString(), ToastLength.Short).Show();               
}

Xamarin typically renames Java style Get methods as standard C# Properties Xamarin通常将Java样式的Get方法重命名为标准C#属性

So GetDuration() in Java becomes Duration in C# 因此Java中的GetDuration()在C#中成为Duration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM