Click here to Skip to main content
Click here to Skip to main content

Playing a Sound in Windows Phone using Media Element

, 21 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to play a sound in Windows Phone using media element


This tip will explain how to play a media file in Windows Phone using the Media Element.

In Windows Phone, the developers can play sound in their App using either MediaElement defined in the Windows.Controls namespace or by using the XNA.

Using the MediaElement is an easier way to play sound in Windows Phone. Just add the MediaElement control to your Page and set the source file path and set the AutoPlay property of the MediaElement to true. You will see that your Windows Phone App page when run, will automatically play the sound.

Using the Code

The following XAML code shows the usage of the Media Element to play the Media file. The AutoPlay is set to true which plays the file 1.mp3 when the page is launched.

    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True" xmlns:telerikPrimitives="

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" 
            Text="@isenthil" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Sound Demo" 
            Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" 
        Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Play Sound" Height="117" 
            HorizontalAlignment="Left" Margin="35,33,0,0" 
            Name="button1" VerticalAlignment="Top" 
            Width="387" Click="button1_Click_2" />
            <MediaElement Height="120" 
            HorizontalAlignment="Left" Margin="89,263,0,0" 
            Name="mediaElement1" VerticalAlignment="Top" 
            Width="160" Source="1.mp3" AutoPlay="True" />


If you have set the AutoPlay property of the Media Element to false, you should call the Play() method of the MediaElement to start playing the media file.



  • 21.07.2012 - V1.0


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
Senthil Kumar is a Software Engineer who has around 3 years of experience in IT industry. He is currently working as a Software Engineer in Bangalore and works mainly on the Windows or Client Development technologies and has good working experience in C#/.NET, Delphi, Winforms and SQL Server.

He is also a Microsoft Technology Certified Professional in ASP.NET.

Senthil completed his Master of Computer Applications from Christ College (Autonomous), Bangalore in the year 2009 and is a MCA Rank Holder. He has passion for Microsoft technologies especially Windows Phone development.
Follow on   Twitter

Comments and Discussions

BugNot Working After Dormant Pinmembermahmoodels19-Oct-14 1:49 
GeneralMy vote of 1 PinmemberMember 827905114-Nov-12 4:37 
so simple
GeneralMy vote of 5 PinmemberChristian Amado30-Jul-12 3:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150331.1 | Last Updated 21 Jul 2012
Article Copyright 2012 by isenthil
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid