gpt4 book ai didi

java - 如何在android中循环播放声音

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:04 27 4
gpt4 key购买 nike

当按下按钮时,第一个声音激活。然后再次按下该按钮,它会停止并且第二个声音激活我的密码是好的吗?

package com.Randomsentence;
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Randomsentence extends Activity {
boolean showRandom = false;
TextView txt;
int time = 30;
int random;
public String[] myString;
Button bt1;
boolean check = false;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt=(TextView)findViewById(R.id.txt);
bt1 = (Button)findViewById(R.id.bt1);
Medaiplayer mp = new Medaiplayer();
Mediaplayer mp2 = new Mediaplayer();
bt1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showRandom = !showRandom;
t = new Thread() {
public void run() {
try {
while(showRandom){
mp = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile1);
mp.setLooping(true);
mp.start();
mp2.reset();
mp2.prepare();
sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
mp.reset();
mp.prepare();
mp2 = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile2);
mp2.setLooping(true);
mp2.start();

}catch(Exception ex){
ex.printStackTrace();
}
}
};
t.start();

}

});

}

// our handler
Handler handler = new Handler() {
public void handleMessage(Message msg) {//display each item in a single line
{

Random rgenerator = new Random();
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
String q = myString[rgenerator.nextInt(myString.length)];
txt.setText(q);

}
}
};
}

最佳答案

添加行:

mp.setLooping(true);

然后当你想停止循环时设置false。

关于java - 如何在android中循环播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6481798/

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