Android的:如何编程设置MediaPlayer的音量?音量、Android、MediaPlayer

2023-09-07 08:34:51 作者:今天睡姿决定劳资明天发型

如何以编程方式设置媒体播放器的音量。我用它来报警通知。任何帮助是非常AP preciated并在此先感谢。

How to set the mediaplayer volume programmatically. I use it for alarm notification. Any help is highly appreciated and thanks in advance.

推荐答案

使用的 AudioManager 您可以控制​​媒体播放器的音量。

Using AudioManager you can control the volume of media player.

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);

也从MediaPlayer的(但我没有尝试)

also from MediaPlayer (But I didn't try that)

public void  setVolume  (float leftVolume, float rightVolume)

自:API级别1

设定本播放器的音量。此API推荐用于平衡一个应用程序内的音频流的输出。除非你正在编写一个应用程序来控制用户设置,此API应该用于preference到setStreamVolume(INT,INT,INT)中规定的特定类型的所有数据流的音量。需要注意的是通过体积值是原始标量。 UI控件应该对数缩放。

Sets the volume on this player. This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type. Note that the passed volume values are raw scalars. UI controls should be scaled logarithmically.

参数

leftVolume留量标

leftVolume left volume scalar

rightVolume右音量标

rightVolume right volume scalar