how to showing time elapsed in a media element in silverlight?
ho开发者_Python百科w can i showing time elapsed of a video file who is playing in a MediaElement control in silverlight?
There is a Position
property on the MediaElement
. This video and source code should help you out http://www.silverlight.net/learn/quickstarts/audioandvideo/
Finally i resolve my problem with this way: i use a Textblock control and bind text property to Position property of MediaElement control, then i use a IvalueConverter to show appropriate Time in TextBlock:
<TextBlock MinWidth="40" Text="{Binding ElementName=myMediaElement, Path=Position, Converter={StaticResource TimeConverterFormatter}}"/>
TimeConverterFormatter is a class for convert TimeSpan value to short time format. because position property in MediaElement show a Timespan value like to "0:00:00:00.0000000" and i want elapsed time value like to this: "00:00"
public class TimeConverterFormatter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.ToString().Length == 16)
return value.ToString().Substring(3, 2) + ":" + value.ToString().Substring(6, 2);
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
I realise that this is an old question now, but in WPF you can just use a normal StringFormat
to do what you want:
<TextBlock Text="{Binding Position, ElementName=myMediaElement,
StringFormat={}{0:hh}:{0:mm}, FallbackValue=00:00, Mode=OneWay}" />
I couldn't work out whether you wanted hours and minutes or minutes and seconds, so here is the latter:
<TextBlock Text="{Binding Position, ElementName=myMediaElement,
StringFormat={}{0:mm}:{0:ss}, FallbackValue=00:00, Mode=OneWay}" />
I can't guarantee that it will work in Silverlight though.
Not sure about Silverlight but this should work to show mm:ss.
lblLength.Content = String.Format("{0:mm}:{0:ss}", mediaElement.Position);
精彩评论