如何获得mp3曲目的持续时间? [英] How to get the duration of mp3 track?

查看:58
本文介绍了如何获得mp3曲目的持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to ask how can I get length of my audio file in app.

我正在加载这样的曲目

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();

但是我不知道如何获得曲目的持续时间?

but I don't know how to get the duration of track?

推荐答案

有两种获取曲目持续时间的方法:

There are two ways to get the duration of the track:

  • first is to read it directly from file's MusicProperties by using GetMusicPropertiesAsync():
   var audioFile = await mp3FilesFolder.GetFileAsync("sound.mp3");
   MusicProperties properties = await audioFile.Properties.GetMusicPropertiesAsync();
   TimeSpan myTrackDuration = properties.Duration;

  • 第二个选项是从 MediaElement BackgroundMediaPlayer
  • 获取其 NaturalDuration

    这篇关于如何获得mp3曲目的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆