Android MediaPlayer not playing sound on some devices

淺唱寂寞╮ 提交于 2019-12-10 16:48:36

问题


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

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