Click here to Skip to main content
13,455,913 members (48,735 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 21 Jul 2012

Playing a Sound in Windows Phone using Media Element

, 21 Jul 2012
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.

You may also be interested in...


Comments and Discussions

BugNot Working After Dormant Pin
mahmoodels19-Oct-14 0:49
membermahmoodels19-Oct-14 0:49 
GeneralMy vote of 1 Pin
Member 827905114-Nov-12 3:37
memberMember 827905114-Nov-12 3:37 
GeneralMy vote of 5 Pin
Christian Amado30-Jul-12 2:27
memberChristian Amado30-Jul-12 2:27 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180322.1 | Last Updated 21 Jul 2012
Article Copyright 2012 by isenthil
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid