我创建了一个简单的应用程序。 在我的主要 Activity 类中,我使用它在单击按钮时播放声音
MediaPlayer buttonSound = MediaPlayer.create(this, R.drawable.button_sound);
public void onClick(View arg0){
buttonSound.start();
}
在我的主要 Activity 类(class)中,我有一个启动背景音乐的服务
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);
也可以为您的媒体播放器使用setVolume (float leftVolume, float rightVolume)
buttonSound.setVolume(10,10);
在哪里
参数
leftVolume--> 左音量标量
rightVolume-->右体积标量
关于android - 如何控制媒体播放器的音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14065997/