美文网首页
Android控制系统音乐

Android控制系统音乐

作者: 当幸福来敲门58 | 来源:发表于2018-01-05 21:07 被阅读75次
    [图片上传失败...(image-7d4742-1515157681534)] 2018-01-05-21-05-58.png

    原理:模拟按键来实现
    命令:
    KeyEvent.KEYCODE_MEDIA_PLAY = 126 打开音乐
    KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE = 85 暂停音乐
    KeyEvent.KEYCODE_MEDIA_NEXT = 87 下一曲
    KeyEvent.KEYCODE_MEDIA_PREVIOUS = 88 上一曲
    步骤:

    1.Android 4.4 模拟键第1种方法,需要在AndroidManifest中添加权限(重要)

    <uses-permission android:name="android.permission.INJECT_EVENTS"
            tools:ignore="ProtectedPermissions" />
    

    2.在代码中实现

    Runtime runtime = Runtime.getRuntime();  
    try {  
        runtime.exec("input keyevent " + KeyEvent.KEYCODE_MEDIA_PLAY);  
        } catch (IOException e) { // TODO Auto-generated catch block  
        e.printStackTrace();  
    } 
    

    3.调节音量

    AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    //加大音量
    mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,
                                AudioManager.FX_FOCUS_NAVIGATION_UP);
    //调小音量
    mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,
                                AudioManager.FX_FOCUS_NAVIGATION_UP);
    

    以下是整体类代码

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        private AudioManager mAudioManager;
    
        @BindView(R.id.start)
        Button start;
        @BindView(R.id.pause)
        Button pause;
        @BindView(R.id.next)
        Button next;
        @BindView(R.id.previous)
        Button previous;
        @BindView(R.id.volume_up)
        Button up;
        @BindView(R.id.volume_down)
        Button down;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            initView();
        }
    
        private void initView(){
            start.setOnClickListener(this);
            pause.setOnClickListener(this);
            next.setOnClickListener(this);
            previous.setOnClickListener(this);
            up.setOnClickListener(this);
            down.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            Runtime runtime = Runtime.getRuntime();
            try {
                switch (view.getId()){
                    case R.id.start:
                        runtime.exec("input keyevent " + KeyEvent.KEYCODE_MEDIA_PLAY);
                        break;
                    case R.id.pause:
                        runtime.exec("input keyevent " + KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
                        break;
                    case R.id.next:
                        runtime.exec("input keyevent " + KeyEvent.KEYCODE_MEDIA_NEXT);
                        break;
                    case R.id.previous:
                        runtime.exec("input keyevent " + KeyEvent.KEYCODE_MEDIA_PREVIOUS);
                        break;
                    case R.id.volume_up:
                        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,
                                AudioManager.FX_FOCUS_NAVIGATION_UP);
                        break;
                    case R.id.volume_down:
                        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,
                                AudioManager.FX_FOCUS_NAVIGATION_UP);
                        break;
                    default:
                }
            } catch (IOException e) { // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Android控制系统音乐

          本文链接:https://www.haomeiwen.com/subject/kmzsnxtx.html