简体   繁体   中英

Android MediaPlayer not playing sound on some devices

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM