【问题标题】:Clicking button to play sound点击按钮播放声音
【发布时间】:2012-06-16 02:14:03
【问题描述】:

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

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>    

【问题讨论】:

  • 现在有什么问题?你的问题是什么?你的按钮的点击事件在哪里?
  • 为此,您需要捕获按钮单击事件,因为您可以编写代码来播放音频您在这段代码中没有做任何事情那么它将如何播放声音。

标签: java android xml eclipse button


【解决方案1】:

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

private MediaPlayer mp;

在按钮点击事件中添加这个

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

停下来

    mp.stop();

【讨论】:

    【解决方案2】:

    首先,您必须使用 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();
                }
            });
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      相关资源
      最近更新 更多