Android 安卓应用中的音量控制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2539264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Volume Control in android application
提问by klaus johan
I'd like to know how to control my application's volume from the volume keys (contrary to my belief , I've read they control only the ringer volume). Should I overwrite the onKey Down/Up?
我想知道如何从音量键控制我的应用程序的音量(与我的信念相反,我读过它们只控制铃声音量)。我应该覆盖 onKey Down/Up 吗?
Or is there other way to accomplish this? I'm asking because if I overwrite the upper mentioned function for an activity, then the functions will receive the event only if a view associated with the this activity has the focus, and I'm looking for something "Globaly" ( to work no matter what activity is running now)
或者有其他方法可以实现这一点吗?我问是因为如果我为一个活动覆盖上面提到的函数,那么只有当与此活动关联的视图具有焦点时,这些函数才会接收事件,并且我正在寻找“全局”的东西(工作没有不管现在正在运行什么活动)
回答by Steve Haley
There was another question from a long time agothat asked the same thing. Essentially the answer is: don't override the onKeyDown and onKeyUp buttons. It's much better to simply use this one line setVolumeControlStream(AudioManager.STREAM_MUSIC);
in your onCreate() method. That tells the OS that the volume buttons should affect the "media" volume when your application is visible, and that's the volume it uses for your application.
很久以前有另一个问题提出了同样的问题。基本上答案是:不要覆盖 onKeyDown 和 onKeyUp 按钮。最好setVolumeControlStream(AudioManager.STREAM_MUSIC);
在 onCreate() 方法中简单地使用这一行。这告诉操作系统,当您的应用程序可见时,音量按钮应该影响“媒体”音量,这就是它用于您的应用程序的音量。
As for controlling the Media volume no matter what app is visible, I'm not sure that can be done - or if it could, whether that would be a good thing.
至于控制媒体音量,无论什么应用程序可见,我不确定这是否可以完成 - 或者如果可以,这是否是一件好事。
回答by Asaf Pinhassi
In your activity you can use one of the following:
在您的活动中,您可以使用以下其中一项:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
this.setVolumeControlStream(AudioManager.STREAM_RING);
this.setVolumeControlStream(AudioManager.STREAM_ALARM);
this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);
this.setVolumeControlStream(AudioManager.STREAM_SYSTEM);
this.setVolumeControlStream(AudioManager.STREAM_VOICECALL);
回答by Rahul Raina
Hope this working android code will help you to develop your own volume control application:
希望这个有效的 android 代码将帮助您开发自己的音量控制应用程序:
public class audioSetting extends Activity {
Button b;
TextView t;
SeekBar s;
EditText e;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.button1);
s = (SeekBar)findViewById(R.id.seekBar1);
e = (EditText)findViewById(R.id.editText1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int a = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
int c = audioManager.getStreamVolume(AudioManager.STREAM_RING);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
audioManager.setStreamVolume(AudioManager.STREAM_RING, (int)(Integer.parseInt(e.getText().toString().trim())), 0);
s.setProgress((int)(Integer.parseInt(e.getText().toString().trim())));
}
});
s.setMax(a);
s.setProgress(c);
e.setText(""+c);
s.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_RING, arg1, 0);
e.setText(""+s.getProgress());
}
});
}
}
回答by Ephraim
This is an old question but there is a superb library for dealing with volume control in android (even better than the volume buttons, which are limited to 5 increments of volume level).
这是一个老问题,但有一个极好的库用于处理 android 中的音量控制(甚至比音量按钮更好,音量按钮限制为 5 级音量增量)。
http://code.google.com/p/media-volume-control
http://code.google.com/p/media-volume-control
Take a look at this package, it provides continuous volume level as well as other nice features (such as when audio is interrupted, volume monitor to be notified if/when the volume level is changed by whatever means--handdy when showing a volume SeekBar, etc.).
看看这个包,它提供了连续的音量级别以及其他不错的功能(例如,当音频中断时,音量监视器会在音量级别以任何方式改变时收到通知——在显示音量时很方便 SeekBar , 等等。)。