如何把音量到最大编程的机器人? [英] How to turn the volume to max programmatically on android?
问题描述
我写了Android的一个应用程序,打开音量和播放一首歌曲45秒,然后停止。伟大工程,但是我只能得到音量调高至50%,有没有一种方法使用setVolume把音量高达100%()?
这是我的code:
最后MediaPlayer的MP = MediaPlayer.create(背景下,R.raw.sound_file_1);
//扮演老虎的眼睛45秒
如果(messages.contains(MUSIC ONLY)){
//调高音量
mp.setVolume(20,20);
mp.start();
//播放铃声,45秒
新的Timer()。时间表(新的TimerTask(){
@覆盖
公共无效的run(){
mp.stop();
}
},45000);
}
您可以使用下面的代码片段,使用的 AudioManager :
AudioManager上午=
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(
AudioManager.STREAM_MUSIC,
am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
0);
这将音量来的最高水平( getStreamMaxVolume()
)的 STREAM_MUSIC
(这是对的例子歌饰演)。对于其他类型的声音,使用不同的值,例如 STREAM_RING
等。
I am writing an app for android that turns up the volume and plays a song for 45 seconds and then stops. That works great, however I can only get the volume to turn up to 50%, Is there a way to turn the volume up to 100% using setVolume()?
This is my code:
final MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
//plays eye of the tiger for 45 seconds
if (messages.contains("MUSIC ONLY")){
//turn up the volume
mp.setVolume(20, 20);
mp.start();
//play ring tone for 45 seconds
new Timer().schedule(new TimerTask() {
@Override
public void run() {
mp.stop();
}
}, 45000);
}
You can use the following snippet, using AudioManager:
AudioManager am =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(
AudioManager.STREAM_MUSIC,
am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
0);
This sets the volume to the maximum level (getStreamMaxVolume()
) for the STREAM_MUSIC
(which is on example a song played). For other types of sounds, use different value, like STREAM_RING
etc.
这篇关于如何把音量到最大编程的机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!