Java Android MediaPlayer 音量很低(已调整音量)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8278939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-15 00:35:48  来源:igfitidea点击:

Android MediaPlayer volume is very low ( already adjusted Volume )

javaandroidmedia-player

提问by Sammy

I m using the MediaPlayer to play one of the internal alarm ringtone. i m using the setVolume(1.0f, 1.0f) to maximize the volume when the ringtone is played. but the ringtone doesn't play full volume ( when I compare it to playing the ringtone separately or through the built it android alarm)

我正在使用 MediaPlayer 播放内部闹钟铃声之一。我在播放铃声时使用 setVolume(1.0f, 1.0f) 来最大化音量。但铃声没有播放全音量(当我将它与单独播放铃声或通过内置的 android 闹钟进行比较时)

here is my code

这是我的代码

mediaPlayer.setDataSource(context, ringtoneUri);
mediaPlayer.setLooping(looping);
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.prepare();
mediaPlayer.start();

I added the following permission android.permission.MODIFY_AUDIO_SETTINGS ( not sure if this is needed )

我添加了以下权限 android.permission.MODIFY_AUDIO_SETTINGS (不确定是否需要)

Any Idea why the mediaPlayer still won't play the sound at maximum?

知道为什么 mediaPlayer 仍然不会最大程度地播放声音吗?

采纳答案by Sammy

Here is the solution I found.

这是我找到的解决方案。

AudioManager amanager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = amanager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
amanager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume, 0);

MediaPlayer mediaPlayer= new MediaPlayer();

mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); // this is important.

mediaPlayer.setDataSource(context, ringtoneUri);
mediaPlayer.setLooping(looping);  
mediaPlayer.prepare();
mediaPlayer.start();

回答by shadowmatter

I encountered the same issue, and then noticed this this in the MediaPlayer documentation:

我遇到了同样的问题,然后在MediaPlayer 文档中注意到了这一点:

While in the Prepared state, properties such as audio/sound volume, screenOnWhilePlaying, looping can be adjusted by invoking the corresponding set methods.

在 Prepared 状态下,可以通过调用相应的 set 方法来调整音频/音量、screenOnWhilePlaying、looping 等属性。

Calling setVolumeaftercalling preparefixes this, so that audio is played at max volume. Actually, according to the docs I just quoted, you should call setLoopingafter prepareas well:

呼叫setVolume呼叫prepare修复了这个问题,以便以最大音量播放音频。实际上,根据我刚才引用的文档,你应该叫setLoopingprepare,以及:

mediaPlayer.setDataSource(context, ringtoneUri);
mediaPlayer.prepare();
mediaPlayer.setLooping(looping);
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.start();