Step 1
Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.
Step 2
Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.
Step 3
From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in a Name and select a Location to save to before selecting Ok to create the Project.
Step 4
Once done select from the Menu, Project, then Add New Item…
Step 5
From the Add New Item window select Visual C# from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 6
Once in the Code View for Library.cs the following should be entered:
using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; public class Library { 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:
Step 7
From the Solution Explorer select MainPage.xaml
Step 8
Select from the Menu, View then Designer
Step 9
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> <AppBar IsOpen="True" IsSticky="True"> <StackPanel Orientation="Horizontal"> <AppBarButton Name="Play" Icon="Play" Label="Play" Click="Play_Click"/> <AppBarButton Icon="Stop" Label="Stop" Click="Stop_Click"/> </StackPanel> </AppBar> </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:
Step 10
Select from the Menu, View then Code
Step 11
Once in the Code View below the public MainPage() { … } the following should be entered:
public Library Library = new Library(); protected override void OnNavigatedTo(NavigationEventArgs e) { Library.Init(); Library.Playing += () => { Position.Value = (int)Display.Position.TotalMilliseconds; }; } 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) { Library.Go(ref Display, Value.Text, e); } private void Volume_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if (Display != null && 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) { Library.Timer(Display.CurrentState == MediaElementState.Playing); }
It should then appear as such:
Step 12
That completes the Windows Universal Application so Save the Project then select the Debug and Simulator option to run the Application
Step 13
Once the Simulator has started the Application should then appear
Step 14
After the Application has started running you can then type in the URL of any media item e.g. http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4 then press or tap enter to load and Play this, you can Pause or Stop the playback
Step 15
To Exit the application select Stop Debugging in Visual Studio Community 2015
Step 16
Another option is to run as a Windows Phone application, select Debug and select Emulator 10.0.1.0 WVGA 4 inch 512MB option to run the Application
Step 17
Once the Emulator has started the Application should then appear
Step 18
After the Application has started running you can then type in the URL of any media item e.g. http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4 then press or tap enter to load and Play this, you can Pause or Stop the playback
Step 19
To Exit the application select Stop Debugging in Visual Studio Community 2015
This is seriously the best tutorial on the net! Nice job!
I was wondering, is it possible to load movies from the device instead of loading it from an url?
I thought if i change http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4 to C:\mypath\movie.mp4 it should work.. but i didnt lol. I cant figure out why :O
Hope you could help me out 🙂
greetings
LikeLike
Thanks for the feedback! It should be possible to get it to open file from anywhere if you use a File Open dialog like in the Text Editor example, although permissions issues may prevent it seeing any folder on a computer but should work
LikeLike
Great, thanks a lot. I got a player running. !!!!!! BIG LIKES!!!!! One question: can I play video from youtube with this url: https://youtu.be/7Co9BK052Ig in the browser does it work.
LikeLike
You won’t be able to play the YouTube video directly but if it’s an MP4 version or have a link to the video direct that will work
LikeLike
Great work!!!! Sure is the best media player tutorial. I have been searching the internet for two days for this. Thanks!
LikeLike
Thanks – glad you found it useful!
LikeLike