我正在制作音板,当用户点击按钮时,应播放声音字节。我非常确定我的代码中的逻辑是正确的,因为我在for循环中搜索了一个错误,因此将声音设置为每个按钮。出于某种原因,屏幕上始终只有一个随机按钮在单击时没有播放其声音,并且每次运行代码时,不起作用的按钮都会发生变化。我认为这是我的XML中的错误,因为每次我更改XML以尝试修复它时,不同的按钮就是不起作用的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ben.soundboard.MainActivity"
>
<ImageButton
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_alignStart="@+id/button4" />
<ImageButton
android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/button3"
android:layout_alignStart="@+id/button5" />
<ImageButton
android:id="@+id/button3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignStart="@+id/button6"
android:layout_marginTop="93dp" />
<ImageButton
android:id="@+id/button4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/button5"
android:layout_alignStart="@+id/button7" />
<ImageButton
android:id="@+id/button5"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/button6"
android:layout_alignStart="@+id/button8" />
<ImageButton
android:id="@+id/button6"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/button9" />
<ImageButton
android:id="@+id/button7"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/button8"
android:layout_marginStart="38dp" />
<ImageButton
android:id="@+id/button8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/button9"
android:layout_centerHorizontal="true" />
<ImageButton
android:id="@+id/button9"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginEnd="49dp"
android:layout_marginBottom="106dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer ohBaby = MediaPlayer.create(this,R.raw.oh_baby);
MediaPlayer fourTwentySount = MediaPlayer.create(this,R.raw.four_twenty);
MediaPlayer anotherOne = MediaPlayer.create(this,R.raw.another_one);
MediaPlayer terrorist_win = MediaPlayer.create(this,R.raw.terrorists_win);
MediaPlayer allahu_akbar = MediaPlayer.create(this,R.raw.allahu_akbar);
MediaPlayer cough = MediaPlayer.create(this,R.raw.cough);
MediaPlayer that_was_easy = MediaPlayer.create(this,R.raw.that_was_easy);
MediaPlayer horn = MediaPlayer.create(this,R.raw.horn);
MediaPlayer ethan_bradberry = MediaPlayer.create(this,R.raw.im_ethan_bradberry);
MediaPlayer[] sounds = {ohBaby,fourTwentySount,anotherOne,terrorist_win,allahu_akbar,cough,that_was_easy,horn,ethan_bradberry};
ImageButton button1 = (ImageButton) findViewById(R.id.button1);
ImageButton button2 = (ImageButton) findViewById(R.id.button2);
ImageButton button3 = (ImageButton) findViewById(R.id.button3);
ImageButton button4 = (ImageButton) findViewById(R.id.button4);
ImageButton button5 = (ImageButton) findViewById(R.id.button5);
ImageButton button6 = (ImageButton) findViewById(R.id.button6);
ImageButton button7 = (ImageButton) findViewById(R.id.button7);
ImageButton button8 = (ImageButton) findViewById(R.id.button8);
ImageButton button9 = (ImageButton) findViewById(R.id.button9);
ImageButton[] buttons = {button1,button2,button3,button4,button5,button6,button7,button8,button9};
for (int i = 0; i < 9;i++) {
buttons[i].setImageResource(R.drawable.ic_action_name);
setButtonSound(buttons[i],sounds[i]);
}
}
public void setButtonSound(ImageButton btn, final MediaPlayer sound)
{
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sound.start();
}
});
}
}
答案 0 :(得分:0)
您没有常见的实现,但在开始新声音之前,请停止在任何MediaPlayer中进行复制:
for (int i = 0; i < 9;i++) {
sounds[i].stop();
}
然后在start()之前使用方法prepare():
sound.prepare();
sound.start();
我认为您的问题是您正在同时创建多个MediaPlayers但是您必须在开始重现新的&#34;声音之前停止播放#34;与另一个MediaPlayer。