开发者

Playing sound effect (CLICK/NAVIGATION_RIGHT) for button clicks - Android

I'm trying to use the playSoundEffect () method to play a sound effect when a button is clicked, but so far its' proved very difficult for some reason.

I have defined the following.

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

Then,

button_flip.playSoundEffect(android.view.SoundEffectConstants.CLICK);

is called in onCreate() method. But I cannot seem to get it to sound when I click the button.开发者_如何学运维 What am I missing here? The documentation has nothing much to go on.

Do I need to define/call in the onClick() method?

Any help is appreciated.


Try this:

button_flip.setOnClickListener(new View.OnClickListener() {

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

The value 0 stands for CLICK sound. You can see more values here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜