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
        <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();

        }


{ 2 comments ... read them below or Comment }

  1. Thank You very much bro!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    ReplyDelete
  2. Hi
    I 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

    ReplyDelete

Followers

Total Pageviews

Powered by Blogger.

Blog Archive

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -