I'm trying to play the sounds according to what's called on the arraylist but the sounds are playing randomly and it seems like the sounds are overlapping. what is wrong with my code and what code should i use or add? can anyone help me?

here is my code:

package com.example.sample;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;




MediaPlayer mp        = null;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.play);

    final Button buttonPlay = (Button) findViewById(R.id.idPlay);
    buttonPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            managerOfSound();
        } // END onClick()
    }); // END buttonHello
} // END onCreate()

protected void managerOfSound() {
    if (mp != null) {
        mp.reset();
        mp.release();
    }
   for (int i = 0; i < tempq.size(); i++){

    if (tempq.contains("a1")){
        mp = MediaPlayer.create(this, R.raw.a1);
        mp.start();
    }else if (tempq.contains("b1")){
        mp = MediaPlayer.create(this, R.raw.b1);
        mp.start();
    }else if (tempq.contains("c1")){
        mp = MediaPlayer.create(this, R.raw.c1);
        mp.start();
    }else if (tempq.contains("d1")){
        mp = MediaPlayer.create(this, R.raw.d1);
        mp.start();
    }else if (tempq.contains("e1")){
        mp = MediaPlayer.create(this, R.raw.e1);
        mp.start();
    }else if (tempq.contains("f1")){
        mp = MediaPlayer.create(this, R.raw.f1);
        mp.start();
    }else if (tempq.contains("g1")){
        mp = MediaPlayer.create(this, R.raw.g1);
        mp.start();
    }else if (tempq.contains("h1")){
        mp = MediaPlayer.create(this, R.raw.h1);
        mp.start();
    }else if (tempq.contains("i1")){
        mp = MediaPlayer.create(this, R.raw.i1);
        mp.start();
    }else if (tempq.contains("j1")){
        mp = MediaPlayer.create(this, R.raw.j1);
        mp.start();
    }else if (tempq.contains("k1")){
        mp = MediaPlayer.create(this, R.raw.k1);
        mp.start();
    }else if (tempq.contains("l1")){
        mp = MediaPlayer.create(this, R.raw.l1);
        mp.start();
    }else if (tempq.contains("m1")){
        mp = MediaPlayer.create(this, R.raw.m1);
        mp.start();

    }else if (tempq.contains("a4")){
        mp = MediaPlayer.create(this, R.raw.a4);
        mp.start();
    }else if (tempq.contains("b4")){
        mp = MediaPlayer.create(this, R.raw.b4);
        mp.start();
    }else if (tempq.contains("c4")){
        mp = MediaPlayer.create(this, R.raw.c4);
        mp.start();
    }else if (tempq.contains("d4")){
        mp = MediaPlayer.create(this, R.raw.d4);
        mp.start();
    }else if (tempq.contains("e4")){
        mp = MediaPlayer.create(this, R.raw.e4);
        mp.start();
    }else if (tempq.contains("f4")){
        mp = MediaPlayer.create(this, R.raw.f4);
        mp.start();
    }else if (tempq.contains("g4")){
        mp = MediaPlayer.create(this, R.raw.g4);
        mp.start();
    }else if (tempq.contains("h4")){
        mp = MediaPlayer.create(this, R.raw.h4);
        mp.start();
    }else if (tempq.contains("i4")){
        mp = MediaPlayer.create(this, R.raw.i4);
        mp.start();
    }else if (tempq.contains("j4")){
        mp = MediaPlayer.create(this, R.raw.j4);
        mp.start();
    }else if (tempq.contains("k4")){
        mp = MediaPlayer.create(this, R.raw.k4);
        mp.start();
    }else if (tempq.contains("l4")){
        mp = MediaPlayer.create(this, R.raw.l4);
        mp.start();
    }else if (tempq.contains("m4")){
        mp = MediaPlayer.create(this, R.raw.m4);
        mp.start();
    }

    }
}

}

Edited 1 Year Ago by pritaeas: Moved to Java.

aya: not according to your code. if you want it that way, you'll need to remove the 'else' statements from your code.

as soon as the first contains returns true, none of the next will be played, since they are under the 'else'.

stultuske: thanks. i will try it. but it seems to be ok because when the tempq contains 3 elements it also plays 3 sounds but the problem is the sounds are overlapping.

Edited 1 Year Ago by aya.dioses

Yes they overlap - that's how you programmed it!...
you loop though the number of entries in tempq and for each one you create and start a new player (the code on line 31 will only resets the last player you created).
Maybe your confusion comes from the fact that you have only one mp variable which you re-use for every new player. That doesn't mean you only have one player. When you re-use the variable you loose your reference to the old player, but the mediaplayer package still has a ref to it, so the player is not garbage collected and sound continues playing.

tempq is a list of some sorts? If it contained a1, b1, c1 like stultuske posed. You loop through all the list elements, but does it not play a1 three times? You're not checking the current element but all elements, it finds a1 then stops.

the problem is the sounds are overlapping

You could solve that issue by adding the tracks to a queue on a separate thread with a player to play the sounds in order, for instance. mp.isPlaying() will return true if it's playing at the moment.

Be sure to check the other parts here and here of the tutorial jalpesh posted for more insight on all the options.

This article has been dead for over six months. Start a new discussion instead.