That is because Media is not opened and hence the progress bar is not aware of the maximum value. Try this...
Use the following events for MediaOpened, and MouseLeftButtonUp for your progress bar or slider. I tried it, and it works just fine.
public partial class AudioPage : Page
{
TimeSpan _position;
DispatcherTimer _timer = new DispatcherTimer();
public AudioPage()
{
InitializeComponent();
_timer.Interval = TimeSpan.FromMilliseconds(1000);
_timer.Tick += new EventHandler(ticktock);
_timer.Start();
}
void ticktock(object sender, EventArgs e)
{
sliderSeek.Value = media.Position.TotalSeconds;
}
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Play_Click(object sender, RoutedEventArgs e)
{
media.Play();
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
media.Pause();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
media.Stop();
}
private void media_MediaOpened(object sender, RoutedEventArgs e)
{
_position = media.NaturalDuration.TimeSpan;
sliderSeek.Minimum = 0;
sliderSeek.Maximum = _position.TotalSeconds;
}
private void sliderSeek_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
int pos = Convert.ToInt32(sliderSeek.Value);
media.Position = new TimeSpan(0, 0, 0, pos, 0);
}
}
The associate XAMl is as follows...
<Grid x:Name="LayoutRoot" Background="#FFFFE8E8">
<Grid.RowDefinitions>
<RowDefinition Height="220*" />
<RowDefinition Height="75*" />
</Grid.RowDefinitions>
<MediaElement Height="189" HorizontalAlignment="Left" Margin="12,12,0,0" Name="media" VerticalAlignment="Top" Width="399" Source="anyfile.mp3" AutoPlay="True" MediaOpened="media_MediaOpened" />
<StackPanel Orientation="Horizontal" Grid.Row="1" Height="30" Margin="12,8,163,37">
<TextBlock Text="Volume" VerticalAlignment="Center"></TextBlock>
<Slider Margin="2" Maximum="1" Minimum="0" Width="102" Value="{Binding Path=Volume, Mode=TwoWay, ElementName=media}"></Slider>
<TextBlock Text="{Binding ElementName=media, Path=Volume, Mode=OneWay, StringFormat=0.00}" VerticalAlignment="Center" />
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1" Height="30" Margin="26,47,163,-2">
<TextBlock Text="Seek" VerticalAlignment="Center"></TextBlock>
<Slider Margin="2" Width="104" Name="sliderSeek" MouseLeftButtonUp="sliderSeek_MouseLeftButtonUp"></Slider>
<TextBlock Text="{Binding ElementName=sliderSeek, Path=Value, StringFormat=0}" VerticalAlignment="Center"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1" Height="30" Margin="266,8,12,37">
<Button Name="Play" Content="Play" Margin="2" Click="Play_Click" />
<Button Name="Pause" Content="Pause" Margin="2" Click="Pause_Click" />
<Button Name="Stop" Content="Stop" Margin="2" Click="Stop_Click" />
</StackPanel>
</Grid>