Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mediaplayer.isPlaying always gets false

I am currently working on Chat application. In this there is feature of sending voice notes. The sending and playing of voice notes is working fine. But the problem is that when I play the voice notes, the previous voice notes playing not stoping.. both are playing together.

here is my code for playing voice notes,

 public static class MessageViewHolder extends RecyclerView.ViewHolder {

    private TextView messageTextView;
    private ImageView messageImageView;
    private TextView timeTextView;
    private LinearLayout textMessageLayout;
    private TextView messengerNameTextView;
    private CircleImageView messengerPhotoImageView;
    ImageView leftQuackImage, rightQuackImage;
    private LinearLayout voiceNotesLayout;
    private ImageView playImageView, stopImageView;
    private SeekBar progressSeekbar;

    private MediaPlayer mPlayer;
    double timeElapsed = 0, finalTime = 0;
    Handler durationHandler = new Handler();
    Runnable updateSeekBarTime;
    boolean isPlaying;

    public MessageViewHolder(View itemView) {
        super(itemView);
        messageTextView = (TextView) itemView.findViewById(R.id.messageTextView);
        messageImageView = (ImageView) itemView.findViewById(R.id.messageImageView);
        timeTextView = (TextView) itemView.findViewById(R.id.timeTextView);
        textMessageLayout = (LinearLayout) itemView.findViewById(R.id.textMessageLayout);
        messengerNameTextView = (TextView) itemView.findViewById(R.id.messengerTextView);
        messengerPhotoImageView = (CircleImageView) itemView.findViewById(R.id.messengerImageView);
        leftQuackImage = (ImageView) itemView.findViewById(R.id.leftQuackImage);
        rightQuackImage = (ImageView) itemView.findViewById(R.id.rightQuackImage);
        voiceNotesLayout = (LinearLayout) itemView.findViewById(R.id.voiceNotesLayout);
        playImageView = (ImageView) itemView.findViewById(R.id.playImageView);
        stopImageView = (ImageView) itemView.findViewById(R.id.stopImageView);
        progressSeekbar = (SeekBar) itemView.findViewById(R.id.progressSeekbar);
        isPlaying = false;
        mPlayer = new MediaPlayer();
    }
    public  void setupAudioPlay(final long duration, final String url){
        progressSeekbar.setMax((int) duration);
        progressSeekbar.setClickable(false);
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mPlayer.setDataSource(url);
            mPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (IllegalStateException e){
            e.printStackTrace();
        }

        playImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("Media player","isPlaying"+mPlayer.isPlaying()+isPlaying);
                if (mPlayer.isPlaying()) {
                    mPlayer.stop();
                    mPlayer.release();
                    mPlayer = null;
                }
                if (!isPlaying) {
                    isPlaying = true;
                    mPlayer.start();
                    playImageView.setVisibility(View.GONE);
                    stopImageView.setVisibility(View.VISIBLE);
                    timeElapsed = mPlayer.getCurrentPosition();
                    progressSeekbar.setProgress((int) timeElapsed);
                    durationHandler.postDelayed(updateSeekBarTime, 100);

                } else {
                    isPlaying = false;
                    mPlayer.pause();
                }
            }
        });

        stopImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isPlaying) {
                    if (stopPlaying()) {
                        playImageView.setVisibility(View.VISIBLE);
                        stopImageView.setVisibility(View.GONE);
                    }
                }
            }
        });

        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (mp != null) {
                    mPlayer.seekTo(0);
                    isPlaying = false;
                    progressSeekbar.setProgress(0);
                }
                playImageView.setVisibility(View.VISIBLE);
                stopImageView.setVisibility(View.GONE);
            }
        });

        updateSeekBarTime = new Runnable() {
            public void run() {
                if (mPlayer != null) {
                    if (mPlayer.isPlaying()) {
                        timeElapsed = mPlayer.getCurrentPosition();
                        progressSeekbar.setProgress((int) timeElapsed);
                        durationHandler.postDelayed(this, 100);
                    } else {
                        mPlayer.pause();
                        isPlaying = false;
                        progressSeekbar.setProgress(0);
                        playImageView.setVisibility(View.VISIBLE);
                        stopImageView.setVisibility(View.GONE);
                    }
                }
            }
        };
    }

    private boolean stopPlaying() {
        mPlayer.pause();
        mPlayer.seekTo(0);
        if (!mPlayer.isPlaying()) {
            return true;
        }
        return false;
    }
}

here the mPlayer.isPlaying always gets false.

Please Help me!

like image 537
Alphonsa Augustine Avatar asked Oct 24 '17 10:10

Alphonsa Augustine


2 Answers

From the documentation:

When the call to pause() returns, the MediaPlayer object enters the Paused state. Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine. It may take some time before the state is updated in calls to isPlaying(), and it can be a number of seconds in the case of streamed content.

This may be the case here. The reasons why sound is not stopping and why isPlaying() returns true might be different. For example multiple instances of MediaPlayer might exist in parallel, making mPlayer static would exclude such an option.

like image 66
lextiz Avatar answered Sep 30 '22 22:09

lextiz


You can check the system Audio status whenever it is necessary. In your case, whenever the play button is clicked the condition below should be checked first.

AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive()) 
{
    // to do
}

In to do part you should implement the primary action which is:

KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE);
audioManager.dispatchMediaKeyEvent(event);

You can find the whole documentation here

like image 23
Fio Avatar answered Sep 30 '22 22:09

Fio