在Android中按顺序播放多个声音

时间:2015-01-25 22:46:26

标签: android audio

我的问题是我有大约12个声音和相同数量的图像。我想要做的是用相应的图像播放声音,当声音结束时,用下一个图像播放下一个声音,依此类推,直到播放完所有声音,我的意思是,一个接一个,不是全部都在同一个时间。

所有这些都是短音,所以我使用的是SoundPool。

有人可以帮助我吗?提前谢谢。

这是代码,对不起,我忘了把它包括在内......

String[] flauta = new String[]{"flauta_do","flauta_re","flauta_mi","flauta_fa","flauta_sol","flauta_la","flauta_si","flauta_do_agudo","flauta_re_agudo","flauta_mi_agudo","flauta_fa_agudo","flauta_sol_agudo"};
String[] pentagrama = new String[]{"pentagrama_do","pentagrama_re","pentagrama_mi","pentagrama_fa","pentagrama_sol","pentagrama_la","pentagrama_si","pentagrama_do_agudo","pentagrama_re_agudo","pentagrama_mi_agudo","pentagrama_fa_agudo","pentagrama_sol_agudo"};
String[] nota = new String[]{"Do","Re","Mi","Fa","Sol","La","Si","Do alto","Re alto","Mi alto","Fa alto","Sol alto"};
String[] sonido = new String[]{"sonido_do","sonido_re","sonido_mi","sonido_fa","sonido_sol","sonido_la","sonido_si","sonido_do_agudo","sonido_re_agudo","sonido_mi_agudo","sonido_fa_agudo","sonido_sol_agudo"};

private SoundPool sPool;

ImageView flautaIV;
ImageView pentagramaIV;
TextView notaTV;

int resID = 0;
String recurso = "";
int sonidoNota = 0;



@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repasar);

        flautaIV = (ImageView)findViewById(R.id.flautaIV);
        pentagramaIV = (ImageView)findViewById(R.id.pentagramaIV);
        notaTV = (TextView)findViewById(R.id.notaTV);

        sPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);


        for(int i = 0; i < flauta.length; i++)
        {
            recurso = flauta[i];

            resID = getResources().getIdentifier(recurso, "drawable", getPackageName());

            flautaIV.setImageResource(resID);

            recurso = pentagrama[i];

            resID = getResources().getIdentifier(recurso, "drawable", getPackageName());

            pentagramaIV.setImageResource(resID);

            notaTV.setText(nota[i]);

            recurso = sonido[i];

            resID = getResources().getIdentifier(recurso, "raw", getPackageName());

            sonidoNota = sPool.load(this, resID, 1);

            sPool.play(sonidoNota, (float)1.0, (float)1.0, 1, 0, 1f);

        }

    }

1 个答案:

答案 0 :(得分:0)

您没有指定,但我假设所有的声音都在同一时间播放,粗略?

SoundPool.play没有阻止,所以它立即环绕你的循环,加载下一个声音,然后播放它。由于您已为SoundPool参数初始化了1 maxStreams,因此我不确定在尝试加载下一个流之前会遇到什么行为已经完成了。在任何情况下,您都需要能够在play调用之后阻止,直到流完成。据我所知,SoundPool没有这种能力。我认为您需要使用MediaPlayer来定义onCompletionListener