Search code examples
androidlistviewscrollmedia-player

Reproducing MediaPlayer on ListView (Android)


I'm starting with android and I've made an app with a ListView. Each element of the ListView has a different Mediaplayer that is suposed to play when you press a button.

It works correctly in the emulator, but when I try it on my phone (Acer E140), it only reproduces the sounds of the elements that are initially visible. When I scroll down and try to reproduce any other, the app crashes. As I said, it works correctly on the emulator.

Any ideas?

Thank you very much!

Here is the related part of the code:

public class SoundsActivity extends Activity {

Dialog dialog;
final int DIALOGO_ALERTA=1;
final int DIALOGO_SALIR=2;
final int DIALOGO_NEW=3;
final int DIALOGO=4;
private static final int MNU_OPC1 = 1;
private static final int MNU_OPC2 = 2;
Elem[] datos = new Elem[50];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    setContentView(R.layout.main);
    datos[0]=new Elem ("1.","Rin", MediaPlayer.create(SoundsActivity.this, R.raw.rin));
    datos[1]=new Elem ("2.","Shirou", MediaPlayer.create(SoundsActivity.this, R.raw.shirou));
    datos[2]=new Elem ("3.","Assasin", MediaPlayer.create(SoundsActivity.this, R.raw.assasin));
    datos[3]=new Elem ("4.","Avenger", MediaPlayer.create(SoundsActivity.this, R.raw.avenger));
    datos[4]=new Elem ("5.","Rider", MediaPlayer.create(SoundsActivity.this, R.raw.rider));
    datos[5]=new Elem ("6.","Berseker", MediaPlayer.create(SoundsActivity.this, R.raw.berseker));
    datos[6]=new Elem ("7.","Caster", MediaPlayer.create(SoundsActivity.this, R.raw.caster));
    datos[7]=new Elem ("8.","Lancer", MediaPlayer.create(SoundsActivity.this, R.raw.lancer));
    datos[8]=new Elem ("9.","Archer", MediaPlayer.create(SoundsActivity.this, R.raw.archer));
    datos[9]=new Elem ("10.","Saber", MediaPlayer.create(SoundsActivity.this, R.raw.saber));

    SharedPreferences settingss = getSharedPreferences("perfil", MODE_PRIVATE);
    String empty = "";
    for(int i=0; i<datos.length; i++){
        empty= empty+ "F";
    }
    String info = settingss.getString("save", empty);
    salvada(info);
    class Adaptador extends ArrayAdapter {
        private MediaPlayer mp=null;
        Activity context;

            Adaptador(Activity context) {
                super(context, R.layout.listitem_titular, datos);
                this.context = context;

            }


            public View getView(final int position, View convertView, final ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View item = inflater.inflate(R.layout.listitem_titular, null);

            final TextView lblTitulo = (TextView)item.findViewById(R.id.Nombre);
            final String nom= datos[position].getNombre();
            lblTitulo.setText(nom);
            TextView numer = (TextView)item.findViewById(R.id.Number);
            final String num= datos[position].getNum();
            //numer.setText(num);
            String g= "" +position;
            numer.setText(g);
            final ImageView img = (ImageView)item.findViewById(R.id.tick);
            final Button btnn = (Button)item.findViewById(R.id.Check);
            final ToggleButton bss = (ToggleButton)item.findViewById(R.id.Play);
            if(datos[position].getGuessed()==true){
                lblTitulo.setVisibility(View.VISIBLE);
                btnn.setVisibility(View.INVISIBLE);
                img.setVisibility(View.VISIBLE);
                notifyDataSetChanged();
            } 

            bss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mp!= null && mp.isPlaying()){
                        mp.pause();
                        mp.seekTo(0);
                    }
                        mp=datos[position].mp;
                        mp.start();

                }
            });


            btnn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
        Bundle b = new Bundle(); 
                    b.putString("Numero", num);
                    b.putString("Nombre", nom);
                    b.putInt("Posicion", position);
        showDialog(DIALOGO_ALERTA, b);
                }

            });

            return(item);
        }

    }

        Adaptador adaptador = new Adaptador(this);

        ListView lstOpciones = (ListView)findViewById(R.id.LstOpciones);

        lstOpciones.setAdapter(adaptador);      
}

Solution

  • It is generally a bad idea to use more than one MediaPlayer object in any given program. For playing multiple sound clips, SoundPool is the accepted solution.

    http://developer.android.com/reference/android/media/SoundPool.html

    MediaPlayer is too heavy to create that many instances of it. Otherwise, I recommend redesigning your class with a single MediaPlayer that changes its source when a ListView item is selected.