在 Android 中单击按钮时播放声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11371426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:11:48  来源:igfitidea点击:

Playing a sound when button clicking in Android

androideclipseeclipse-adt

提问by Cerin

I'm new to Android, and I'm trying to do something simple like play a custom MP3 when a button is clicked. This seems to be a fairlycommonquestion, but even though my code follows the examples, and I'm not getting any errors, I don't hear any sound in either the simulator or a real phone.

我是 Android 新手,我正在尝试做一些简单的事情,例如在单击按钮时播放自定义 MP3。这似乎是一个相当普遍的问题,但即使我的代码遵循示例,并且我没有收到任何错误,但我在模拟器或真实手机中都听不到任何声音。

My MainActivity.java:

我的 MainActivity.java:

public class MainActivity extends Activity {

    private static final String TAG = "MyActivity";

    public void MyActivity(Bundle onSavedStateInstance) {
        Log.v(TAG, "Initializing sounds...");

        final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarma_67560);

        Button play_button = (Button)this.findViewById(R.id.play_button);
        play_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.v(TAG, "Playing sound...");
                mp.start();
            }
        });
        Log.v(TAG, "Sounds initialized.");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

And my activity_main.xml:

还有我的activity_main.xml:

<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/play_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play_button" />

</RelativeLayout>

I'm developing using the Eclipse plugin, which doesn't show any errors and seems to run the app correctly in the simulator or real phone, but when I click the play button, nothing happens. I'm not sure if the simulator supports sound (but I'm assuming it does) and I've confirmed sound is unmuted on my phone.

我正在使用 Eclipse 插件进行开发,该插件没有显示任何错误,并且似乎可以在模拟器或真实手机中正确运行应用程序,但是当我单击播放按钮时,没有任何反应。我不确定模拟器是否支持声音(但我假设它支持)并且我已经确认我的手机上的声音没有静音。

What am I doing wrong?

我究竟做错了什么?

Also, I'm not seeing my logging statements shown anywhere, either in Eclipse's console or the LogCat panel. Should I be seeing those printed somewhere?

此外,我没有在 Eclipse 的控制台或 LogCat 面板中的任何地方看到我的日志记录语句。我应该在某处看到那些印刷品吗?

回答by Raghav Sood

This won't work for a very simple reason: MyActivity() is never called.

由于一个非常简单的原因,这不起作用:MyActivity() 从未被调用。

Android will call your Activity's onCreate(), but any other method calls must be done by you. As you never call you MyActivity() method, the button is never given an onClickListener(), and the sound never played. Try using the following code instead:

Android 将调用您的 Activity 的 onCreate(),但任何其他方法调用必须由您完成。由于您从不调用 MyActivity() 方法,因此按钮不会被赋予 onClickListener(),声音也不会播放。请尝试使用以下代码:

public class MainActivity extends Activity {

    private static final String TAG = "MyActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.v(TAG, "Initializing sounds...");

        final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarma_67560);

        Button play_button = (Button)this.findViewById(R.id.play_button);
        play_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.v(TAG, "Playing sound...");
                mp.start();
            }
        });
        Log.v(TAG, "Sounds initialized.");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

回答by gian1200

You should put your code inside onCreate and after setContentView() since your method/constructor public void MyActivity(Bundle onSavedStateInstance) is never called, therefore your code is never executed. Something like this:

你应该把你的代码放在 onCreate 和 setContentView() 之后,因为你的方法/构造函数 public void MyActivity(Bundle onSavedStateInstance) 永远不会被调用,因此你的代码永远不会被执行。像这样的东西:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v(TAG, "Initializing sounds...");
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarma_67560);
    Button play_button = (Button)this.findViewById(R.id.play_button);
    play_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v(TAG, "Playing sound...");
            mp.start();
        }
    });
    Log.v(TAG, "Sounds initialized.");
}