如何用Java顺序播放音频文件? [英] How do you play audio file sequentially in Java?

查看:176
本文介绍了如何用Java顺序播放音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此对于我正在开发的应用程序,我有一堆音频文件,这些音频文件可能会播放,也可能不会播放(取决于某些变量的状态)(播放顺序也可能会更改).

So for an app I'm developing I have a bunch of audio files that, depending on the state of some variables, may or may not play (and the order in which they play can change as well).

我了解setOnCompletionListener的基础知识,但是我不知道使用它的最佳方法.截至目前,如果我要播放audio1,audio2,然后是audio3,我的代码将设置如下:

I understand the basics of the setOnCompletionListener, but I can't figure out the best way to use it. As of right now, if I want to play audio1, audio2, then audio3, my code is set up as follows:

 clipSetup(1);
 aP.setOnCompleteListener(new MediaPlayer.OnCompletionListener(){
      @Override
      public void onCompletion(MediaPlayer mp){
           clipSetup(2);
           aP.setOnCompleteListener(new MediaPlayer.OnCompletionListener(){
                @Override
                public void onCompletion(MediaPlayer mp){
                    clipSetup(3);
                }
           });
      }
 });

 private MediaPlayer clipSetup(int i){
      switch (i){
           case 1:
                aP = MediaPlayer.create(this, R.raw.audio1);
                aP.start();
                break;
           case 2:
                aP = MediaPlayer.create(this, R.raw.audio2);
                aP.start();
                break;
           case 3:
                aP = MediaPlayer.create(this, R.raw.audio3);
                aP.start();
                break;
       }
       return aP;
 }

这行得通,但是非常糟糕,特别是当出于某些目的我必须播放15个以上的文件时.有什么更好的方法可以实现我所拥有的目标?

This works, but its ugly as all hell, especially when I have to play upwards of 15 files for some purposes. Is there a better way of accomplishing what I've got above?

推荐答案

我以前没有使用过音频文件,但这是我最好的镜头

I have not worked with audio files before, but here is my best shot

这是一个很好的起点.这仅允许您将文件添加到一个缓冲区中,一个接一个地播放.调用addInitFile()启动音频文件,然后调用addFile()添加更多文件以在第一个文件之后继续播放.仅仅是一个开始,代码未经测试就被扔在一起了

It is a good starting idea. This only allows you to add files to a buffer that will play one after another. Call addInitFile() to start an audio file, and then call addFile() to add more files to keep playing after the first one. It is just a start and code that was thrown together without testing

可能是更好的代码,但这只是一个开始.至少比案例切换要好

It could be much better code, but its just a start. at least better than a case switch

出于可访问性的目的,我将其设为内部类

I would make this an inner class for accessibility purposes

public class CustomMediaBuffer{
    public static ArrayList<Integer> buffer = new ArrayList<Integer>();

    public static void addInitFile(final int res){
        buffer.add(res);
        ap = MediaPlayer.create(this, res);
        ap.start();
        ap.setOnCompleteListener(new MediaPlayer.OnCompletionListener(){
            public void onCompletion(MediaPlayer mp){
                int indx = buffer.indexOf(res);
                if(buffer.size()-1>index){
                    ap = MediaPlayer.create(this, buffer.get(indx+1));
                    ap.start();
                }
            }
        });
    }

    public static void addFile(int res){
        buffer.add(res);
    }



}

这是更好的,更新的代码:

Here is the better, updated code:

public class CustomMediaBuffer{
    public static ArrayList<MediaPlayer> buffer = new ArrayList<MediaPlayer>();

    public static void addInitFile(final int res){
        final MediaPlayer mp= MediaPlayer.create(this, res );
        buffer.add(mp);
        ap = buffer.get(0);
        ap.start();
        ap.setOnCompleteListener(new MediaPlayer.OnCompletionListener(){
            public void onCompletion(MediaPlayer media){
                int indx = buffer.indexOf(mp);
                if(buffer.size()-1>index){
                    ap = buffer.get(indx+1));
                    ap.start();
                }
            }
        });
    }

    public static void addFile(final int res){
        final MediaPlayer mp= MediaPlayer.create(this, res );
        mp.setOnCompleteListener(new MediaPlayer.OnCompletionListener(){
            public void onCompletion(MediaPlayer media){
                int indx = buffer.indexOf(mp);
                if(buffer.size()-1>index){
                    ap = buffer.get(indx+1));
                    ap.start();
                }
            }
        });
        buffer.add(mp);

    }

}

这篇关于如何用Java顺序播放音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆