单击按钮播放声音

时间:2012-06-13 06:44:16

标签: java android xml eclipse button

我已经在本网站上查看了几个关于我的问题的回答问题,但是无法让这个问题起作用。我试图让按钮在点击后播放声音。

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>    

2 个答案:

答案 0 :(得分:0)

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

private MediaPlayer mp;

在按钮点击事件

上添加此项
    mp = MediaPlayer.create(this, R.raw.beep);
    mp.start();//to start playing the sound

停止

    mp.stop();

答案 1 :(得分:0)

首先,您必须使用XML中的按钮ID创建按钮的Object 像这样的文件:

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();
            }
        });

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