0

我从 android 开始,我用 ListView 制作了一个应用程序。ListView 的每个元素都有一个不同的 Mediaplayer,当您按下按钮时会播放。

它在模拟器中可以正常工作,但是当我在我的手机(Acer E140)上尝试它时,它只能再现最初可见的元素的声音。当我向下滚动并尝试重现任何其他内容时,应用程序崩溃了。正如我所说,它在模拟器上正常工作。

有任何想法吗?

非常感谢你!

这是代码的相关部分:

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);      
}
4

1 回答 1

1

MediaPlayer在任何给定程序中使用多个对象通常是一个坏主意。对于播放多个声音片段,SoundPool是公认的解决方案。

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

MediaPlayer太重了,无法创建那么多实例。MediaPlayer否则,我建议使用一个在选择项目时更改其源的单一来重新设计您的类ListView

于 2012-07-17T13:32:06.703 回答