Working With Media Player Launcher In Windows Phone 8

Introduction

In this article we will learn about the Media Player Launcher that helps to launch the Windows Phone Media Player from our app. It can be used to play the sample music with uniform Media Player interface.

Media Player Launcher Task

This is a kind of launcher provided by Windows Phone to launch the mobile Media Player for an app. This task expects four parameters to be set and works depending on those parameters. 

Before using this task we need to include the following namespace:

using Microsoft.Phone.Tasks;

To use this task we need to use the following procedure:

  1. Create a new instance of the Media Player launcher task.

    Microsoft.Phone.Tasks.
    MediaPlayerLauncher mpl = newMediaPlayerLauncher() ;
     
  2. Next is to set the following properties of this task.

    mpl.Location = MediaLocationType.Install;

    mpl.Orientation = MediaPlayerOrientation.Landscape;

    mpl.Controls = MediaPlaybackControls.None;

    mpl.Media = newUri("music.mp3",UriKind.Relative);
     

  3. Finally, show this task by calling the Show method as in the following:

    mpl.Show();

The properties that we can set for this task are as follows:

  • Location
    Sets the location of the media file to be played. It can take the three possible values Install, Data or None. If you use none then the file not found exception will be thrown by the Show method.
     
  • Orientation
    Sets the orientation of the Media Player.
     
  • Controls
    Sets the controls to be shown in the Media Player.
     
  • Media
    The Path of the file to be played.

Demo

The following code demonstrates how to use the Media Player launcher in an app. For this app to work you need to add a music file to your project.

Use the following procedure to add a music file:

  1. Click "Project" -> "Add existing Item"
     
  2. Locate your music file.
     
  3. Click "OK".
     
  4. Now open the properties of the newly added file.
     
  5. Set the build action to "Content" to play this file from the installation directory.

Note

  • If you are using the emulator then you won't be able to play videos.
     
  • Set the Location property to Data if you want to play the file from isolated storage and to install it if you want to play it from the installation directory. 
     
  • You can also use a media element to play music if you don't want any interface to show.

XAML

<phone:PhoneApplicationPage

    x:Class="Demo.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True">

 

    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

 

        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock Text="Demo" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>

            <TextBlock  Text="Demo" Margin="9,-7,0,0" FontSize="40" />

        </StackPanel>

 

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <StackPanel Orientation="Vertical">

                <TextBlock Text="Sample Music" FontSize="45" Height="106"></TextBlock>

                <TextBlock Text="Sample List" FontSize="25" Height="106"></TextBlock>

                <Button x:Name="sendBtn" Click="sendBtn_Click"  Content="Music 1" HorizontalAlignment="Left" Width="222" Height="80" VerticalAlignment="Bottom" Margin="10,0,0,0"/>

                <Button x:Name="sendBtn_Copy" Click="sendBtn_Click"  Content="Music 2" HorizontalAlignment="Left" Width="222" Height="80" VerticalAlignment="Bottom" Margin="10,0,0,0"/>

                <Button x:Name="sendBtn_Copy1" Click="sendBtn_Click"  Content="Music 3" HorizontalAlignment="Left" Width="222" Height="80" VerticalAlignment="Bottom" Margin="10,0,0,0"/>

                <Button x:Name="sendBtn_Copy2" Click="sendBtn_Click"  Content="Music 4" HorizontalAlignment="Left" Width="222" Height="80" VerticalAlignment="Bottom" Margin="10,0,0,0"/>

            </StackPanel>

        </Grid>

        <Button x:Name="sendBtn_Copy3" Click="sendBtn_Click"  Content="Music 5" HorizontalAlignment="Left" Width="222" Height="80" VerticalAlignment="Bottom" Margin="246,0,0,355" Grid.Row="1"/>

        <Button x:Name="sendBtn_Copy4" Click="sendBtn_Click"  Content="Music 6" HorizontalAlignment="Left" Width="222" Height="80" VerticalAlignment="Bottom" Margin="246,0,0,275" Grid.Row="1"/>

        <Button x:Name="sendBtn_Copy5" Click="sendBtn_Click"  Content="Music 7" HorizontalAlignment="Left" Width="222" Height="80" VerticalAlignment="Bottom" Margin="246,0,0,195" Grid.Row="1"/>

        <Button x:Name="sendBtn_Copy6" Click="sendBtn_Click"  Content="Music 8" HorizontalAlignment="Left" Width="222" Height="80" VerticalAlignment="Bottom" Margin="246,0,0,115" Grid.Row="1"/>

    </Grid>

</phone:PhoneApplicationPage>

C# Code Behind

using Microsoft.Phone.Controls;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Diagnostics;

using Microsoft.Phone.Tasks;

using System.Windows.Media;

 

namespace Demo

{

    public partial class MainPage : PhoneApplicationPage

    {

        // Constructor

        public MainPage()

        {

            InitializeComponent();

        }

 

        private void sendBtn_Click(object sender, RoutedEventArgs e)

        {

            /* Step 1 */

            Microsoft.Phone.Tasks.MediaPlayerLauncher mpl = new MediaPlayerLauncher() ;

           

            /* Step 2 */

            mpl.Location = MediaLocationType.Install;

            mpl.Orientation = MediaPlayerOrientation.Landscape;

            mpl.Controls = MediaPlaybackControls.None;

            mpl.Media = new Uri("music.mp3", UriKind.Relative);

 

            /* Step 3 */

            mpl.Show();

 

        }

    }

}

The Show method is used to launch the task.

Output

 

 


Similar Articles