none
SoundEffect and trouble

    Question

  • I have a problem with SoundEffect


    Button1

                var stream = TitleContainer.OpenStream("Music/music1.wav");
                var music1 = SoundEffect.FromStream(stream);
                FrameworkDispatcher.Update();
               
                music1.Play();

    Button2 does not work (Error)

    music1.Stop();



    Tuesday, October 12, 2010 2:41 PM

All replies

  • What error do you get?

    Tuesday, October 12, 2010 2:44 PM
  • The name 'music1' does not exist in the current context

    Tuesday, October 12, 2010 3:49 PM
  • music1 is a local variable defined in the Button1 click method. You may want to define that variable outside of the method to ensure that both methods can access it.

    var music1;
    
    void Button1_Click(object sender, EventArgs args)
    {
        music1.Play();
    }
    
    void Button2_Click(object sender, EventArgs args)
    {
        music1.Stop();
    }


     

    Tuesday, October 12, 2010 4:09 PM
  • Again there is an error

                var stream = TitleContainer.OpenStream("Music/music1.wav");
                var music1 = SoundEffect.FromStream(stream);
                FrameworkDispatcher.Update();
    
    
    void Button1_Click(object sender, EventArgs args)
    {
        music1.Play();
    }
    
    void Button2_Click(object sender, EventArgs args)
    {
        music1.Stop();
    }


    Error

    Invalid token '(' in class, struct, or interface member declaration



    Tuesday, October 12, 2010 6:25 PM
  • var music1;
    
     
    void Button1_Click(object sender, EventArgs args) 
    { 
        var stream = TitleContainer.OpenStream("Music/music1.wav"); 
        music1 = SoundEffect.FromStream(stream); 
        FrameworkDispatcher.Update(); 
    
        music1.Play(); 
    } 
     
    void Button2_Click(object sender, EventArgs args) 
    { 
        if (music1 != null)
            music1.Stop(); 
    }


    Wednesday, October 13, 2010 1:37 PM
  • This is strange! 

    Copy > Paste and I got error...

    The contextual keyword 'var' may only appear within a local variable declaration

    Wednesday, October 13, 2010 2:15 PM