【问题标题】:How to get the duration of mp3 track?如何获取 mp3 曲目的时长?
【发布时间】:2015-03-18 17:40:10
【问题描述】:

我想问一下如何在应用程序中获取我的音频文件的长度。

我正在加载这样的曲目

var installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

var resourcesFolder = await installFolder.GetFolderAsync("Resources");
var mp3FilesFolder = await resourcesFolder.GetFolderAsync("mp3Files");

var audioFile = await mp3FilesFolder.GetFileAsync("sound.mp3");
var stream = await audioFile.OpenAsync(Windows.Storage.FileAccessMode.Read);

mediaplayer.SetSource(stream, audioFile.ContentType);
mediaplayer.Play();

但我不知道如何获取曲目时长?

【问题讨论】:

    标签: c# audio windows-phone-8.1


    【解决方案1】:

    获取曲目时长的方法有两种:

       var audioFile = await mp3FilesFolder.GetFileAsync("sound.mp3");
       MusicProperties properties = await audioFile.Properties.GetMusicPropertiesAsync();
       TimeSpan myTrackDuration = properties.Duration;
    
    • 第二个选项是从 MediaElementBackgroundMediaPlayer 获取它的 NaturalDuration

    【讨论】:

    • 嗨,我总是将此时间跨度值设为 0(我尝试了上述两种方法)。有人遇到同样的问题吗?
    • @Noorul 您是否尝试过通过 MediaElement 获取持续时间?
    • 是的,我尝试了两种方式,这是我的代码 1) TimeSpan duration = mediaElement.NaturalDuration.TimeSpan;和 2)MusicProperties musicProp = await storageFile.Properties.GetMusicPropertiesAsync();持续时间 = musicProp.Duration;
    • @Noorul 使用 MediaElement - 你把代码放在哪里了?一旦打开文件(例如在打开的事件中),您是否已经这样做了?
    • 感谢您的回复,之前我是在分配媒体元素源后分配它。我尝试使用媒体元素打开事件。很高兴它有效,能够获得媒体长度。非常感谢。
    【解决方案2】:

    假设mediaPlayerMediaElement,您可以使用MediaElement.NaturalDuration 获取持续时间。

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多