我有一系列ListView行,由简单的ViewHolder辅助ArrayAdapter支持,每行都有一个'播放/暂停'按钮,用于暂停和恢复服务中的MediaPlayer,
然而,点击其中一个按钮会导致音乐开始,点击任何后续按钮播放并暂停原始按钮,而不是我实际点击的按钮,我之前从未遇到过这样的问题?
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.play_podcast_btn = (ImageButton) row.findViewById(R.id.play_episode_btn);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
holder.play_podcast_btn.setTag(holder);
}
和
static class ViewHolder {
ImageButton play_podcast_btn;
}
和
holder.play_podcast_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ImageButton play_podcast_btn =(ImageButton)v;
if(podCastPlaying == false)
{
startNewPodcast();
}
else
{
((MainActivity) context).togglePlayPause();
}
}
});
回调:
在MediaPlayer服务中:( BusProvider提供回调启动服务的活动:下面)
public void pause() {
mediaPlayer.pause();
BusProvider.getInstance().post(new PodcastPausedEvent(true));
}
public void play() {
mediaPlayer.start();
BusProvider.getInstance().post(new PodcastPlayedEvent(true));
}
MainActivity :(被通知暂停并触发另一个回调,这次是到列表行)
@Subscribe
public void OnPodcastPaused(PodcastPausedEvent obj) {
onPodcastStatusListener.podcastPaused();
play_pause_button.setImageResource(R.drawable.ic_play_button_black);
}
@Subscribe
public void OnPodcastPlayed(PodcastPlayedEvent obj) {
onPodcastStatusListener.podcastPlayed();
play_pause_button.setImageResource(R.drawable.ic_pause_button_black);
}
并在ListView Row ImageButton onClick:
((MainActivity) context).preparePodcast(item.getFilename(), item.getItem_title(), item.getChannel_title(),
item.getItem_pubdate(), item.getItem_description()).setPodcastStatusListener(new podcastStatusListener(){
@Override
public void podcastPrepared() {
mH.podCastPlaying = false;
}
@Override
public void podcastPlayed() {
mH.play_podcast_btn.setImageResource(R.drawable.pause_button_overlay);
mH.podCastPlaying = true;
}
@Override
public void podcastPaused() {
mH.play_podcast_btn.setImageResource(R.drawable.play_button_overlay);
}
@Override
public void podcastComplete() {
mH.podCastPlaying = false;
}
});
}
else
{
((MainActivity) context).togglePlayPause();
}
以及上面调用的函数的代码
public MainActivity preparePodcast(String path, String podcastTitle, String channelName, String date_time_string, String desc)
{
pod_loading_indicator.setVisibility(View.VISIBLE);
PodcastPlayerService.preparePlayer("http://lbc.audioagain.com/shared/audio/" + path); //boils down to mediaplayer.prepareAsync();
current_podcast_tv.setText(podcastTitle);
channel_name_tv.setText(channelName);
episode_date_time.setText(date_time_string);
episode_description.setText(desc);
return this;
}