0

我想在按钮单击时将音乐从设备音乐播放器切换到下一个,我尝试了代码

    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);
             }

        }
    }) ;

但它没有显示任何变化。我必须添加任何权限。有人请帮我整理一下。

4

1 回答 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();
   }

           }
于 2013-04-15T10:01:02.877 回答