Stef
Stef

Reputation: 201

Media player not stopping old song when new song is selected. Throws a null pointer exception also

Below code throws a null pointer exception at the "if" Condition. I have a list of songs and on click on an item I want that song to be played and while clicking next item the previous song should stop playing and the newly selected song should get played. Could someone please help me???

MediaPlayer player = null;
    public void ringtonelistview() {
        final CharSequence[] ringtoneList = { "city_nights.mp3",
                "cute_morning_alarm.mp3", "melody_alarm_clock.mp3",
                "morning_alarm.mp3", "powiadomienie.mp3", "silver.mp3" };

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setTitle("Select one Ringtone");
        alt_bld.setSingleChoiceItems(ringtoneList, -1,
                new DialogInterface.OnClickListe`enter code here`ner() {
                    public void onClick(DialogInterface dialog, int item) {
                        final Uri myUri;
                        ringtoneName = (String) ringtoneList[item];
                        myUri = Uri.parse(getExternalFilesDir("").toString()
                                + "/RINGTONEFOLDER/" + ringtoneName);
                        try {
                            if (null != player || player.isPlaying() == true) {
                                player.stop();
                                player.release();
                            } else {
                                player = MediaPlayer.create(
                                        MainActivity.this,
                                        myUri);
                                player.start();
                                player = null;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

Upvotes: 0

Views: 922

Answers (1)

Nguyen Thanh An
Nguyen Thanh An

Reputation: 269

try this code :

if(mediaplauer != null)
   if(mediaplayer.isplaying())
   {
     mediaplayer.stop();
     // start your new song
   }else{
     // start your new song
   }

Upvotes: 1

Related Questions