I have a series of ListView rows, backed by a simple ViewHolder assisted ArrayAdapter, each row has a 'Play/Pause' button, which pause and resumes a MediaPlayer in a service,
However, tapping one of the buttons causes the music to start, tapping any subsequent button plays and pauses the original button, not the one i'm actually clicking, i've never had such an issue before?
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);
}
and
static class ViewHolder {
ImageButton play_podcast_btn;
}
and
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();
}
}
});
Callbacks:
In MediaPlayer Service: (BusProvider provides a call back to the activity that started service: below)
public void pause() {
mediaPlayer.pause();
BusProvider.getInstance().post(new PodcastPausedEvent(true));
}
public void play() {
mediaPlayer.start();
BusProvider.getInstance().post(new PodcastPlayedEvent(true));
}
MainActivity: (is notified of the pause and triggers another callback, this time to the list row)
@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);
}
and in the 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();
}
and the code the for the function called above
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;
}