想要按顺序播放声音文件列表

时间:2017-10-19 04:22:49

标签: android android-mediaplayer

我正在尝试使用mid播放一组MediaPlayer类型的文件。但我面临着两个问题。

1)我无法控制正在播放的每个文件的持续时间。 (播放第一个和第二个mid文件之间存在间隙,等等。)

2)我无法阻止介于两者之间的循环。(我已尝试过一些解决办法,但如果正在播放某个文件,则不会停止。)

这是我的代码:

private MediaPlayer player;
    private int[][] arohRagaTones = {{R.raw.s, R.raw.r1, R.raw.m1, R.raw.p, R.raw.d1, R.raw.s},
            {R.raw.s, R.raw.r2_g1, R.raw.r3_g2, R.raw.m1, R.raw.d2_n1, R.raw.s},
            {R.raw.s, R.raw.r2_g1, R.raw.r3_g2, R.raw.m1, R.raw.p, R.raw.d2_n1, R.raw.d3_n2, R.raw.s},
            {R.raw.s, R.raw.r3_g2, R.raw.m1, R.raw.d1, R.raw.d3_n2, R.raw.s},
            {R.raw.s, R.raw.r2_g1, R.raw.g3, R.raw.p, R.raw.d2_n1, R.raw.s},
            {R.raw.s, R.raw.r1, R.raw.m1, R.raw.p, R.raw.d1, R.raw.s},
            {R.raw.s, R.raw.r1, R.raw.r3_g2, R.raw.p, R.raw.d1, R.raw.s},
            {R.raw.s, R.raw.r2_g1, R.raw.g3, R.raw.p, R.raw.n3, R.raw.s},
            {R.raw.s, R.raw.r2_g1, R.raw.m1, R.raw.p, R.raw.d2_n1, R.raw.s},
            {R.raw.s, R.raw.r2_g1, R.raw.g3, R.raw.m1, R.raw.p, R.raw.d2_n1, R.raw.s}};

player = MediaPlayer.create(getApplicationContext(), arohRagaTones[musicPosition][currentArohTrack]);
            player.setOnCompletionListener(arohListener);
            player.start();

private MediaPlayer.OnCompletionListener arohListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.release();
            if (currentArohTrack < arohRagaTones[musicPosition].length) {
                currentArohTrack++;
                try {
                    mediaPlayer = MediaPlayer.create(getApplicationContext(), arohRagaTones[musicPosition][currentArohTrack]);
                    Log.e(TAG, arohRagaTones[musicPosition][currentArohTrack] + "");
                    mediaPlayer.setOnCompletionListener(arohListener);
                    if (isRagaPlaying) {
                        mediaPlayer.start();
                    } else {
                        mediaPlayer.stop();
                        mediaPlayer.release();
                        pauseRaga.setVisibility(View.GONE);
                        playRaga.setVisibility(View.VISIBLE);
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                    e.printStackTrace();
                    pauseRaga.setVisibility(View.GONE);
                    playRaga.setVisibility(View.VISIBLE);
                }
            } else {
                pauseRaga.setVisibility(View.GONE);
                playRaga.setVisibility(View.VISIBLE);
            }
        }
    };

所有这些mid文件都是小钢琴音符,持续时间为1秒或1.5秒。请建议我如何更好地实施并解决这两个主要问题?

0 个答案:

没有答案