I can not update the screen components

93 Views Asked by At

I have a list of players (ListView). Each item is have two buttons "start" and "stop". When I click on "start" color "Start" button change red and begins to move seekbar. When click on the "stop" button color "start" start is black and seekbar progress becomes zero. when the song ends with the color button "start" also becomes black and seekbar progress becomes zero.

 holder.start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                releaseMP();
                startPlayProgressUpdater(holder.seekBar, holder.start);
                try {
                    mediaPlayer = new MediaPlayer();
                    global_position = position;
                    mediaPlayer.setDataSource(recordBeans.get(global_position).getFile());
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mediaPlayer.prepareAsync();
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            holder.seekBar.setMax(mediaPlayer.getDuration());
                            mediaPlayer.start();
                            recordBeans.get(global_position).setPlay(true);
                            holder.start.setTextColor(Color.RED);
                            startPlayProgressUpdater(holder.seekBar, holder.start);
                        }
                    });

                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mediaPlayer) {
                            releaseMP();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

This is implemented in this method:

public void startPlayProgressUpdater(final SeekBar seekBar, final Button start) {
        if (mediaPlayer != null) {
            recordBeans.get(global_position).setSeekPos(mediaPlayer.getCurrentPosition());
            if ((Integer) seekBar.getTag() == global_position) {
                seekBar.setProgress(recordBeans.get(global_position).getSeekPos());
            }
            Runnable notification = new Runnable() {
                public void run() {
                    startPlayProgressUpdater(seekBar, start);
                }
            };
            handler.postDelayed(notification, 1000);
        } else {
            recordBeans.get(global_position).setPlay(false);
            start.setTextColor(Color.BLACK);
            recordBeans.get(global_position).setSeekPos(0);
            seekBar.setProgress(0);
        }
    }

When I click the "stop" or the song ends, the player becomes the NULL and color button again changes. it is implemented in all of these methods:

holder.stop.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if ((Integer) v.getTag() == global_position && mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
});

and

private void releaseMP() {
    if (mediaPlayer != null) {
        try {
            mediaPlayer.release();
            mediaPlayer = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I want the same functionality if I worked while playing the song click on the "start" another song. ie a new song starts playing, the color buttons "start" becomes A red color and previous button "start" again black.

for this I when you click on "start" call methods:

releaseMP();
startPlayProgressUpdater(holder.seekBar, holder.start);

and the method works but the data is not cleared. but if I click on "Stop" then the data of all previous songs cleared. what I'm doing wrong?

enter image description here

1

There are 1 best solutions below

2
On

I think you meant to call reset(), not release() in the stop button OnClick. Try to call release() in your main container OnStop()

reset()

This method resets the media player

release()

This method releases any resource attached with MediaPlayer object