Introduction
Windows 8 Apps have a good control for playing audio & videos in applications. I have integrated a Media Control in this sample and used several methods to make it work.
To embed a music or video player in your application we use the “MediaElement class”. I also created customized controls to give more functionality such as play, pause, volume +, Volume – and Stop.
You can use also “enable full-screen mode” when you use a video. I used the MediaElement class to control and manage audio media playback in a Windows Store app using C#.
In this sample the MediaElement class has the following methods:
- Pause: Pauses media at the current position.
- Play: Plays media from the current position.
- Stop: Stops and resets media to be played from the beginning.
The MediaElement class has these properties:
IsMuted Gets or sets a value indicating whether the audio is muted.
Volume Gets or sets the media’s volume.
Description
I’ve used Visual Studio Ultimate 2012 and used the following 5 buttons:
Button | Click handler action |
Play | Calls the Play method. |
Pause | Calls the Pause method. |
Stop | Calls the Stop method. |
Vol – | Volume Down |
Vol + | Volume Up |
And one MediaElement (Name property: Media).
C# Code
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Data;
- using Windows.UI.Xaml.Input;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Navigation;
-
-
-
- namespace Music_Player
- {
-
-
-
- public sealed partial class MainPage : Page
- {
- public MainPage()
- {
- this.InitializeComponent();
- }
-
-
-
-
-
-
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- }
-
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- Media.Play();
- }
-
- private void Button_Click_2(object sender, RoutedEventArgs e)
- {
- Media.Pause();
- }
-
- private void Button_Click_3(object sender, RoutedEventArgs e)
- {
- Media.Stop();
- }
-
- private void Button_Click_4(object sender, RoutedEventArgs e)
- {
- if (Media.IsMuted)
- {
- Media.IsMuted = false;
- }
-
- if (Media.Volume > 0)
- {
- Media.Volume -= .1;
- }
- }
-
- private void Button_Click_5(object sender, RoutedEventArgs e)
- {
- if (Media.IsMuted)
- {
- Media.IsMuted = false;
- }
-
- if (Media.Volume < 1)
- {
- Media.Volume += .1;
- }
- }
- }
- }