开发者

How to play media file

I am using the following code to play audio file. I have tested the audio file on Android phone player & its playing quite loud. When I am trying to play the same audio file from the following code , its very feeble. Is there any problem with my code ? Can I increase the volume of the media file by changing any value ?

While testing , the volume of the Android device has been put to maximum value.

Kindly provide your inputs/sample code. Thanks in advance.

public void playAlertSound() {

        MediaPlayer player = MediaPlayer.create(this, R.raw.beep);
        player.setLooping(false); // Set looping
        player.setVolume(0.90f, 0.90f);
        // Begin pl开发者_如何学编程aying selected media
        player.start();
        // Release media instance to system
        player.release();

    }


Try player.setVolume(1.0f, 1.0f); instead; or just leave off that line entirely. You can also try scaling up the value past 1.0, although that's not really recommended.


You shouldn't call player.release() immediately. Try calling that in your onPause() or onDestroy() methods instead.

You might try using AudioManager.getStreamMaxVolume() to get the maximum volume and use it:

AudioManager audio =
    (AudioManager) Context.getSystemService(Context.AUDIO_SERVICE);
int max = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
player.setVolume(max, max);

I'm not sure though if setVolume() expects absolute levels or multipliers from 0.0f to 1.0f. It mentions logarithmic adjustment, so you might try something closer to 1.0f like 0.95f or 1.0f itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜