简体   繁体   English

在Android上使用MediaPlayer的音量问题

[英]Issues with volume using MediaPlayer on Android

I'm having problems playing sounds with MediaPlayer. 我在用MediaPlayer播放声音时遇到问题。 If the sound starts when the volume of the device is 0, then it won't play. 如果设备音量为0时声音开始,则不会播放。 I'll explain: if in the moment of start playing a sound, the device has volume = 0, and afterwards I increase the volume, it still doesn't sound. 我将说明:如果在开始播放声音时,设备的音量= 0,然后我增加了音量,它仍然没有声音。 However, if it has volume != 0 when the sound starts, I can lower it to mute, and then increase it again and it's still playing. 但是,如果声音开始时它的音量!= 0,我可以将其降低至静音,然后再次增大使其仍在播放。

Here is the code: 这是代码:

mpMusic = MediaPlayer.create(context, R.raw.musicmenu_overcast);
float streamVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mpMusic.setVolume(streamVolume, streamVolume);
mpMusic.start();
mpMusic.setLooping(true);

What am I doing wrong? 我究竟做错了什么? Thank you all! 谢谢你们!

Note that here, there are 2 kinds of sound level : 请注意,这里有两种声音级别:

  • The STREAM volume 流体积
  • The PLAYER volume PLAYER音量

If you set the PLAYER volume to "0", whatever the value of the STREAM volume is, you will never hear anything... If you set the PLAYER volume to "10" for example, you will hear your music with a level that is 10% of the STREAM volume. 如果将PLAYER音量设置为“ 0”,无论STREAM音量值是多少,您都将听不到任何声音。例如,如果将PLAYER音量设置为“ 10”,您将听到的音乐音量为是STREAM量的10%。

In your case, you are setting the volume player to "0" when the STREAM volume is 0... that is why you don't hear anything. 在您的情况下,您要在STREAM音量为0时将音量播放器设置为“ 0”,这就是为什么您听不到任何声音的原因。

Are you sure that you need to adjust the volume of the PLAYER ? 您确定需要调整播放器的音量吗?

mpMusic.setLooping(true);

您正在循环播放声音,这就是为什么声音不断播放的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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