Reputation: 197
I would like to ask how can I get length of my audio file in app.
I'm loading track like this
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?
Upvotes: 5
Views: 10623
Reputation: 29792
There are two ways to get the duration of the track:
var audioFile = await mp3FilesFolder.GetFileAsync("sound.mp3");
MusicProperties properties = await audioFile.Properties.GetMusicPropertiesAsync();
TimeSpan myTrackDuration = properties.Duration;
Upvotes: 9
Reputation: 151710
Assuming mediaPlayer
is a MediaElement
, you can get the duration using MediaElement.NaturalDuration
.
Upvotes: 2