• 耳机 插拔


    package com.music.you.tube.receiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    import com.music.you.tube.player.PlayService;
    import com.music.you.tube.util.Actions;
    import com.music.you.tube.util.LogUtil;
    
    /**
     * 来电/耳机拔出时暂停播放
     */
    public class NoisyAudioStreamReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    
            if (intent.hasExtra("state")){
                if (intent.getIntExtra("state", 0) == 0){
                    Intent serviceIntent = new Intent(context, PlayService.class);
                    serviceIntent.setAction(Actions.ACTION_MEDIA_HEADSET);
                    context.startService(serviceIntent);
                    LogUtil.e( "headset not connected");
                }
                else if (intent.getIntExtra("state", 0) == 1){
                    LogUtil.e( "headset connected");
                }
            }
        }
    }
    private IntentFilter mNoisyFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        private NoisyAudioStreamReceiver mNoisyReceiver = new NoisyAudioStreamReceiver();
    
    
            registerReceiver(mNoisyReceiver, mNoisyFilter);
    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;
            }
            unregisterReceiver(mNoisyReceiver);

    //耳机插拔发送的广播的action 可以自己取名字。

  • 相关阅读:
    内置函数
    Day19 列表生成器、迭代器&生成器
    Day18 高阶函数
    Day17 装饰器
    searchBar 隐藏
    iOS UITableView UIScrollView 的支持触摸事件
    手势冲突UIPanGestureRecognizer 和UIPinchGestureRecognizer
    navigationController 之间的切换
    searchDisplayController 时引起的数组越界
    在适配iPhone 6 Plus屏幕的时候,模拟器上两边有很细的白边如何解决
  • 原文地址:https://www.cnblogs.com/qianyukun/p/6735090.html
Copyright © 2020-2023  润新知