gpt4 book ai didi

java - 循环播放音频Android Studio

转载 作者:行者123 更新时间:2023-12-03 01:37:49 25 4
gpt4 key购买 nike

有人可以帮助我重复播放音频吗?
因此一键播放音频

这是无重复播放音频的代码

private void panggilAntrian(){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.nomor);
panggil.start();
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor("158");
}
});
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.silahkan);
panggil.start();
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.loket);
panggil.start();
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor("158");
}
});
}
});
}
});
}

在代码中,有一个功能可以重复每个数字播放音频。
重复播放基于数字的音频的功能是这个
private void cekNomor(String kata){
final String[] noPanggil = kata.split("(?<=\\G.{1})");
for(i=0; i<noPanggil.length; i++){
switch (noPanggil[i]){
case "0":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.nol);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.nol);
}
});
}
break;
case "1":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.satu);
Log.e("SUARA", noPanggil[i]);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.satu);
}
});
}
break;
case "2":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.dua);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.dua);
}
});
}
break;
case "3":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.tiga);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.tiga);
}
});
}
break;
case "4":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.empat);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.empat);
}
});
}
break;
case "5":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.lima);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.lima);
Log.e("SUARA", noPanggil[i]);
}
});
}
break;
case "6":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.enam);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.enam);
}
});
}
break;
case "7":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.tujuh);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.tujuh);
}
});
}
break;
case "8":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.delapan);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.delapan);
Log.e("SUARA", noPanggil[i]);
}
});
}
break;
case "9":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.sembilan);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.sembilan);
}
});
}
break;
}
panggil.start();
}
}

输出用于检查第一个数字()不播放任何内容,而用于检查第二个数字()仅播放第一个索引,即“1”。
而两个checksNumber()都应依次播放三个音频。
谁能帮我?

最佳答案

您无法使用for循环执行此操作,因为这是同步编程,并且循环不会等待声音完成再循环播放其他歌曲。

解决此问题的方法之一是制作递归函数,以便您在onCompletion上再次调用它,只是以不同的数字开头:

旁注:请注意,我并未测试此代码,并且不确定它是否会起作用,我只是想朝着正确的方向前进。

private void cekNomor(String kata, i){
final String[] noPanggil = kata.split("(?<=\\G.{1})");
switch (noPanggil[i]){
case "0":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.nol);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);
}
});
}
break;
case "1":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.satu);
Log.e("SUARA", noPanggil[i]);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);
}
});
}
break;
case "2":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.dua);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);
}
});
}
break;
case "3":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.tiga);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);
}
});
}
break;
case "4":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.empat);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);
}
});
}
break;
case "5":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.lima);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);
}
});
}
break;
case "6":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.enam);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);;
}
});
}
break;
case "7":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.tujuh);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);
}
});
}
break;
case "8":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.delapan);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);
}
});
}
break;
case "9":
if(i==0){
panggil = MediaPlayer.create(AntrianLoket.this, R.raw.sembilan);
}else{
panggil.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cekNomor(String kata, ++i);
}
});
}
break;
}
panggil.start();
}

关于java - 循环播放音频Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50711377/

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