【问题标题】:how to shuffle list song with Media Player android如何使用 Media Player android 随机播放列表歌曲
【发布时间】:2017-05-23 06:07:11
【问题描述】:

我已经在媒体播放器中随机播放歌曲列表,但没有正确的工作请帮助:

@Override
  public void onCompletion(MediaPlayer mediaPlayer) {

        if (isShuffle) {
          if (intent.hasExtra("songIndex")) {
            Random random = new Random();
            songIndex = random.nextInt((songList.size() - 1) + 1);
            playSongWithIndex(songIndex);
          }
        }
      }

以及播放歌曲的方法:

public void playSongWithIndex(final int songIndex) {
    progressBar.setVisibility(View.VISIBLE);
    if (music_exist.exists()) {
      playMusicOfflineMode();
    } else {
      Log.e("TAG", "playSongWithIndex: "+songList.get(songIndex).getFile_128() );
      try {
        G.mediaPlayer.reset();
        G.mediaPlayer.setDataSource(songList.get(songIndex).getFile_128());
        G.mediaPlayer.prepareAsync();
        G.mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
          @Override
          public void onPrepared(MediaPlayer mediaPlayer) {
            runOnUiThread(new Runnable() {
              @Override
              public void run() {
                setAdapterLyric(songManager.showTextInRecyclerView(songList.get(songIndex).getLyric()));

              }
            });
            G.mediaPlayer.start();
            music_name_text.setText(songList.get(songIndex).getEn_name());
            singer_name_text.setText(songList.get(songIndex).getSinger_name_en());
            comment_count_text.setText(songList.get(songIndex).getComment_count() + "");
            like_count_text.setText(songList.get(songIndex).getLike_count() + "");
            play_music_img.setImageResource(R.drawable.ic_pause_black_24dp);
            seekBar.setMax(G.mediaPlayer.getDuration());
            progressBar.setVisibility(View.INVISIBLE);
            play_music_img.setEnabled(true);
            timer = new Timer();
            timer.schedule(new MainTimer(), 0, 1000);

          }
        });
      } catch (IllegalArgumentException | IllegalStateException | IOException e) {
        e.printStackTrace();
      }
    }
  }

我遇到了这个日志:

E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/04_Let's_Talk_About_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/03_Don't_Give_Up.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/04_Let's_Talk_About_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/03_Don't_Give_Up.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/04_Let's_Talk_About_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/01_Cheri_Cheril_Lady.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/01_Cheri_Cheril_Lady.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/02_With_A_Little_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/03_Don't_Give_Up.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/02_With_A_Little_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/03_Don't_Give_Up.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/02_With_A_Little_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/02_With_A_Little_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/02_With_A_Little_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/03_Don't_Give_Up.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/03_Don't_Give_Up.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/01_Cheri_Cheril_Lady.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/01_Cheri_Cheril_Lady.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/04_Let's_Talk_About_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/01_Cheri_Cheril_Lady.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/04_Let's_Talk_About_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/01_Cheri_Cheril_Lady.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/02_With_A_Little_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/03_Don't_Give_Up.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/01_Cheri_Cheril_Lady.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/02_With_A_Little_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/01_Cheri_Cheril_Lady.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/02_With_A_Little_Love.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/03_Don't_Give_Up.mp3
E/TAG: playSongWithIndex: http://cdn.oneremind.com/up/song/Modern_Talking/Let's_Talk_About_Love/04_Let's_Talk_About_Love.mp3

媒体播放器卡在一个循环中,无法正常工作我的问题在哪里?

【问题讨论】:

  • 你可以像这样使用 Collections.shuffle(arrlist);

标签: android loops media-player shuffle


【解决方案1】:

使用集合 shullfe:

long seed = System.nanoTime();
Collections.shuffle(songList, new Random(seed));

【讨论】:

    【解决方案2】:

    为什么不用这种方式创建媒体播放器?

     G.mediaPlayer = MediaPlayer.create(this,Uri.parse("YourFilePath");//this is a context
     //Then start it without preparation.
     G.mediaPlayer.start();
    

    【讨论】:

    • 你能告诉我你在尝试这个方法时遇到了什么错误吗?
    • 我的意思是试试我写的代码,如果你试过了但没有用,请写下错误。
    • 您的代码一次在第二次媒体播放器中工作,完成后不再播放if (isShuffle) { if (intent.hasExtra("songIndex")) { Random random = new Random(); songIndex = random.nextInt((songList.size() - 1) + 1); G.mediaPlayer = MediaPlayer.create(this,Uri.parse(songList.get(songIndex).getFile_128())); G.mediaPlayer.start(); } }
    • 尝试停止并释放播放器,然后使用代码。
    • 再次设置监听解决G.mediaPlayer.setOnCompletionListener(this)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多