-2

我已经浏览了这个网站上关于我的问题的几个已回答的问题,但无法让它发挥作用。我正在尝试让按钮在单击后播放声音。

Java代码:

package jg.AvengersSoundboard;

import android.app.Activity;
import android.os.Bundle;

public class Activity2 extends Activity {
    private Object mp;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    }
}

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"  >



<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Brain bag full of cats (HULK)"/>



<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Do you think this madness will end in your rule? (THOR)" />



<Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Freedom (LOKI)" />



<Button
    android:id="@+id/button4"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Genius, Playboy, Billionaire. (STARK)" />




<Button
    android:id="@+id/button5"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="I have an army, we have a Hulk.  (STARK)" />



<Button
    android:id="@+id/button6"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="I put a bullet in my mouth. (HULK)" />



<Button
    android:id="@+id/button7"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="I&apos;m bringing the party to you (STARK)" />



<Button
    android:id="@+id/button8"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Last time in Germany (Cpt. America)" />



<Button
    android:id="@+id/button9"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Move away please" />



<Button
    android:id="@+id/button10"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Submarine (HULK)" />



<Button
    android:id="@+id/button11"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Tell me nobody kissed me (STARK)" />



<Button
    android:id="@+id/button12"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="You mean peace" />


   <Button
       android:id="@+id/Button02"
       android:layout_width="match_parent"
       android:layout_height="55px"
       android:text="Previous"
       android:textSize="18px" >

</Button>    

4

2 回答 2

0

首先,您必须使用 XML 文件中的按钮 ID 创建按钮的对象,如下所示:

Button one = (Button)this.findViewById(R.id.button1);
Button two = (Button)this.findViewById(R.id.button2);
Button zero = (Button)this.findViewById(R.id.button3);

……其他人也一样……

现在创建一个media player像这样的实例变量:

MediaPlayer mp;

现在在你的OnCreate方法上写这个:

mp = MediaPlayer.create(this, R.raw.mamacita_zero/*Your Sound file in raw folder*/);

现在为您的按钮设置一个 onClickListener :

zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.start();
            }
        });

在每个按钮上,您都可以使用相同的方法来播放声音。

于 2012-06-13T07:01:58.883 回答
0

您需要将声音文件存储在 res/raw/beep.mp3

private MediaPlayer mp;

将此添加到按钮单击事件中

    mp = MediaPlayer.create(this, R.raw.beep);
    mp.start();//to start playing the sound

阻止那个

    mp.stop();
于 2012-06-13T06:58:15.077 回答