实现方法:
这部分的代码主要是为了控制的实现音乐的播放和暂停,有需要的朋友的可以记录下来,方便以后能用到。
private long eventtime = 0;
private AudioManager vAudioManager = null;
//此处在onCreate方法中初始化
eventtime = SystemClock.uptimeMillis();
vAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//这是播放或者暂停
if (vAudioManager.isMusicActive()){
Toast.makeText(getApplicationContext(), "有音乐在播放---暂停", Toast.LENGTH_SHORT).show();
pauseMusic();//暂停
}else {
Toast.makeText(getApplicationContext(), "无音乐在播放--开始", Toast.LENGTH_SHORT).show();
playMusic();//播放
}
这部分主要是控制上一曲和下一曲
/**
* 暂停
*/
private void pauseMusic() {
if (eventtime<=0)return;
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendOrderedBroadcast(upIntent, null);
}
/**
* 播放
*/
private void playMusic() {
if (eventtime<=0)return;
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendOrderedBroadcast(upIntent, null);
}
/**
* 上一曲
*/
private void lastMusic() {
if (eventtime<=0)return;
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendOrderedBroadcast(upIntent, null);
}
/**
* 下一曲
*/
private void nextMusic() {
if (eventtime<=0)return;
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendOrderedBroadcast(upIntent, null);
最后是控制系统每天的音量大小
// 调低音量
vAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
// 调高音量
vAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
最后
以上就是利用代码控制设备上音乐播放的实现方法,希望对大家的学习或工作有一定的参考价值,如果大家有疑问,欢迎评论留言,谢谢大家的支持。
你可能感兴趣的文章:
Android如何进阶:http://docs.qq.com/doc/DWHFqVHBMVEJPWUx
Android面试题汇总:http://docs.qq.com/doc/DWGZIRFh5VEtYWE1D
Android音视频需要学习哪些:http://docs.qq.com/doc/DWFFWZHNPTHZVdHFX
Android常有的开源框架有哪些框:docs.qq.com/doc/DWHlGYUdseVhsSUda
Android车载应需要学习哪些:docs.qq.com/doc/DWEl0blBabXVvU2Nw Android
Framework怎么学:docs.qq.com/doc/DWFdlc2JocEtNbEJ1