0

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;
}
Broak
  • 4,161
  • 4
  • 31
  • 54
  • Seems like many references like play_pause_button , mH are member level in this code and are bound to cause the problem. You should try refactoring. – ksarmalkar Jul 17 '14 at 13:24
  • @ksarmalkar to quote myself on from another post: i SEEM to have done it. Basically i declare a variable at the top of the class: 'currentlyPlayingPodcast' I have a variable declared in my ViewHolder called : currentPodcastPos in the GetView i set the value of this currentPodcastPos to the current position of the list. I set the button's tag with an instance of my viewholder (so i can access the currentPodcastPos from inside my onclick/callback) Then simply if(currentlyPlayingPodcast == holder.currentPodcastPos) then toggle the play pause, otherwise start the new song. Thanks! – Broak Jul 18 '14 at 00:52
  • Oh Thats nice. Good you figured it out. It was difficult to predict without the entire file. – ksarmalkar Jul 18 '14 at 15:08

0 Answers0