Play with Media Player Task in Windows Phone

DZone 's Guide to

Play with Media Player Task in Windows Phone

· Mobile Zone ·
Free Resource

In this article we are going to see how to play around with the Media Player Launcher task, with this Media Player task we can provide with in the application to launch a media player and play a video or audio file. We can provide a list of media files that can be played one by one, along with we have option to make use of the Media play back controls like rewind and stop to skip or rerun the media file. Media file location can be as a resource file or we can also use an Isolated storage to store and retrieve the file while rendering the player to play the respective file. Let us see the steps on how to use this task in a Windows Phone application development.

Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to trigger the Media Player Task as shown in the screen below.


Now drag and drop few controls to the screen which is used to trigger event that launches the Media Player Launcher Task. Once we designed the screen with the controls we can see the screen looks like below.


Now we need to write our code in the button click event to trigger the Media Player task on the user click. To do that just go to the code behind and first add the USING handler code on top with the existing using statements as shown in the code below.

using Microsoft.Phone.Tasks;

Next is to add the below code to the button click event which basically creates an instance of the Media Player task as shown in the code below. With the MediaPlayerLauncher task we can render a video or audio stream directly from the online server or we can play by saving the file on to the local Isolated Storage as per the requirement. We have a property called MediaFileLocationType through which we can specify from which location we are going to stream the video or an audio file. MediaFileLocationType have 3 different options as shown below.

  • MediaLocationType.Data – Video/Audio file stored in Isolated Storage.
  • MediaLocationType.Install – Video/Audi0 file which is packed with the .XAP file which is available in the root
  • MediaLocationType.None – If the Video or Audio file is not available in both the locations, it may throw an error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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 Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace F5DebugHowto19
    public partial class MainPage : PhoneApplicationPage
        // Constructor
        public MainPage()

        private void button1_Click(object sender, RoutedEventArgs e)
            MediaPlayerLauncher mpLauncherTask = new MediaPlayerLauncher();
            mpLauncherTask.Media = new Uri(@"http://media.ch9.ms/ch9/97e3/0da29222-9422-4d95-8760-406720a497e3/CloudCover79.mp4", UriKind.Absolute);
            mpLauncherTask.Controls = MediaPlaybackControls.All;
            mpLauncherTask.Location = MediaLocationType.Data;
            mpLauncherTask.Orientation = MediaPlayerOrientation.Portrait;


Note – Since we are going to make this application run in Windows Phone Emulator we will not see the Video Rendered, if its an Audio source then it will be rendered.

Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.

Output Screens:


So as we can see the expected output in the screens above, we need to run the application on to the real device to stream the video. Also we can make use of the MediaPlayerLauncher.Controls to handle which controls are required and not required (Rewind, Fastforward, stop etc) based on if any requirements. That’s it from this short tutorial on Windows Phone see you all in the next tutorial soon. Mean while Happy Programming!!!


Published at DZone with permission of Karthikeyan Anbarasan , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}