Making Soundboard...MediaPlayer help?

Discussion in 'Android App Developers' started by UNEXPECTED62, Feb 7, 2010.

  1. UNEXPECTED62
    Offline

    UNEXPECTED62 New Member

    Joined:
    Dec 28, 2009
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I have been working on a soundboard for some time now. This is my first app for Android, but I have been programming Java at an educational level (3rd year college student).

    Before I go too far, would Android's MediaPlayer class be a good thing to use for making a soundboard? Or should a different approach be taken?

    Now that that is out of the way, here is the code for one of my buttons.

    Code:
    public class Soundboard extends Activity 
    {
              MediaPlayer theme;
              Button themeSongButton;
    
              private boolean hasBeenRun = false;
            
            @Override
             public void onCreate(Bundle savedInstanceState) 
            {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.soundboard);
            
            
            theme = MediaPlayer.create(Soundboard.this,R.raw.theme);
    
             /*
             * Theme song button
             */
            
            themeSongButton.setOnClickListener(new OnClickListener()
            {
                public void onClick(View arg0)
                {
                    
                    if (hasBeenRun)
                    {
                        stopPlayer();
                    
                        try 
                        {
                            theme.prepare();
                        } 
                        catch (IllegalStateException e) 
                        {
                        
                        } catch (IOException e) 
                        {
                            
                        }
                    }
                    theme.start();
                    if (!hasBeenRun)
                    {
                        hasBeenRun = true;
                    }
                    theme.setOnCompletionListener(new OnCompletionListener()
                    {
                        public void onCompletion(MediaPlayer mp) 
                        {
                            try 
                            {
                                mp.prepare();
                            } 
                            catch (IllegalStateException e) 
                            {
                                
                            }                 
                            catch(IOException ioe)
                            {
                                
                            }
                        }
                    });
                                            
                }
            });
    
    And here is that method stopPlayer() that is called above.

    Code:
    public void stopPlayer()
        {
            if(theme.isPlaying())
            {
                theme.stop();
            }
                    
        }
    I am new to coding forums, so I'm sure the etiquette for posting code. I omitted all of the other buttons and sounds, as it is literally verbatim the above, sans a different media player object and button with it's listener, etc. The stopPlayer() method also actually includes all the buttons and stops them if they're playing. This is so if a sound is still going and you hit a new button, it will cut the sound off and start the new one.

    The bug I am running into is, after so many button presses, no sounds will play anymore. I don't get a force close, they just stop responding. I think I am somehow getting in the wrong state for the MediaPlayer, but following the StateDiagram in the API I feel like I am correct.

    Can anyone shed some light on this for me?