Eclipse:Android音板按钮是;“可过度舔”;
在我的android soundboard中,所有按钮都工作正常,声音播放正常,但有一个问题。问题是,当你按下应用程序中的一个按钮时,你可以按下其他按钮。例如,如果我按下的按钮播放类似警报器的噪音,我可以按下另一个按钮,两种声音将同时播放。这不是我想要的。是否有任何代码可以添加以使其一次只能单击一个按钮,或者有一些代码使上一个声音停止并播放新选择的声音Eclipse:Android音板按钮是;“可过度舔”;,android,eclipse,button,click,Android,Eclipse,Button,Click,在我的android soundboard中,所有按钮都工作正常,声音播放正常,但有一个问题。问题是,当你按下应用程序中的一个按钮时,你可以按下其他按钮。例如,如果我按下的按钮播放类似警报器的噪音,我可以按下另一个按钮,两种声音将同时播放。这不是我想要的。是否有任何代码可以添加以使其一次只能单击一个按钮,或者有一些代码使上一个声音停止并播放新选择的声音 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.soundboard"
android:versionCode="1"
android:versionName="1.2">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="false" android:allowClearUserData="true">
<activity android:label="Vegeta Soundboard" android:screenOrientation="portrait" android:name="Soundboard" android:icon="@drawable/icon">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
最简单的方法是,只要有一个全局的声音播放布尔值,当用户单击声音按钮时,该布尔值就会被设置,并且在该声音结束时会被取消设置
public class Main extends Activity{
//global variable, notice how it's not in any method or inner class
//so it's accessible to all the members of this class
private boolean soundPlaying = false;
@Override
public void onCreate(Bundle b){
super.onCreate(b);
}
//Somewhere in here you have your onclick function that you have called from your xml button
public void playSound(View v){
if(soundPlaying)
return;
soundPlaying = true;
//myMedia is your media player object
myMedia.start();//this will only work if you have a media player set up with media
myMedia.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;//here you set it to false cause the sound is done
}
});
}
}
好吧,我遇到了另一个问题。我如何将全局变量合并到android应用程序中,以便它在我的手机上工作?我不确定我是否理解你的意思。当你打电话时,你希望看到什么?假设你在你的应用程序中,你希望收到的电话会有什么影响?我的意思是如何将全局变量放入应用程序中?我是为它创建JavaScript,还是将其添加到main.xml中,还是将其放在我没有列出的位置?只是活动类的顶级布尔成员。好的,我已经添加了manifest.xml。我应该从这里把布尔值放在哪里?上面的代码应该放在哪里(你的方向看起来很混乱)我正试图让同样的事情发生(对不起,我是个傻瓜
public class newBoard extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Toast.makeText(this, "Thank you for using this App.", Toast.LENGTH_LONG).show();
// ads - load request to display
AdView layout = (AdView)this.findViewById(R.id.adView);
// ads - load display with an ad
AdRequest adRequest = new AdRequest();
adRequest.setTesting(true);
layout.loadAd(adRequest);
// import sound files
final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01);
final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound02);
final MediaPlayer sound03 = MediaPlayer.create(this, R.raw.sound03);
final MediaPlayer sound04 = MediaPlayer.create(this, R.raw.sound04);
final MediaPlayer sound05 = MediaPlayer.create(this, R.raw.sound05);
final MediaPlayer sound06 = MediaPlayer.create(this, R.raw.sound06);
final MediaPlayer sound07 = MediaPlayer.create(this, R.raw.sound07);
final MediaPlayer sound08 = MediaPlayer.create(this, R.raw.sound08);
final MediaPlayer sound09 = MediaPlayer.create(this, R.raw.sound09);
final MediaPlayer sound10 = MediaPlayer.create(this, R.raw.sound10);
final MediaPlayer sound11 = MediaPlayer.create(this, R.raw.sound11);
final MediaPlayer sound12 = MediaPlayer.create(this, R.raw.sound12);
final MediaPlayer sound13 = MediaPlayer.create(this, R.raw.sound13);
final MediaPlayer sound14 = MediaPlayer.create(this, R.raw.sound14);
final MediaPlayer sound15 = MediaPlayer.create(this, R.raw.sound15);
final MediaPlayer sound16 = MediaPlayer.create(this, R.raw.sound16);
final MediaPlayer sound17 = MediaPlayer.create(this, R.raw.sound17);
final MediaPlayer sound18 = MediaPlayer.create(this, R.raw.sound18);
final MediaPlayer sound19 = MediaPlayer.create(this, R.raw.sound19);
final MediaPlayer sound20 = MediaPlayer.create(this, R.raw.sound20);
final MediaPlayer sound21 = MediaPlayer.create(this, R.raw.sound21);
final MediaPlayer sound22 = MediaPlayer.create(this, R.raw.sound22);
final MediaPlayer sound23 = MediaPlayer.create(this, R.raw.sound23);
final MediaPlayer sound24 = MediaPlayer.create(this, R.raw.sound24);
final MediaPlayer sound25 = MediaPlayer.create(this, R.raw.sound25);
// play sound files on clicks
Button s01 = (Button) findViewById(R.id.button01);
s01.setText(this.getString(R.string.quote01));
s01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound01.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound01.start();
}
});
registerForContextMenu(s01);
Button s02 = (Button) findViewById(R.id.button02);
s02.setText(this.getString(R.string.quote02));
s02.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound02.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound02.start();
}
});
registerForContextMenu(s02);
Button s03 = (Button) findViewById(R.id.button03);
s03.setText(this.getString(R.string.quote03));
s03.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound03.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound03.start();
}
});
registerForContextMenu(s03);
Button s04 = (Button) findViewById(R.id.button04);
s04.setText(this.getString(R.string.quote04));
s04.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound04.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound04.start();
}
});
registerForContextMenu(s04);
Button s05 = (Button) findViewById(R.id.button05);
s05.setText(this.getString(R.string.quote05));
s05.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound05.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound05.start();
}
});
registerForContextMenu(s05);
Button s06 = (Button) findViewById(R.id.button06);
s06.setText(this.getString(R.string.quote06));
s06.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound06.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound06.start();
}
});
registerForContextMenu(s06);
Button s07 = (Button) findViewById(R.id.button07);
s07.setText(this.getString(R.string.quote07));
s07.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound07.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound07.start();
}
});
registerForContextMenu(s07);
Button s08 = (Button) findViewById(R.id.button08);
s08.setText(this.getString(R.string.quote08));
s08.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound08.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound08.start();
}
});
registerForContextMenu(s08);
Button s09 = (Button) findViewById(R.id.button09);
s09.setText(this.getString(R.string.quote09));
s09.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound09.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound09.start();
}
});
registerForContextMenu(s09);
Button s10 = (Button) findViewById(R.id.button10);
s10.setText(this.getString(R.string.quote10));
s10.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound10.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound10.start();
}
});
registerForContextMenu(s10);