在 Android 上为按钮点击播放声音效果(CLICK/NAVIGATION_RIGHT)。

4

我正在尝试使用playSoundEffect()方法在按钮被点击时播放声音效果,但是目前由于某些原因它非常困难。

我定义了以下内容。

<ImageButton android:id="@+id/Button_flip" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="FLIP!"
android:src="@drawable/flip" android:soundEffectsEnabled="true">
</ImageButton>

然后,
button_flip.playSoundEffect(android.view.SoundEffectConstants.CLICK);

在onCreate()方法中调用。但是当我点击按钮时似乎无法发出声音。我错过了什么?文档没有太多信息可供参考。

我是否需要在onClick()方法中定义/调用?

非常感谢任何帮助。

1个回答

6

试试这个:

button_flip.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            button_flip.playSoundEffect(0);
        }
    });

数值0代表“点击”声音。您可以在此处查看更多数值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接