本文介绍了使用搜索栏以控制在android的音量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能准确地改变我的应用程序中使用搜索栏没有控制我的Android设备上的音量的音量按钮音量?我有我的机器人,这就是为什么我要使用一个搜索栏来控制音量的音量键单独的功能。谁能帮帮我吗?
How can I accurately change the volume of my app using a seekbar without controlling the volume by the volume buttons on my android device? I have seperate function on the Volume keys on my android that's why I want to use a seekbar to control the volume. Can Anyone please help me?
推荐答案
请看看下面code。它解决了你的问题。
Please look at below code . It solves your problem.
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class TestExample extends Activity
{
/** Called when the activity is first created. */
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
这篇关于使用搜索栏以控制在android的音量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!