如何使用MediaPlayer开始

如何使用MediaPlayer开始

通过单击“停止”按钮停止上一个曲目后,需要选择另一个随机曲目

我正在通过单击“播放”按钮播放随机轨道,通过单击“停止”停止它,然后我需要再次随机化,换句话说,选择另一个轨道。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button play36 = (Button) findViewById(R.id.threesix);
    Button stop = (Button) findViewById(R.id.stop);

    String[] listOfFiles = new String[0];
    try {
        listOfFiles = getAssets().list("");
        final List<String> musicOnlyList = new ArrayList<>();

        for (int i = 0; i < listOfFiles.length; i++) {
            if (getExtension(listOfFiles[i]).equals("mp3"))
                musicOnlyList.add(listOfFiles[i]);
        }

        final MediaPlayer mediaPlayer = new MediaPlayer();

        play36.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int itemIndex = new Random().nextInt(musicOnlyList.size());
                String file = musicOnlyList.get(itemIndex);
                AssetFileDescriptor afd = null;
                try {
                    afd = getAssets().openFd(file);
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.stop();
                mediaPlayer.reset();

            }
        });

    } catch (IOException e) {
        e.printStackTrace();
    }


在这种情况下,我每次启动和停止播放器时都会获得相同的音轨,但是每次都需要通过单击“播放”按钮来进行随机化

最佳答案

您需要在play36的监听器中选择一个新的随机数:

Button play36 = (Button)findViewById(R.id.threesix);
Button stop = (Button)findViewById(R.id.stop);

String[] listOfFiles = new String[0];
try {
    listOfFiles = getAssets().list("");
    final List<String> musicOnlyList = new ArrayList<>();

    for(int i = 0; i < listOfFiles.length; i++){
        if (getExtension(listOfFiles[i]).equals("mp3"))
            musicOnlyList.add(listOfFiles[i]);
    }

    final MediaPlayer mediaPlayer = new MediaPlayer();

    play36.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int itemIndex = new Random().nextInt(musicOnlyList.size());
            String file = musicOnlyList.get(itemIndex);
            AssetFileDescriptor afd = null;
            try {
                afd = getAssets().openFd(file);
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.start();
        }
    });

    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaPlayer.stop();
            try {
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

} catch (IOException e) {
    e.printStackTrace();
}

关于java - 如何使用MediaPlayer开始 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54164628/

10-10 02:55