KamelK
KamelK

Reputation: 71

Android - Mediaplayer next button doesn't work

I am working on a live streaming radio Android app...i have an array with 4 tracks, when i reach the end of the list suppose that the next button will represent " the last song" but it doesn't and crashes the app.

Here is the button code:

    counter=0;

    mediaPlayer=new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    btn_next.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View v) {
             if (counter < songurl.length) {
                 counter = counter + 1;
                 textview.setText(songurl[counter]);
                 try {
                     mediaPlayer.reset();
                 } catch (Exception ex) {
                     try {
                         mediaPlayer.setDataSource(songurl[counter]);
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                     try {
                         mediaPlayer.prepare();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                     mediaPlayer.start();
                 }
             } else {

                 Toast.makeText(MainActivity.this, "last song", Toast.LENGTH_SHORT).show();
             }
         }
    });

One more question... in case I want to make it a closed loop when the app reaches the last song and I want to press next to start the list from the beginning how can I do it? Thanks.

Upvotes: 1

Views: 143

Answers (4)

Neeraj Jha
Neeraj Jha

Reputation: 123

You can simply add a modulo (%) operator in your code

counter = counter++ % songurl.length

no need to add an extra if else block

btn_next.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        counter = counter++ % songurl.length
        textview.setText(songurl[counter]);
        try {
            mediaPlayer.reset();
        } catch (Exception ex) {
            try {
                mediaPlayer.setDataSource(songurl[counter]);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.start();
        }

    }
});

Upvotes: 3

Jerry.L
Jerry.L

Reputation: 46

You see you called mediaPlayer.setDataSource(songurl[counter]) in the catch block. what if it will not have exception when you call mediaPlayer.reset()

try this:

try{
    mediaPlayer.reset();
    mediaPlayer.setDataSource();
    mediaPlayer.prepareAsync();--or mediaPlayer.prepare();
    (It depends on whether you load the url on the internet)
    mediaPlayer.start(); -- if you call mediaPlayer.prepareAsync() you should call mediaPlayer.start() at the OnPreparedListener;

 mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
}

And if you still wondering, I suggest you to read something about MediaPlay's state.

PS If mediaPlayer is playing ,and you wanna let it play another one ,call mediaPlayer.stop() first;

Upvotes: 0

Dave
Dave

Reputation: 4291

One problem you have is that you are incrementing the counter just after you check that it is within the bounds, which means your safety check is useless. Move counter = counter + 1; to the end of the if statement.

As far as making it loop continuously, you can change it to counter = (counter + 1) % songurl.length; at the end of the if statement body.

Beyond the scope of the question you asked, there are some problems with your usage of MediaPlayer. It doesn't make any sense to have so much key logic in a catch statement. That means you expect the exception to happen. You should re-think what you are doing there.

Upvotes: 0

Aman Verma
Aman Verma

Reputation: 3325

To make the loop you should check in the onclicklistener that the if the counter is equal to the songurl.length which is i think the total number of song in the playlist.

int counter=0;

    MediaPlayer mediaPlayer=new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    btn_next.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if (counter < songurl.length) {
                counter = counter + 1;
                textview.setText(songurl[counter]);
                try {
                    mediaPlayer.reset();
                } catch (Exception ex) {
                    try {
                        mediaPlayer.setDataSource(songurl[counter]);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        mediaPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.start();
                }
            } else if(counter == songurl.length) {

                counter = 0;
               // Toast.makeText(MainActivity.this, "last song", Toast.LENGTH_SHORT).show();
                textview.setText(songurl[counter]);
                try {
                    mediaPlayer.reset();
                } catch (Exception ex) {
                    try {
                        mediaPlayer.setDataSource(songurl[counter]);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        mediaPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.start();
                }
            }
        }
    });

in the elseif condition i have set the counter to 0. so that it will get in the loop.

Upvotes: -1

Related Questions