0
我正在製作音板,當用戶單擊按鈕時,聲音字節應該播放。我非常確定,我的代碼中的邏輯是正確的,因爲我在for循環中搜索了一個錯誤,所以將聲音設置爲每個按鈕都是正確的。出於某種原因,屏幕上總是隻有一個隨機按鈕,點擊時不會播放聲音,每次運行代碼時都不會改變按鈕。我認爲它是我的XML中的一個錯誤,因爲每次我更改XML以嘗試修復它時,不同的按鈕就是不起作用的按鈕。由於佈局錯誤點擊按鈕時,Android聲音不會播放
<?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();
}
});
}
}
你有什麼錯誤或者什麼都沒有發生? – codeMagic
@codeMagic我沒有收到錯誤,但是當我單擊按鈕時聲音不起作用。 –
*「每當我更改XML以嘗試修復它時,不同的按鈕是不起作用的。」*它總是不同的*聲音*也是同一個? – codeMagic