同一活动中的多个音轨

时间:2016-08-09 13:15:30

标签: android android-mediaplayer

我正在创建一个音频播放应用程序。我目前刚刚添加了两首曲目,但最终我需要添加更多...我目前创建了两个不同的媒体播放器来运行两个曲目,但是随着使用此过程增加曲目数量将导致问题和缺乏表现!

有什么方法可以更好,更通用地编写代码吗?

这是java代码

public class ChantsFragment extends Fragment {

    TextView text1, text2
    ImageButton play1, play2, pause1, pause2, repeatoff1, repeatoff2, repeaton1, repeaton2, stop1, stop2;
    MediaPlayer mediaPlayer1, mediaPlayer2;
    Toast on, off;

    public ChantsFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chants, container, false);


        play1 = (ImageButton) view.findViewById(R.id.play1);
        play2 = (ImageButton) view.findViewById(R.id.play2);
        pause1 = (ImageButton) view.findViewById(R.id.pause1);
        pause2 = (ImageButton) view.findViewById(R.id.pause2);
        repeatoff1 = (ImageButton) view.findViewById(R.id.repeatoff1);
        repeatoff2 = (ImageButton) view.findViewById(R.id.repeatoff2);
        repeaton1 = (ImageButton) view.findViewById(R.id.repeaton1);
        repeaton2 = (ImageButton) view.findViewById(R.id.repeaton2);
        stop1 = (ImageButton) view.findViewById(R.id.stop1);
        stop2 = (ImageButton) view.findViewById(R.id.stop2);

        mediaPlayer1 = MediaPlayer.create(getActivity(), R.raw.audio1);
        mediaPlayer2 = MediaPlayer.create(getActivity(), R.raw.audio2);

        on = Toast.makeText(getActivity(), "The chant will repeat itself", Toast.LENGTH_LONG);
        off = Toast.makeText(getActivity(), "The chant will no longer repeat itself", Toast.LENGTH_LONG);

        //Track 1

        text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) {
                    text1.setClickable(false);
                }

                if (mediaPlayer2 != null && mediaPlayer2.isPlaying()) {
                    mediaPlayer2.stop();
                    mediaPlayer2.release();
                    mediaPlayer2 = null;
                    play2.setVisibility(View.GONE);
                    pause2.setVisibility(View.GONE);
                    repeatoff2.setVisibility(View.GONE);
                    repeaton2.setVisibility(View.GONE);
                    stop2.setVisibility(View.GONE);
                }

                if (mediaPlayer1 == null) {
                    mediaPlayer1 = MediaPlayer.create(getActivity(), R.raw.audio1);
                } else {
                    text1.setClickable(true);
                    mediaPlayer1.start();
                    play1.setVisibility(View.GONE);
                    pause1.setVisibility(View.VISIBLE);
                    repeatoff1.setVisibility(View.VISIBLE);
                    stop1.setVisibility(View.VISIBLE);
                }

                mediaPlayer1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        play1.setVisibility(View.GONE);
                        pause1.setVisibility(View.GONE);
                        repeatoff1.setVisibility(View.GONE);
                        repeaton1.setVisibility(View.GONE);
                        stop1.setVisibility(View.GONE);
                    }
                });
            }
        });


        play1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play1.setVisibility(View.GONE);
                pause1.setVisibility(View.VISIBLE);
                mediaPlayer1.start();
            }
        });

        pause1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play1.setVisibility(View.VISIBLE);
                pause1.setVisibility(View.GONE);
                mediaPlayer1.pause();
            }
        });

        repeatoff1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer1.setLooping(true);
                repeatoff1.setVisibility(View.GONE);
                repeaton1.setVisibility(View.VISIBLE);
                on.show();
                off.cancel();
            }
        });

        repeaton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer1.setLooping(false);
                repeaton1.setVisibility(View.GONE);
                repeatoff1.setVisibility(View.VISIBLE);
                off.show();
                on.cancel();
            }
        });

        stop1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer1.stop();
                mediaPlayer1 = null;
                text1.setClickable(true);
                play1.setVisibility(View.GONE);
                pause1.setVisibility(View.GONE);
                repeatoff1.setVisibility(View.GONE);
                repeaton1.setVisibility(View.GONE);
                stop1.setVisibility(View.GONE);
            }
        });

        //Track 2

        text2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer2 != null && mediaPlayer2.isPlaying()) {
                    text2.setClickable(false);
                }

                if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) {
                    mediaPlayer1.stop();
                    mediaPlayer1.release();
                    mediaPlayer1 = null;
                    play1.setVisibility(View.GONE);
                    pause1.setVisibility(View.GONE);
                    repeatoff1.setVisibility(View.GONE);
                    repeaton1.setVisibility(View.GONE);
                    stop1.setVisibility(View.GONE);
                }

                if (mediaPlayer2 == null) {
                    mediaPlayer2 = MediaPlayer.create(getActivity(), R.raw.audio2);
                } else {
                    text2.setClickable(true);
                    mediaPlayer2.start();
                    play2.setVisibility(View.GONE);
                    pause2.setVisibility(View.VISIBLE);
                    repeatoff2.setVisibility(View.VISIBLE);
                    stop2.setVisibility(View.VISIBLE);
                }

                mediaPlayer2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        play2.setVisibility(View.GONE);
                        pause2.setVisibility(View.GONE);
                        repeatoff2.setVisibility(View.GONE);
                        repeaton2.setVisibility(View.GONE);
                        stop2.setVisibility(View.GONE);
                    }
                });
            }
        });

        play2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play2.setVisibility(View.GONE);
                pause2.setVisibility(View.VISIBLE);
                mediaPlayer2.start();
            }
        });

        pause2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play2.setVisibility(View.VISIBLE);
                pause2.setVisibility(View.GONE);
                mediaPlayer2.pause();
            }
        });

        repeatoff2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer2.setLooping(true);
                repeatoff2.setVisibility(View.GONE);
                repeaton2.setVisibility(View.VISIBLE);
                on.show();
                off.cancel();
            }
        });

        repeaton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer2.setLooping(false);
                repeaton2.setVisibility(View.GONE);
                repeatoff2.setVisibility(View.VISIBLE);
                off.show();
                on.cancel();
            }
        });

        stop2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer2.stop();
                mediaPlayer2 = null;
                text2.setClickable(true);
                play2.setVisibility(View.GONE);
                pause2.setVisibility(View.GONE);
                repeatoff2.setVisibility(View.GONE);
                repeaton2.setVisibility(View.GONE);
                stop2.setVisibility(View.GONE);
            }
        });

        return view;
    }

修改

我目前已将其大约10个曲目添加到应用程序中。我希望播放在当前曲目(如果正在播放)上停止选择另一首曲目并使用媒体播放器执行相同操作意味着单独检查是否正在播放9个媒体播放器! (当然除非我完全错过了另一种方法)

1 个答案:

答案 0 :(得分:0)

好吧,为了一次播放多个音频文件,SoundPool似乎是你最好的选择,主要是因为它更轻巧,你可以控制流的数量。您可以创建一个新的SoundPool实例:

SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100); // 10 = maxStreams, 100 = quality

然后您可以开始将不同的声音加载到其中:

int soundId1 = soundPool.load(context, R.raw.audio1, 1);
int soundId2 = soundPool.load(context, R.raw.audio2, 1);
...

您可以将播放控制为:

int idBeingPlayed = soundPool.play (soundID, 1, 1, 1, -1, 1); // volume params, returns the streamId

其他控件:

soundPool.pause(streamID);
soundPool.resume(streamID);

有关详细信息,请访问offfical docs。希望这有帮助!