如何将媒体播放器的第二个按钮添加到列表视图中?

时间:2016-03-25 00:16:44

标签: android listview

我需要一些帮助才能在列表视图中添加第二个按钮来阻止Media Player播放。这是一个普通的列表视图,带有一个XML,用于定义列表中的项目(一个文本和一个按钮)。或者在哪里添加方法以在第二次单击时停止播放。现在代码正常工作,Media Player在单击按钮时启动,当单击列表中的其他按钮时它停止,但我想在第二次单击按钮时停止它。我需要一个提示因为我疯了。 ... 谢谢。 我的代码:

 public class Songs extends Activity implements      Songs Custom Adapter . Play Sound Alert  {


ListView lstSounds;
SongsCustomAdapter soundsAdapter;
SongsCustomAdapter2[] mySounds;


MediaPlayer mp;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.songs_listview);
    setTheme(android.R.style.Theme_Holo);

    lstSounds = (ListView)

            findViewById(R.id.soundList);


    mySounds = new SongsCustomAdapter2[10];
    SongsCustomAdapter2 s1 = new SongsCustomAdapter2();

    SongsCustomAdapter2 s10 = new SongsCustomAdapter2();

    // Set sound one to a beep
    s1.setSoundName("1");
    s1.setSoundUri(R.raw.a1);

    s10.setSoundName("1");

    s10.setSoundUri(R.raw.a10);

    // Add sounds to the list
    mySounds[0] = s1;

    mySounds[9] = s10;

    soundsAdapter = new

            SongsCustomAdapter(this, mySounds);

    lstSounds.setAdapter(soundsAdapter);


}

@Override
public void playSound(int uri) {
    // Play sound
    stopPlaying();
    mp = MediaPlayer.create(this, uri);
    if (!mp.isPlaying())

        mp.setLooping(true);
        mp.start();

    }


private void stopPlaying() {
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
    }
}


 @Override
 public void onDestroy() {

   stopPlaying();
    super.onDestroy();
}

@Override
public void onStart() {
    super.onStart();

}




@Override
public void onStop() {

    super.onStop();


}

适配器

public class SongsCustomAdapter extends BaseAdapter implements View.OnClickListener {

    SongsCustomAdapter2[] sounds;
    Activity context;
    PlaySoundAlert soundPlayerAlert;


public SongsCustomAdapter(Activity context, SongsCustomAdapter2[] soundsArray) {
    this.context = context;
    this.sounds = soundsArray;


    this.soundPlayerAlert = (PlaySoundAlert)context;

}

@Override
public int getCount() {
    return sounds == null ? 0 : sounds.length;
}

@Override
public Object getItem(int i) {
    return sounds[i];
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    SongsCustomAdapter2 item = (SongsCustomAdapter2)getItem(i);

    if (view == null) // reuse existing view
        view = context.getLayoutInflater().inflate(R.layout.list_item_songs,
                viewGroup, false);


    TextView t = (TextView)view.findViewById(R.id.list_item_string);
    t.setText(item.getSoundName());


    ImageButton stopsong = (ImageButton)       view.findViewById(R.id.stop_song_list_btn);
    stopsong.setTag(item.getSoundUri());

    stopsong.setOnClickListener(this);

    return view;
}

public interface PlaySoundAlert {
    public void playSound(int uri);



}

@Override
public void onClick(View view) {
    ImageButton playsong = (ImageButton) view;


    if (playsong != null) {
        int soundUri = (int)playsong.getTag();

        if (soundPlayerAlert != null) {
            soundPlayerAlert.playSound(soundUri);
        }
    }
    }

}

适配器2

public class SongsCustomAdapter2 {

    private int soundUri;
    private String soundName;

    public String getSoundName() {
        return soundName;
    }

    public void setSoundName(String soundName) {
        this.soundName = soundName;
    }

    public int getSoundUri() {
        return soundUri;
    }

    public void setSoundUri(int soundUri) {
        this.soundUri = soundUri;
    }
}

0 个答案:

没有答案