0

I want to switch the music from the device music player to the to the next on Button click,I tried the code

    changeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
            synchronized (this) {
                        i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
                        sendOrderedBroadcast(i, null);

                        i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
                        sendOrderedBroadcast(i, null);
             }

        }
    }) ;

But it Doesn't show any change.Is there any permission which I have to add. Somebody please help me to sort it out.

1 Answer 1

0

you can pass song index directly on click method to change song.

     changeButton.setOnClickListener(new View.OnClickListener() {

         @Override
    public void onClick(View v) {
   playSong(currentSongIndex+1);

     }};

     //playSong(int songIndex) method definition here

  public String playSong(int songIndex){
        // Play song

        try {
          mp.reset();
             mp.setDataSource(songsList.get(songIndex).get("songPath"));

      mp.prepareAsync();
     } catch (IllegalArgumentException e) {
          e.printStackTrace();
   }

           }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.