Media player
<UserControl x:Class='SilverlightApplication3.MainPage'
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
mc:Ignorable='d'
d:DesignWidth='640'
d:DesignHeight='480'>
<Grid Margin="20" Background="White" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<MediaElement x:Name="media" AutoPlay="False" Source="test.mp3"
MediaOpened="media_MediaOpened" MediaFailed="media_MediaFailed"
MediaEnded="media_MediaEnded" CurrentStateChanged="media_CurrentStateChanged"></MediaElement>
<StackPanel Orientation="Horizontal">
<Button Click="cmdPlay_Click" Padding="5" Margin="1" Content="Play"></Button>
<Button Click="cmdStop_Click" Padding="5" Margin="1" Content="Stop"></Button>
<Button Click="cmdPause_Click" Padding="5" Margin="1" Content="Pause"></Button>
</StackPanel>
<Grid Grid.Row="1" Margin="0,10,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Margin="5" Text="Volume: "></TextBlock>
<Slider Grid.Column="1" Minimum="0" Maximum="1" x:Name="sliderVolume" Value="0.5"
ValueChanged="sliderVolume_ValueChanged" ></Slider>
<TextBlock Grid.Row="1" Margin="5" Text="Balance: "></TextBlock>
<Slider Grid.Row="1" Grid.Column="1" Minimum="-1" Maximum="1" x:Name="sliderBalance"
ValueChanged="sliderBalance_ValueChanged" >
</Slider>
<TextBlock Grid.Row="2" Margin="5" Text="Seek To: "></TextBlock>
<Slider Minimum="0" Grid.Column="1" Grid.Row="2" x:Name="sliderPosition"
ValueChanged="sliderPosition_ValueChanged" ></Slider>
<Slider Minimum="0" Grid.Column="1" Grid.Row="2" IsHitTestVisible="False" x:Name="sliderPositionBackground"
Opacity="0.5">
</Slider>
</Grid>
<CheckBox Grid.Row="2" Margin="5,5,5,0" Content="Loop" x:Name="chkLoop"></CheckBox>
<CheckBox Grid.Row="3" Margin="5,0" Content="Mute" x:Name="chkMute" Click="chkMute_Click"></CheckBox>
<TextBlock Grid.Row="4" Margin="10" HorizontalAlignment="Center" x:Name="lblStatus"></TextBlock>
</Grid>
</UserControl>
//File: Page.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace SilverlightApplication3
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
timer.Interval = TimeSpan.FromSeconds(0.1);
timer.Tick += timer_Tick;
}
private void timer_Tick(object sender, EventArgs e)
{
lblStatus.Text = media.Position.ToString().TrimEnd(new char[]{'0'});
sliderPositionBackground.Value = media.Position.TotalSeconds;
}
private DispatcherTimer timer = new DispatcherTimer();
private void cmdPlay_Click(object sender, RoutedEventArgs e)
{
media.Stop();
media.Play();
timer.Start();
}
private void cmdPause_Click(object sender, RoutedEventArgs e)
{
media.Pause();
timer.Stop();
}
private void cmdStop_Click(object sender, RoutedEventArgs e)
{
media.Stop();
timer.Stop();
}
private void media_MediaOpened(object sender, RoutedEventArgs e)
{
sliderPosition.Maximum = media.NaturalDuration.TimeSpan.TotalSeconds;
}
private void sliderPosition_ValueChanged(object sender, RoutedEventArgs e)
{
media.Pause();
media.Position = TimeSpan.FromSeconds(sliderPosition.Value);
media.Play();
}
private void sliderBalance_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
media.Balance = sliderBalance.Value;
}
private void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (media != null) media.Volume = sliderVolume.Value;
}
private void chkMute_Click(object sender, RoutedEventArgs e)
{
media.IsMuted = (bool)chkMute.IsChecked;
}
private void media_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
lblStatus.Text = e.ErrorException.Message;
}
private void media_MediaEnded(object sender, RoutedEventArgs e)
{
if ((bool)chkLoop.IsChecked)
{
media.Position = TimeSpan.Zero;
media.Play();
}
else
{
timer.Stop();
}
}
private void media_CurrentStateChanged(object sender, RoutedEventArgs e)
{
lblStatus.Text = media.CurrentState.ToString();
}
}
}
Related examples in the same category