如果我想在用户点击按钮时播放声音,则为:
Button one = (Button) this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);
one.setOnClickListener(new OnClickListener(){
public void onClick(View v){
mp.start(); }
});
但是当我有2个声音并且我想在每次点击时播放不同的声音时我该怎么办?
答案 0 :(得分:3)
int iClicked = 0;
public void onClick(View v)
{
iClicked++;
if(iClicked % 2==0){
// Do sth, e.g. play sound I
}
else { // Do sth else, e.g. play sound II
}
}
答案 1 :(得分:0)
试试这个
private int rId=0;//class member
//onclick
final int [] music={R.raw.soho,R.raw.other};
int id=music.length%2;
final MediaPlayer mp = MediaPlayer.create(this, rId);
id++;
答案 2 :(得分:0)
我想在每次点击时播放不同的声音?
为此:
1。将所有原始文件ID存储在整数数组中。似乎:
int arrRawFiles ={R.raw.soho,R.raw.soho2,R.raw.soho3}
2. 在Button的onClick
上根据数组长度生成一个随机整数,然后使用该数字从Array获取原始文件ID:
public void onClick(View v)
{
int index = new Random().nextInt(arrRawFiles.length);
MediaPlayer mp = MediaPlayer.create(this,index);
mp.start();
}
答案 3 :(得分:0)
MediaPlayer mp;
int clickCount=0;
one.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
if(clickCount %2 ==0){
mp = MediaPlayer.create(this, R.raw.soho);
}
else{
mp = MediaPlayer.create(this, R.raw.another);
}
mp.start();
clickCount++;
} });
答案 4 :(得分:0)
这很简单。查看示例代码并尝试...
final boolean isFirstSound = true;
Button one = (Button) this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.second_sound);
one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
isFirstSound = !isFirstSound;
if(isFirstSound)
mp.start();
else
mp2.start();
}
});
答案 5 :(得分:0)
您可以将音频片段的引用放入数组
int[] audio={R.raw.clip1,R.raw.clip2};
for(i=0;i<audio.length;i++){
//your MediaPlayer Code here
}
我没试过这个,但你会明白如何播放很多片段。 祝你好运
答案 6 :(得分:0)
public class ButtonPlayer implements View.OnClickListener {
private final int[] sounds;
private int soundIndex = -1;
public ButtonPlayer(int... sounds) {
this.sounds = sounds;
}
@Override
public void onClick(View view) {
if (sounds.length != 0) {
soundIndex++;
soundIndex = soundIndex % sounds.length;
MediaPlayer mp = MediaPlayer.create(this, sounds[soundIndex]);
mp.start();
}
}
}
然后在代码的某处:
Button one = (Button) this.findViewById(R.id.button1);
one.setOnClickListener(new ButtonPlayer(
R.raw.first_composition,
R.raw.second_composition));
您可以稍后通过为构造函数中的每个声音生成'n'个媒体播放器来优化此类。但这是优化的任务,而不是实施。
当然,总是尝试为小功能创建独立的类。不要搞乱活动。
答案 7 :(得分:0)
int count=0;
Button one = (Button) this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);
one.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
count++;
if(count%2==1) {
//play first sound
}else{
//play second sound
}
} });
答案 8 :(得分:0)
尝试类似
的内容boolean playFirst = true;
final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.second_sound_soho);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(playFirst == true){
mp.start();
playFirst = false;
}else{
mp2.start();
playFirst = true;
}
}
});
答案 9 :(得分:0)
如果您只想在2个声音之间切换,我认为您第一次检查时不需要布尔值或int值 只需检查sound1是否正在播放 - &gt;停止sound1并打开sound2并反对
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.soho);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.second_sound);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(mp1.isPlaying()){
mp1.stop();
mp2.start();
}else{
mp2.stop();
mp1.start();
}
}
});
希望这个帮助