I am building a wp8 app which could play poems line by line displayed in listbox.
Say I have 20 lines of Poems showing as 20 items in listbox. I have 20 mp3 files (one for each line), I used BackgroundAudioPlayer to play individual files and select each item (poem) in listbox. During playback of these mp3 files, there were delays of 1-3 seconds, so I found the full version of these mp3 files (20 files merged into one), reading poems line by line.
I am playing this mp3 file using BackgroundAudioPlayer. Playing as one file, I cannot select each item in listbox as I don't know the line number by playing one mp3 file.
Now what I want is to store the duration of 20 files in seconds, without storing and playing. The files are located in a link (http:123.com/1.mp3, /2.mp3, ...)
I used AudioTrack with absolute path and then used duration property, but it always returns zero. How can I get the duration of an mp3 file without playing them?
Edited: In the background Agent I did below:
protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
{
switch (playState)
{
case PlayState.TrackReady:
TrackDuration tr = DataSource.Connection.Table<TrackDuration>().SingleOrDefault();
if (tr.IsGetTrackDuration)
{
if (player.Track != null)
{
player.Position = TimeSpan.FromSeconds(player.Track.Duration.TotalSeconds);
Recitation r = DataSource.Connection.Query<Recitation>("select * from Recitation where ID = " + player.Track.Title).SingleOrDefault();
r.AyaDuration = player.Track.Duration.TotalSeconds;
DataSource.saveRecitionDownloaded(r);
PlayNextTrack(player);
}
}
break;
}
NotifyComplete();
}
protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
{
switch (action)
{
case UserAction.Play:
PlayTrack(player);
break;
case UserAction.Seek:
if (null!=player.Track)
player.Position = (TimeSpan)param;
break;
}
NotifyComplete();
}
private void PlayNextTrack(BackgroundAudioPlayer player)
{
if (++currentTrackNumber >= audioTrack.Count)
return;
PlayTrack(player);
}
private void PlayTrack(BackgroundAudioPlayer player)
{
//If No Track then load Track
if (audioTrack.Count == 0)
{
//Load Tracks and Add them into Track Play List
}
if ((player.Track == null) || (player.Track.Title != audioTrack[currentTrackNumber].Title))
player.Track = audioTrack[currentTrackNumber];
if ((player.Track != null) && (player.PlayerState != PlayState.Playing))
player.Play();
}
The question is It plays some of the files which I don't want. (tr.IsGetTrackDuration) is always true, but I don't know why it plays some?
To determine the file size of an audio file, we have to multiply the bit rate of the audio by its duration in seconds. As a result, we get file size values in terms of kilobits and megabits.
To select a certain portion of the audio file, click and drag your mouse from the beginning to the end of the audio you want to keep. This selects that portion of audio, allowing you to edit it. Click the scissors icon from the toolbar to "Cut" or delete the region of audio you've selected.
MusicProperties class has a property for the duration
MusicProperties musicProperties = await file.Properties.GetMusicPropertiesAsync();
outputText.AppendLine("Duration: " + musicProperties.Duration);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With