同时播放和暂停多个音频文件

时间:2017-07-14 00:43:30

标签: java android android-mediaplayer

我正在开发一个音乐播放器应用程序,在我的应用程序中我想在单击按钮上播放所有mp3文件。我能够使用MediaPlayer,但无法使用“暂停”按钮暂停所有歌曲。如何暂停所有播放的歌曲

播放按钮

  for (int i = 0; i < InstrumentCountSize; i++) {
        mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setDataSource(instruments_count.get(i));
        mp.prepare();
        mp.start();

    }

暂停按钮

   if (mp != null) {
        try {
            mp.stop();
            mp.reset();
            mp.release();
            mp = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:1)

您的单个​​MediaPlayer变量只能引用一个MediaPlayer,因此您的暂停按钮代码只会尝试释放您创建的最后 MediaPlayer个实例。您需要保留对所有// This needs to replace your "mp" variable. List<MediaPlayer> mps = new ArrayList<MediaPlayer>(); // Play button code .... // Make sure all the media are prepared before playing for (int i = 0; i < InstrumentCountSize; i++) { MediaPlayer mp = new MediaPlayer(); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setDataSource(instruments_count.get(i)); mp.prepare(); mps.add(mp); } // Now that all the media are prepared, start playing them. // This should allow them to start playing at (approximately) the same time. for (MediaPlayer mp: mps) { mp.start(); } // Pause button code ... for (MediaPlayer mp: mps) { try { mp.stop(); mp.reset(); mp.release(); } catch (Exception e) { e.printStackTrace(); } } mps.clear(); 个实例的引用。

[增订]

这样的事情会更好:

sfood -i -r myscript.py | sfood-cluster > dependencies.txt