问题
I have made a simple alert beep for my application. But the problem is that the sound is played on some devices and in some others not.
The sample code below:
public void audioPlayer() { // leshon tingull kur ka kolision //Armando 8/7/2013
MediaPlayer beep_alert = MediaPlayer.create(Maps.this,R.raw.double_beep);
try {
beep_alert.start();
} catch (Exception e) {
e.printStackTrace();
}
}
There is no error shown, it just don play for example nexus 4
, but it plays ok in nexus 7
.
What is going wrong?
The double_beep is a mp3 file.
Thanks in advance...
回答1:
Make sure the volume is up, otherwise you won't hear anything.
Generally it's not really advised to turn the volume up programmatically without any user input - if the volume is down it's usually through user action in the first place.
But here's how you can do it:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
This will max out STREAM_MUSIC
. There are many other ways to control/change volume.
Have a read through this as well to familiarise yourself with controlling volumes: http://developer.android.com/training/managing-audio/volume-playback.html
回答2:
Your sound file shoudn't contain Special characters like _
(Underscore) and all. Try it by renaming into doubledeep
. Also it should not starts with Capital Letter
.
来源:https://stackoverflow.com/questions/17620112/android-mediaplayer-not-playing-sound-on-some-devices