Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何控制mediaplayer音量_Android - Fatal编程技术网

Android 如何控制mediaplayer音量

Android 如何控制mediaplayer音量,android,Android,我创建了一个简单的应用程序。 在我的主要活动类中,我使用它在单击按钮时播放声音 MediaPlayer buttonSound = MediaPlayer.create(this, R.drawable.button_sound); public void onClick(View arg0){ buttonSound.start(); } 在我的主要活动课上,我有一个服务,可以启动背景音乐 Int

我创建了一个简单的应用程序。 在我的主要活动类中,我使用它在单击按钮时播放声音

MediaPlayer buttonSound = MediaPlayer.create(this, R.drawable.button_sound); 

public void onClick(View arg0){
                buttonSound.start();
                             }
在我的主要活动课上,我有一个服务,可以启动背景音乐

 Intent svc=new Intent(this, BackgroundSoundService.class);
 startService(svc);
问题是我不能用手机的音量按钮来处理它们(我的背景音乐和按钮声音)。 如果我减少手机的音量,我的应用程序音量不会改变!
我该怎么办?

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

// Get the AudioManager
AudioManager audioManager =(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
// Set the volume of played media to your choice.
audioManager.setStreamVolume (AudioManager.STREAM_MUSIC,10,0); 
也可以对媒体播放器使用设置音量(float leftVolume,float righvolume),如下所示

buttonSound.setVolume(10,10);
其中
参数
左卷-->左卷标量
右卷-->右卷标量