Switch to mp3 after stopping

Asked by At

I can start and stop while playing mp3. But I can't start the other one before the mp3 ends. How can I solve this problem

mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    progressDialog = new ProgressDialog(context);
    viewHolder.playicon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!InternetKontrol()){
                Toast.makeText(context, "İnternet Bağlantınızı Kontrol Ediniz", Toast.LENGTH_SHORT).show();
            }else {


                if (!playPause) {

                    if (initialStage) {

                            new Player().execute("https://tevhidmeali.com/assets/mp3/ar/ayet/husari/" + list.get(i).getAyet_mp3_ar());

                    } else {
                        if (!mediaPlayer.isPlaying()) {

                            mediaPlayer.start();
                            viewHolder.playicon.setImageResource(R.drawable.mp3_pause_icon);
                        }
                    }
                    viewHolder.playicon.setImageResource(R.drawable.mp3_pause_icon);

                    playPause = true;

                } else {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                        viewHolder.playicon.setImageResource(R.drawable.mp3_play_icon);
                    }
                    viewHolder.playicon.setImageResource(R.drawable.mp3_play_icon);

                    playPause = false;
                }

            }

        }
    });

AsyncTask

class Player extends AsyncTask<String, Void, Boolean>{
    @Override
    protected Boolean doInBackground(String... strings) {
        Boolean prepared = false;

        try{
            mediaPlayer.setDataSource(strings[0]);

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    initialStage = true;
                    playPause = false;
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                }
            });

            mediaPlayer.prepare();
            prepared = true;
        }catch (Exception e){
            Log.e("MyAudioStream",e.getMessage());
            prepared = false;
        }
        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        if (progressDialog.isShowing()){
            progressDialog.cancel();
        }

            mediaPlayer.start();


        initialStage = false;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog.setMessage("Yükleniyor");
        progressDialog.show();
    }
}

............................... ............................... ............................... ............................... ............................... ...............................

0 Answers