Need solution to Stop wav files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vikram Raman
    New Member
    • Feb 2010
    • 24

    #1

    Need solution to Stop wav files

    I am developing a small project in wpf. I have 13 user controls. Each has one wav(sound) file. I wrote a function in a class to play the wav file. I have list view control to display the user control items in the main window. Whenever i click the list view item, the corresponding wav file should play.It plays, it works fine. If i go to the next item while playing the current item, the current item's file is playing. It should be stopped.I dont know how to do that...

    The following is the Class code

    Code:
     class Class1
        {
          string  fpath;
          //public SoundPlayer player;
          public MediaPlayer player;
            public void playsound(string s)
            {
                if (s != null)
                {
                  
                    try
                    {
                       
    
                        //player = new SoundPlayer(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\IntroToRatios\\" + s + "");
                        //player.Play();
                       
                        
                       
                            fpath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\IntroToRatios\\" + s + "";
                            player = new MediaPlayer();                        
                            player.Open(new Uri(fpath));
                            player.Play();
                            
                        
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                
               
            }      
        }
    The following is the user control code
    Code:
    Class1 cls = new Class1()
    
    if(timertick = 200)
    {
    cls.playsound("2.wav");
    }
    Help me....

    Thank you.....
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Instead of using multiple instances of MediaPlayer, only declare one instance of media player and use it
    I would guess that telling it to play another sound would implicitly stop it, but if it does not, there is surely a Stop() function.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      It looks like you're creating a new player object every time you play a sound, so you're losing your reference to the last object you created to play the last sound.

      Firstly, I don't think you need to create a new Media Player object every time... I think one should be enough. In your PlaySound method, you can probably have some code like this..

      Code:
      if (player == null) player = new MediaPlayer();
      Then it may help to call a player.Stop() before you load the new file and play it.

      *Edit: Haha, oops, I guess that's what I get for waiting so long to hit he post button :)

      Comment

      • Vikram Raman
        New Member
        • Feb 2010
        • 24

        #4
        I gave it(player.Stop( )) before I load the new file and play it. When I debug the program I saw that the player has null value. So it creates a new object.The old file is playing too... Help pls

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Are you still declaring a new instance of class1 when you play a new file? Try using only one instance of that as well.

          Comment

          • Vikram Raman
            New Member
            • Feb 2010
            • 24

            #6
            Thank you Plater. Now it works fine.

            Comment

            Working...