locked
play audio RRS feed

  • Question

  • Im trying to use the MediaElement class suggested. I try like this: MediaElement me = new MediaElement(); me.Source = new Uri("bell.wav"); It looks correct to me, but I get 3 errors. I browsed the web and found this blog: http://blogs.msdn.com/b/mikezh/archive/2012/05/17/play-audio-in-windows-phone-application.aspx His code is written almost identically to mine, but he says his works! I tried one of his snippets, and I got nothing. string dingSoundFile = "bell.wav"; SoundEffectAction soundEffect = new SoundEffectAction(); soundEffect.Source = dingSoundFile; soundEffect.Play(); Next I created a empty project to be sure my errors werent from myself, and I got the same errors. :( I get invalid token '=' and invalide token ';' And sometimes a type error. Not sure how. I'd just like to play the bell that I added to he project, I don't care what class does it. Any help is appreciated.
    Wednesday, March 13, 2013 4:45 PM

Answers

  • Hello Zappu.

    The problem with creating a new MediaElement every time you hit a button, you're going to end up with a lot of objects added to your layout.  (I am assuming you are adding the MediaElement to the visual layout.)  In addition you have to set the MediaState to Manual if you want to be able to control it from code.

    So if you wanted to create a new one with the click of a button you could do something like this...

    private void createMediaButton(object sender, System.Windows.RoutedEventArgs e)
    {
    	MediaElement me = new MediaElement();
    	me.LoadedBehavior = MediaState.Manual;
    	me.Source = new Uri("Sounds/bounce.wav", UriKind.RelativeOrAbsolute);
    	LayoutRoot.Children.Add(me);
    	me.Play();
    	me.MediaEnded += meEnded;
    }
    
    private void meEnded(object sender, EventArgs e)
    {
    	MediaElement me = sender as MediaElement;
    	LayoutRoot.Children.Remove(me);
    }

    I added the meEnded which removes the object once it has played.

    Another option is to create the MediaElement in you xaml and then change the source from code and play it...

    <Grid x:Name="LayoutRoot">
    	<MediaElement x:Name="myMediaElement" 
    		HorizontalAlignment="Left" 
    		Height="100" 
    		VerticalAlignment="Top" 
    		Width="100" Volume="1" 
    		LoadedBehavior="Manual"/>

    Notice the LoadBehavior is set to Manual.

    Then to add a source on a button click and play it...

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
    	myMediaElement.Source = new Uri("Sounds/bounce.wav", UriKind.RelativeOrAbsolute);
        	myMediaElement.Play();
    }

    Hope that helps.

    ~Christine

    • Marked as answer by zAppU Tuesday, April 30, 2013 1:16 PM
    Wednesday, March 13, 2013 6:20 PM

  • I finally got sound playing! I am using the names below. Referenced from Microsoft.Xna.Framework.dll 

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;

    I used the SoundEffect and SoundEffectInstance classes like so:

                    SoundEffect se;
    
                    se = SoundEffect.FromStream(TitleContainer.OpenStream("bell.wav"));
                    FrameworkDispatcher.Update();				                       
                    SoundEffectInstance sei = se.CreateInstance();                
                    sei.Volume = 0.5f;
                    sei.Play();

    After beating my head in some, I found that the "FrameworkDispatcher.Update();" is needed from this blog: 

    http://stackoverflow.com/questions/6566520/playing-an-array-of-sounds-windows-phone-7

    I don't know if this is the best solution (??), but it's the only one I could get to work. Thanks for all the help. 

    • Marked as answer by zAppU Tuesday, April 30, 2013 1:16 PM
    Thursday, March 14, 2013 4:41 PM

All replies

  • Hello Zappu.

    The problem with creating a new MediaElement every time you hit a button, you're going to end up with a lot of objects added to your layout.  (I am assuming you are adding the MediaElement to the visual layout.)  In addition you have to set the MediaState to Manual if you want to be able to control it from code.

    So if you wanted to create a new one with the click of a button you could do something like this...

    private void createMediaButton(object sender, System.Windows.RoutedEventArgs e)
    {
    	MediaElement me = new MediaElement();
    	me.LoadedBehavior = MediaState.Manual;
    	me.Source = new Uri("Sounds/bounce.wav", UriKind.RelativeOrAbsolute);
    	LayoutRoot.Children.Add(me);
    	me.Play();
    	me.MediaEnded += meEnded;
    }
    
    private void meEnded(object sender, EventArgs e)
    {
    	MediaElement me = sender as MediaElement;
    	LayoutRoot.Children.Remove(me);
    }

    I added the meEnded which removes the object once it has played.

    Another option is to create the MediaElement in you xaml and then change the source from code and play it...

    <Grid x:Name="LayoutRoot">
    	<MediaElement x:Name="myMediaElement" 
    		HorizontalAlignment="Left" 
    		Height="100" 
    		VerticalAlignment="Top" 
    		Width="100" Volume="1" 
    		LoadedBehavior="Manual"/>

    Notice the LoadBehavior is set to Manual.

    Then to add a source on a button click and play it...

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
    	myMediaElement.Source = new Uri("Sounds/bounce.wav", UriKind.RelativeOrAbsolute);
        	myMediaElement.Play();
    }

    Hope that helps.

    ~Christine

    • Marked as answer by zAppU Tuesday, April 30, 2013 1:16 PM
    Wednesday, March 13, 2013 6:20 PM
  • You know I got to thinking about it and if you are only after a sound, why not use System.Media.SoundPlayer?

    private void playLoadedSound(object sender, System.Windows.RoutedEventArgs e)
    {
    	System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
    	sp.SoundLocation = "Sounds/bounce.wav";
    	sp.Play();
    	sp.Dispose();
    }


    ~Christine


    Wednesday, March 13, 2013 6:31 PM
  • Thanks for the reply and the helpful tips.

    I could not find the MediaElement.LoadedBehavior prop in the System.Windows.Controls.MediaElement.  :/

    I am trying to play a sound if a condition is true.  I'm not sure if the SoundPlayer works in blend. I'm gonna find out. Thanks again for the help  : )


    • Edited by zAppU Thursday, March 14, 2013 4:46 PM
    Wednesday, March 13, 2013 7:11 PM
  • The System.Media.SoundPlayer is from System.dll. I don't believe it will work because of that. I tried in blend and it prompts me.
    Wednesday, March 13, 2013 7:28 PM
  • Actually, the samples I posted up above I created/tested in Blend.

    What sort of a project is this?  Silverlight, WPF, Windows Phone?

    ~Christine

    Wednesday, March 13, 2013 8:21 PM
  • Oh, It's a windows phone project. Maybe that's why.
    Wednesday, March 13, 2013 10:54 PM
  • Windows Phone MediaElement does not have MediaElement.LoadedBehavior.

    And no, the System.Media cannot be used in WP either.

    So basically...

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Phone.Controls;
    
    
    namespace PhoneApp1
    {
        public partial class MainPage : PhoneApplicationPage
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void btn1_Click(object sender, RoutedEventArgs e)
            {
                //Changes an Existing MediaElement's Source
                myMediaElement.Source = new Uri("Sounds/bounce.wav", UriKind.RelativeOrAbsolute);
                myMediaElement.Play();
            }
    
            private void btn2_Click(object sender, RoutedEventArgs e)
            {
                //Creats a new MediaElement and Removes When Done Playing.
                MediaElement me = new MediaElement();
                me.Source = new Uri("Sounds/bounce.wav", UriKind.RelativeOrAbsolute);
                ContentPanel.Children.Add(me);
                me.MediaEnded += onEnd;
                me.Play();
            }
    
            private void onEnd(object sender, EventArgs e)
            {
                //Called when MediaElement Ended.
                MediaElement me = sender as MediaElement;
                ContentPanel.Children.Remove(me);
            }
        }
    }

    Xaml for the ContentPanel...

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <MediaElement x:Name="myMediaElement" HorizontalAlignment="Left" Height="72" VerticalAlignment="Top" Width="62" Margin="394,0,0,0"/>
        <Button x:Name="btn1" Content="Change Source of Existing Media" HorizontalAlignment="Left" VerticalAlignment="Top" Click="btn1_Click"/>
        <Button x:Name="btn2" Content="Create New Media" HorizontalAlignment="Left" Margin="0,72,0,0" VerticalAlignment="Top" Click="btn2_Click"/>
    </Grid>

    I have not done phone apps in some time, since WP7 was about to come out. But I recall they would turn down an application if you did not allow the user to turn the sounds on or off.  You may want to check into that. 

    I did test this using VS Express 2012 for WP8 and Blend for VS 2012.  The sound played both ways.

    ~Christine



    Thursday, March 14, 2013 1:04 AM
  • I'm trying what you suggested now. Thanks for the help. I also found an XNA namespace that might be useful. Microsoft.Xna.Framework.Audio. So far, it works with the phone and seems like it can do sound. I'll see.
    • Edited by zAppU Thursday, March 14, 2013 3:00 AM
    Thursday, March 14, 2013 2:59 AM
  • Ooh! Share the xna solution if it works please. What a brilliant idea! ~Christine
    Thursday, March 14, 2013 4:23 AM

  • I finally got sound playing! I am using the names below. Referenced from Microsoft.Xna.Framework.dll 

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;

    I used the SoundEffect and SoundEffectInstance classes like so:

                    SoundEffect se;
    
                    se = SoundEffect.FromStream(TitleContainer.OpenStream("bell.wav"));
                    FrameworkDispatcher.Update();				                       
                    SoundEffectInstance sei = se.CreateInstance();                
                    sei.Volume = 0.5f;
                    sei.Play();

    After beating my head in some, I found that the "FrameworkDispatcher.Update();" is needed from this blog: 

    http://stackoverflow.com/questions/6566520/playing-an-array-of-sounds-windows-phone-7

    I don't know if this is the best solution (??), but it's the only one I could get to work. Thanks for all the help. 

    • Marked as answer by zAppU Tuesday, April 30, 2013 1:16 PM
    Thursday, March 14, 2013 4:41 PM