【问题标题】:Playing Multiple Sounds Sequently in Android在 Android 中连续播放多个声音
【发布时间】:2015-01-26 08:42:06
【问题描述】:

我的问题是我有大约 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);

        }

    }

【问题讨论】:

  • 请出示您的密码

标签: android audio


【解决方案1】:

您没有具体说明,但我假设所有声音大致同时播放?

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

【讨论】:

  • 感谢您的回答,您将如何更改我的代码以使用 MediaPlayer 及其侦听器?
猜你喜欢
  • 2023-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多