Android模拟音量按键- 控制系统音频 包括外接音响
问题简述
如何通过app内部的button控制手机的系统音频(包括外接音响)
- 这次的问题是如果用户已经用了某个音乐软件 ( spotify 网易音乐之类的), 我们能不能通过自己app内部的button去控制音频。 像系统自带的widget,可以 控制上一首 /下一首, 播放/暂停 , 以及音量控制。
UI
UI代码
其实没有什么具体的内容,就是为了省事也一会放上吧
代码内容
//初始
protected ConstraintLayout audioPage;
private ImageButton playButton;
private ImageButton pauseButton;
private MediaPlayer mediaPlayer01;
public AudioManager audiomanage;
private TextView mVolume ;
public SeekBar soundBar;
private int maxVolume, currentVolume;
private int volume=0;
private boolean isPlayMusic = false;
//获得音频权限
audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mediaPlayer01 = new MediaPlayer();
playButton = findViewById(R.id.audio_play);
pauseButton = findViewById(R.id.audio_pause);
final SeekBar soundBar=(SeekBar)findViewById(R.id.sound);
mVolume = (TextView)findViewById(R.id.mVolume);
audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //获取系统最大音量
soundBar.setMax(maxVolume); //拖动条最高值与系统最大声匹配
currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前值
soundBar.setProgress(currentVolume);
mVolume.setText(currentVolume*100/maxVolume + " %");
soundBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() //调音监听器
{
public void onProgressChanged(SeekBar arg0,int progress,boolean fromUser)
{
audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前值
soundBar.setProgress(currentVolume);
mVolume.setText(currentVolume*100/maxVolume + " %");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
//确认音频是否在播放 修改中间button
public void playOrPause() {
AudioManager mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
if (mAudioManager.isMusicActive()) {
Log.e(TAG, "isMusicActive");
playButton.setVisibility(View.GONE);
pauseButton.setVisibility(View.VISIBLE);
isPlayMusic = true;
} else {
Log.e(TAG, "Music Not Active");
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.GONE);
isPlayMusic = false;
}
}
//
public void audioPauseOnClick(View view) {
try{
String keyCommand = "input keyevent " + KeyEvent.KEYCODE_MEDIA_PAUSE;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(keyCommand);
} catch(IOException e){
}
final Instrumentation m_Instrumentation = new Instrumentation();
new Thread(new Runnable() {
@Override
public void run() {
m_Instrumentation.sendKeySync(new KeyEvent(KeyEvent.KEYCODE_MEDIA_PAUSE, KeyEvent.KEYCODE_1) );
}
}).start();
new Handler().postDelayed(new Runnable() {
public void run() {
playOrPause();
}
}, 1000);
}
//
public void audioPlayOnClick(View view) {
try{
String keyCommand = "input keyevent " + KeyEvent.KEYCODE_MEDIA_PLAY;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(keyCommand);
} catch(IOException e){
}
final Instrumentation m_Instrumentation = new Instrumentation();
new Thread(new Runnable() {
@Override
public void run() {
m_Instrumentation.sendKeySync(new KeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_1) );
}
}).start();
new Handler().postDelayed(new Runnable() {
public void run() {
//设置了延时,因为有时候会有延迟反应
playOrPause();
}
}, 1000);
}
public void audioPreviousOnClick(View view) {
try{
String keyCommand = "input keyevent " + KeyEvent.KEYCODE_MEDIA_PREVIOUS;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(keyCommand);
} catch(IOException e){
}
final Instrumentation m_Instrumentation = new Instrumentation();
new Thread(new Runnable() {
@Override
public void run() {
m_Instrumentation.sendKeySync(new KeyEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS, KeyEvent.KEYCODE_1) );
}
}).start();
new Handler().postDelayed(new Runnable() {
public void run() {
// Log.e(TAG, "Awning fully closed. " );
playOrPause();
}
}, 1000);
}
public void audioNextOnClick(View view) {
try{
String keyCommand = "input keyevent " + KeyEvent.KEYCODE_MEDIA_NEXT;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(keyCommand);
} catch(IOException e){
}
final Instrumentation m_Instrumentation = new Instrumentation();
new Thread(new Runnable() {
@Override
public void run() {
m_Instrumentation.sendKeySync(new KeyEvent(KeyEvent.KEYCODE_MEDIA_NEXT, KeyEvent.KEYCODE_1) );
}
}).start();
new Handler().postDelayed(new Runnable() {
public void run() {
playOrPause();
}
}, 1000);
}