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

╄→尐↘猪︶ㄣ 提交于 2019-11-27 06:26:04

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/3905651/playing-sound-effect-click-navigation-right-for-button-clicks-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!