gpt4 book ai didi

android - 在Android中顺序播放多种声音

转载 作者:行者123 更新时间:2023-12-03 02:05:29 25 4
gpt4 key购买 nike

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

}

}

最佳答案

您没有指定,但我假设所有声音都在同时播放,大概吗?
SoundPool.play不会阻塞,因此它会立即环绕您的循环,加载下一个声音,然后播放。由于您已经使用SoundPool参数的1初始化了maxStreams,因此我不确定在尝试加载下一个流之前,您将获得什么样的行为。无论如何,您需要能够在play调用之后进行阻塞,直到流完成。据我所知,SoundPool没有该功能。我认为您将需要使用MediaPlayer,它允许您定义onCompletionListener

关于android - 在Android中顺序播放多种声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com