- Back to Home »
- Windows Phone apps »
- XAMLC# code to play audio using MediaElement in Windows Phone 8 apps
Posted by :
Sudhir Chekuri
Tuesday, 5 November 2013
Playing Audio using MediaElement
Add audio files required to Assets folder.
XAML code for button with click event and media element with audio file as source
XAML code for button with click event and media element with audio file as source
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<Button Height="200" Width="200"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Background="Red"
Click="Button_Click_1"
>Play</Button>
<Button Height="200" Width="200"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Background="Red"
Click="Button_Click_2"
>Stop</Button>
</StackPanel>
<MediaElement x:Name="MElement"
Source="/Assets/Krrish.mp3"
Volume="50"
AutoPlay="False"
>
</MediaElement>
</Grid>
CSharp.net code in MainPage.xaml.cs page inside button click to play audio
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MElement.Play();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
MElement.Stop();
Thank You very much bro!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ReplyDeleteHi
ReplyDeleteI used the above example and it worked great, thx :-)
The only problem is when the mobile goes to sleep and is waked again, it does not work anymore? I have to restart the app before the sound works again.
Any suggestions on what might be the problem?
Regards
Martin