zoukankan      html  css  js  c++  java
  • 耳机 线控 控制播放

    package com.music.you.tube.receiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.view.KeyEvent;
    
    import com.music.you.tube.player.PlayService;
    import com.music.you.tube.util.Actions;
    
    /**
     * 耳机线控
     */
    public class RemoteControlReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if (event == null || event.getAction() != KeyEvent.ACTION_UP) {
                return;
            }
            Intent serviceIntent;
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_MEDIA_PLAY:
                case KeyEvent.KEYCODE_MEDIA_PAUSE:
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                case KeyEvent.KEYCODE_HEADSETHOOK:
                    serviceIntent = new Intent(context, PlayService.class);
                    serviceIntent.setAction(Actions.ACTION_MEDIA_PLAY_PAUSE);
                    context.startService(serviceIntent);
                    break;
                case KeyEvent.KEYCODE_MEDIA_NEXT:
                    serviceIntent = new Intent(context, PlayService.class);
                    serviceIntent.setAction(Actions.ACTION_MEDIA_NEXT);
                    context.startService(serviceIntent);
                    break;
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                    serviceIntent = new Intent(context, PlayService.class);
                    serviceIntent.setAction(Actions.ACTION_MEDIA_PREVIOUS);
                    context.startService(serviceIntent);
                    break;
            }
        }
    }
    <receiver android:name="com.music.you.tube.receiver.RemoteControlReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.MEDIA_BUTTON" />
                </intent-filter>
            </receiver>
    package com.music.you.tube.util;
    
    /**
     * Actions
     */
    public class Actions {
        public static final String ACTION_MEDIA_PLAY_PAUSE = "podcast.music.ACTION_MEDIA_PLAY_PAUSE";
        public static final String ACTION_MEDIA_HEADSET = "podcast.music.ACTION_MEDIA_HEADSET";
        public static final String ACTION_MEDIA_NEXT = "podcast.music.ACTION_MEDIA_NEXT";
        public static final String ACTION_MEDIA_PREVIOUS = "podcast.music.ACTION_MEDIA_PREVIOUS";
        public static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";
    }
    //实例    : 注册和接收广播在service中   也可以在activity中
    
    
     private ComponentName mRemoteReceiver;
    
    
    
    
    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            mRemoteReceiver = new ComponentName(getPackageName(), RemoteControlReceiver.class.getName());
            mAudioManager.registerMediaButtonEventReceiver(mRemoteReceiver);
    
    
    
    
    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (intent == null || intent.getAction() == null) {
                return START_NOT_STICKY;
            }
            switch (intent.getAction()) {
                case Actions.ACTION_MEDIA_HEADSET:
                    if (getCurrentState() == YoutubePlayerView.STATE.PLAYING) {
                        pause();
                    }
                    break;
                case Actions.ACTION_MEDIA_PLAY_PAUSE:
                    if (getCurrentState() == YoutubePlayerView.STATE.PLAYING) {
                        pause();
                    } else if (getCurrentState() == YoutubePlayerView.STATE.PAUSED) {
                        play();
                    } else if (getCurrentState() == YoutubePlayerView.STATE.BUFFERING) {
                        stop();
                    } else if (getCurrentState() == YoutubePlayerView.STATE.CUED) {
                        stop();
                    } else if (getCurrentState() == YoutubePlayerView.STATE.ENDED) {
                        pause();
                    } else if (getCurrentState() == YoutubePlayerView.STATE.NONE) {
                        stop();
                    } else if (getCurrentState() == YoutubePlayerView.STATE.UNSTARTED) {
                        stop();
                    }
                    break;
                case Actions.ACTION_MEDIA_NEXT:
                    next();
                    break;
                case Actions.ACTION_MEDIA_PREVIOUS:
                    prev();
                    break;
            }
            return START_NOT_STICKY;
        }
            mAudioManager.unregisterMediaButtonEventReceiver(mRemoteReceiver);
  • 相关阅读:
    使用weave管理docker网络
    为docker配置固定ip
    Building good docker images
    使用curl命令获取文件下载速度
    吐槽Java
    Kubernetes 中的服务发现与负载均衡(转)
    Kubernetes系列之介绍篇(转)
    top命令中的wa指标(转)
    uwsgi常用参数详解(转)
    Unix域套接字-Unix Domain Socket(转)
  • 原文地址:https://www.cnblogs.com/qianyukun/p/6735079.html
Copyright © 2011-2022 走看看