Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MediaPlayer to play the same file multiple times with overlap

What's currently happening with my android application:

I mapped a simple image to a button and have it play a sound on click. On each click, I create a MediaPlayer object with a sound file in my raw folder, I set an OnClickListener for that MediaPlayer object which stops playing the file and releases it, and then I play the MediaPlayer object.

The code for the defined section:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ImageButton start = (ImageButton) findViewById(R.id.imageButton1);
    start.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            MediaPlayer play = MediaPlayer.create(MainActivity.this, R.raw.dvno);
            play.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    mp.stop();
                    mp.release();
                    mp = null;
                }
            });
            play.start();
        }
    });
}

What's wrong with it:

It works fine and does not crash, but it's terrible for memory and very slow in general. I'd like for users to click the button many times in a row, as fast as they possibly can, and hear the sound play instantaneously over and over with overlap. Creating a new MediaPlayer object on each click and waiting for it to finish and be released consumes too many resources and the sound often lags behind the actual press of the button. I'd like to be able to create a single sound as MediaPlayer object which can be played while overlapping on itself.

Possible Solution:

Create a single final MediaPlayer object in the scope of onCreate rather than onClick and somehow use threading to start the MediaPlayer object on every click. I read that this might be a possible solution, but I haven't ever used threads before and I don't know if they're slow, if not slower, than my current code, so I'd like to know if there's a solution without using threads that might be simpler. Manipulating the state of a single MediaPlayer to overlap on itself seems impossible at this point, but maybe I'm wrong.

Would this crash the program because of illegal states? If not, is this going to be slower than I want it to be? And if not, can anyone suggest a fix to my code?

like image 362
Maharlikans Avatar asked Oct 21 '12 19:10

Maharlikans


1 Answers

I suggest you use a SoundPool instead of MediaPlayer for this. Tutorial is here

like image 178
Raimo Ihle Avatar answered Nov 15 '22 00:11

Raimo Ihle