如何编辑列表视图中的方法?

时间:2014-08-05 13:42:15

标签: android listview

我想创建一个ListView音频文件。我在列表中播放音频时遇到问题。当我从列表中选择音频时,之前播放的音频不会停止。    我知道MediaPleyer有方法stop(),但我不知道如何在我的java结构中编辑方法stop() ...

java code:

 public class MainActivity extends Activity {

 int []audio={R.raw.salavat_1,R.raw.salavat_2,R.raw.salavat_3,R.raw.salavat_4,
       R.raw.salavat_5};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView list=(ListView) findViewById(R.id.listView1);
    Myadapter adapter=new Myadapter(getApplicationContext());
     seekBar=(SeekBar) findViewById(R.id.seekBar1);
    list.setAdapter(adapter);

}

public class Myadapter extends BaseAdapter implements Runnable
{
    LayoutInflater myInflater;
    public Myadapter(Context context) {
        myInflater=LayoutInflater.from(context);
        // TODO Auto-generated constructor stub
    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return audio.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return  position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder Holder;

        if(convertView==null)
        {
            Holder=new ViewHolder();
            convertView=myInflater.inflate(R.layout.item_layout, null);
            Holder.play=(ImageView)convertView.findViewById(R.id.Imag_play);

           Holder.play.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            play(audio [position]);

                }

        });

           convertView.setTag(Holder);




        }
        else
            Holder=(ViewHolder) convertView.getTag();



        // TODO Auto-generated method stub
        return convertView;
    }
      ----------
     public void run() {
        int currentPosition= 0;
        int total = mp.getDuration();
        while (mp!=null && currentPosition<total) {
            try {
                Thread.sleep(1000);
                currentPosition= mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }            
            seekBar.setProgress(currentPosition);
        }
    }


    protected void play(int audio) {


            if(seekBar.getProgress() > 0) {
                mp.start();
                return;
            }
        mp=MediaPlayer.create(getApplicationContext(), audio);
        mp.start();                     
        seekBar.setProgress(0);
        seekBar.setMax(mp.getDuration());
        new Thread(this).start();




        // TODO Auto-generated method stub

    }

1 个答案:

答案 0 :(得分:0)

您不需要编辑MediaPleyer.stop()方法。

而是调整play(int audio)方法,首先检查音轨是否在播放,如果正在播放,请将其停止。

您可以通过调用play(int audio)MediaPlayer.isPlaying()方法开始时执行此操作。如果这是真的,请致电MediaPlayer.stop()。然后,您可以使用mp = null

清除MediaPlayer对象

编辑:像:

protected void play(int audio) {
    if(seekBar.getProgress() > 0) {
        mp.start();
        return;
    }

    // Stop and clean the MediaPlayer if there is already a track playing
    if(mp.isPlaying() == true) {
        mp.stop();
        mp = null;
    }

    mp=MediaPlayer.create(getApplicationContext(), audio);
    mp.start();                     
    seekBar.setProgress(0);
    seekBar.setMax(mp.getDuration());
    new Thread(this).start();

    // TODO Auto-generated method stub

}

注意:我没有运行此代码。