问题
I have the following 2 lines of code:
myButton.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
myButton.playSoundEffect(SoundEffectConstants.CLICK);
When I press the button, haptic feedback works fine but no audio is heard.
Anybody had this problem when they started with Android?
回答1:
I had this problem and it turned out to be something quite stupid on my part. I had disabled "Audible Selection" for the entire phone. On the home page, it's on Settings-->Sound-->Audible selection. There is probably a programmatic way to do the same thing but I have not found it yet.
回答2:
I also had the same problem. On my Samsung Galaxy S4, the path is Settings --> My Device --> Sound --> Touch Sounds (under the System heading). Once I enabled this, then the default system sounds started working on my app.
回答3:
Well, I came across the same situation, The trick was, to use Audiomanager.
define an AudioManager in the Activity class.
AudioManager audioManager;
Initialise in onCreate.
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
In the click method, use the audioManager to play the sound.
public void play(View view) {
audioManager.playSoundEffect(SoundEffectConstants.CLICK,1.0f);
}
Note that the volume is given as 1.0f if you don't specify the volume, you won't hear any sound.
回答4:
I know I'm coming to this is a very old thread, but I ran into this problem as well but the answers offered didn't solve my problem. In case anyone else comes across this, here's what I found.
View.playSoundEffect() did not work on my device if it was not called from the UI thread. (I don't see this restriction documented in the Android literature, so it may vary from device to device or from OS version to OS version.) View.performHapticFeedback() does not seem to care. I was calling both from a ThreadPool thread; haptic feedback worked and sound effects didn't.
I fixed this by posting the sound effect call to run on the UI thread like this:
getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{
mSketchView.playSoundEffect(SoundEffectConstants.CLICK);
}
});
来源:https://stackoverflow.com/questions/4362698/cant-play-sound-on-button-click-using-playsoundeffectsoundeffectconstants-clic