Windows 8.1 and Windows Phone 8.1 Universal Application – Media Player

Step 1

Download Visual Studio Community 2013 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2013 or if it has already been downloaded and installed then start the application.

Visual Studio Community 2013

Step 2

Once Visual Studio Community 2013 has started select File, then New, then Project… from the Menu.

vs-file-new-project

Step 3

From the New Project window select Visual C# then Store Apps from Installed Templates then select Blank App (Universal Apps) from the list, then type in a Name and select a Location to save to before selecting Ok to create the Project.

vs-new-project

Step 4

Once the Project is created from the Solution Explorer select the Project ending with .Shared

vs-shared

Step 5

Once done select from the Menu, Project, then Add New Item…

vs-add-new-item

Step 6

From the Add New Item window select Visual C# from Installed then select Code File from the list, then type in the Name as Shared.cs before selecting Add to add the file to the Project

vs-shared-code-file

Step 7

Once in the Code View for Shared.cs the following should be entered:

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

public class Shared
{
    public delegate void PlayingEvent();
    public event PlayingEvent Playing;
    
    private DispatcherTimer timer = new DispatcherTimer();

    public void Init()
    {
        timer.Tick += (object sender, object e) =>
        {
            if (Playing != null) Playing();
        };
    }

    public void Timer(bool enabled)
    {
        if (enabled) timer.Start(); else timer.Stop();
    }

    public void Go(ref MediaElement display, string value, KeyRoutedEventArgs args)
    {
        if (args.Key == Windows.System.VirtualKey.Enter)
        {
            try
            {
                display.Source = new Uri(value, UriKind.Absolute);
                display.Play();
            }
            catch
            {

            }           
        }
    }
}

It should then appear as such:

vs-shared-mediaplayer

Step 8

From the Solution Explorer select the MainPage.xaml from the Project ending with .Windows

vs-windows-mainpage

Step 9

Select from the Menu, View then Designer

vs-designer

Step 10

The Design View will be displayed along with the XAML View and in this above <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<Page.BottomAppBar>
	<CommandBar IsOpen="True" IsSticky="True">
		<AppBarButton Name="Play" Icon="Play" Label="Play" Click="Play_Click"/>
		<AppBarButton Icon="Stop" Label="Stop" Click="Stop_Click"/>
	</CommandBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<TextBox Grid.Row="0" Margin="20" Name="Value" InputScope="Url" KeyDown="Go_KeyDown"/>
	<Grid Grid.Row="1" Margin="5">
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="*"/>
			<ColumnDefinition Width="120"/>
		</Grid.ColumnDefinitions>
		<Slider Grid.Column="0" Padding="5" Minimum="0" Name="Position"/>
		<Slider Grid.Column="2" Padding="5" Minimum="0" Maximum="1" Value="0.5" Name="Volume" ValueChanged="Volume_ValueChanged"/>
	</Grid>
	<MediaElement Grid.Row="2" Name="Display" AutoPlay="True" MediaOpened="Display_MediaOpened" MediaEnded="Display_MediaEnded" CurrentStateChanged="Display_CurrentStateChanged"/>
</Grid>

It should appear as such:

vs-windows-xaml-mediaplayer

Step 11

Select from the Menu, View then Code

vs-code

Step 12

Once in the Code View below the public MainPage() { … } the following should be entered:

public Shared Shared = new Shared();

private void Play_Click(object sender, RoutedEventArgs e)
{
	if (Display.CurrentState == MediaElementState.Playing)
	{
		Display.Pause();
		Play.Icon = new SymbolIcon(Symbol.Play);
		Play.Label = "Play";
	}
	else
	{
		Display.Play();
		Play.Icon = new SymbolIcon(Symbol.Pause);
		Play.Label = "Pause";
	}
}

private void Stop_Click(object sender, RoutedEventArgs e)
{
	Display.Stop();
}

private void Go_KeyDown(object sender, KeyRoutedEventArgs e)
{
	Shared.Go(ref Display, Value.Text, e);
}

private void Volume_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
	if (Volume != null)
	{
		Display.Volume = (double)Volume.Value;
	}
}

private void Display_MediaOpened(object sender, RoutedEventArgs e)
{
	Position.Maximum = (int)Display.NaturalDuration.TimeSpan.TotalMilliseconds;
	Display.Play();
	Play.Icon = new SymbolIcon(Symbol.Pause);
	Play.Label = "Pause";
}

private void Display_MediaEnded(object sender, RoutedEventArgs e)
{
	Play.Icon = new SymbolIcon(Symbol.Play);
	Play.Label = "Play";
	Display.Stop();
	Position.Value = 0;
}

private void Display_CurrentStateChanged(object sender, RoutedEventArgs e)
{
	Shared.Timer(Display.CurrentState == MediaElementState.Playing);
}

While still in the Code View in the public MainPage() { … } method below this.InitializeComponent(); the following should be entered:

Shared.Init();
Shared.Playing += () =>
{
	Position.Value = (int)Display.Position.TotalMilliseconds;
};

It should then appear as such:

vs-windows-code-mediaplayer

Step 13

That completes the Windows Application part so Save the Project then select the Debug and Simulator option to run the Application

vs-windows-debug

Step 14

Once the Simulator has started the Application should then appear

vs-windows-run-mediaplayer

Step 15

After it has started running you can then type in the URL of any media item then press or tap enter to load and Play this, you can Pause or Stop the playback

vs-windows-ran-mediaplayer

Step 16

To Exit the application select Stop Debugging in Visual Studio Community 2013

vs-stop

Step 17

Another part is the Windows Phone application so from the Solution Explorer select the MainPage.xaml from the Project ending with .WindowsPhone

vs-phone-mainpage

Step 18

Select from the Menu, Project then Set as StartUp Project

vs-startup-project

Step 19

Select from the Menu, View then Designer

vs-designer

Step 20

The Design View will be displayed along with the XAML View and in this above <Grid> enter the following XAML:

<Page.BottomAppBar>
	<CommandBar IsOpen="True" IsSticky="True">
		<AppBarButton Name="Play" Icon="Play" Label="Play" Click="Play_Click"/>
		<AppBarButton Icon="Stop" Label="Stop" Click="Stop_Click"/>
	</CommandBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid>enter the following XAML:

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<TextBox Grid.Row="0" Margin="20" Name="Value" InputScope="Url" KeyDown="Go_KeyDown"/>
	<Grid Grid.Row="1" Margin="5">
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="*"/>
			<ColumnDefinition Width="120"/>
		</Grid.ColumnDefinitions>
		<Slider Grid.Column="0" Padding="5" Minimum="0" Name="Position"/>
		<Slider Grid.Column="2" Padding="5" Minimum="0" Maximum="1" Value="0.5" Name="Volume" ValueChanged="Volume_ValueChanged"/>
	</Grid>
	<MediaElement Grid.Row="2" Name="Display" AutoPlay="True" MediaOpened="Display_MediaOpened" MediaEnded="Display_MediaEnded" CurrentStateChanged="Display_CurrentStateChanged"/>
</Grid>

It should appear as such:

vs-phone-xaml-mediaplayer

Step 21

Select from the Menu, View then Code

vs-code

Step 22

Once in the Code View below the protected override void OnNavigatedTo(NavigationEventArgs e) { … } the following should be entered:

public Shared Shared = new Shared();

private void Play_Click(object sender, RoutedEventArgs e)
{
	if (Display.CurrentState == MediaElementState.Playing)
	{
		Display.Pause();
		Play.Icon = new SymbolIcon(Symbol.Play);
		Play.Label = "Play";
	}
	else
	{
		Display.Play();
		Play.Icon = new SymbolIcon(Symbol.Pause);
		Play.Label = "Pause";
	}
}

private void Stop_Click(object sender, RoutedEventArgs e)
{
	Display.Stop();
}

private void Go_KeyDown(object sender, KeyRoutedEventArgs e)
{
	Shared.Go(ref Display, Value.Text, e);
}

private void Volume_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
	if (Volume != null)
	{
		Display.Volume = (double)Volume.Value;
	}
}

private void Display_MediaOpened(object sender, RoutedEventArgs e)
{
	Position.Maximum = (int)Display.NaturalDuration.TimeSpan.TotalMilliseconds;
	Display.Play();
	Play.Icon = new SymbolIcon(Symbol.Pause);
	Play.Label = "Pause";
}

private void Display_MediaEnded(object sender, RoutedEventArgs e)
{
	Play.Icon = new SymbolIcon(Symbol.Play);
	Play.Label = "Play";
	Display.Stop();
	Position.Value = 0;
}

private void Display_CurrentStateChanged(object sender, RoutedEventArgs e)
{
	Shared.Timer(Display.CurrentState == MediaElementState.Playing);
}

While still in the Code View in the protected override void OnNavigatedTo(NavigationEventArgs e) method the following should be entered:

Shared.Init();
Shared.Playing += () =>
{
	Position.Value = (int)Display.Position.TotalMilliseconds;
};

It should then appear as such:

vs-phone-code-mediaplayer

Step 23

That completes the Windows Phone Application part so Save the Project then select Debug and select Emulator 8.1 WVGA 4 inch 512MB option to run the Application

vs-phone-debug

Step 24

Once the Emulator has started the Application should then appear

vs-phone-run-mediaplayer

Step 25

After it has started running you can then type in the URL of any media item then press or tap enter to load and Play this, you can Pause or Stop the playback

vs-phone-ran-mediaplayer

Step 26

To Exit the application select Stop Debugging in Visual Studio Community 2013

vs-stop

Creative Commons License

One thought on “Windows 8.1 and Windows Phone 8.1 Universal Application – Media Player

Leave a comment