Android 9.0蓝牙音乐上一首、下一首、暂停和播放

代码路径:

packages/apps/Bluetooth/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java

1、在AvrcpControllerStateMachine.java中MESSAGE_PROCESS_PLAY_STATUS_CHANGED和MESSAGE_PROCESS_PLAY_POS_CHANGED就是获取到播放状态

  case MESSAGE_PROCESS_PLAY_POS_CHANGED:
                        if (msg.arg2 != -1) {
                            mAddressedPlayer.setPlayTime(msg.arg2);
                            broadcastPlayBackStateChanged(getCurrentPlayBackState());
                        }
                        break;

                    case MESSAGE_PROCESS_PLAY_STATUS_CHANGED:
                        int status = msg.arg1;
                        mAddressedPlayer.setPlayStatus(status);
                        broadcastPlayBackStateChanged(getCurrentPlayBackState());
                        if (status == PlaybackState.STATE_PLAYING) {
                            a2dpSinkService.informTGStatePlaying(mRemoteDevice.mBTDevice, true);
                        } else if (status == PlaybackState.STATE_PAUSED
                                || status == PlaybackState.STATE_STOPPED) {
                            a2dpSinkService.informTGStatePlaying(mRemoteDevice.mBTDevice, false);
                        }
                        break;

2、在AvrcpControllerStateMachine.java中broadcastPlayBackStateChanged()方法中发送音乐播放状态出去

  public static final String ACTION_TRACK_EVENT =
            "android.bluetooth.avrcp-controller.profile.action.TRACK_EVENT";
 private void broadcastPlayBackStateChanged(PlaybackState state) {
        Intent intent = new Intent(AvrcpControllerService.ACTION_TRACK_EVENT);
        intent.putExtra(AvrcpControllerService.EXTRA_PLAYBACK, state);
        if (DBG) {
            Log.d(TAG, " broadcastPlayBackStateChanged = " + state.toString());
        }
        mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
    }

3、在我们自己的服务中可以监听该广播:android.bluetooth.avrcp-controller.profile.action.TRACK_EVENT,并且做相应的处理

if (Constance.ACTION_TRACK_EVENT.equals(action)) {
					PlaybackState playbackState = intent.getParcelableExtra(Constance.EXTRA_PLAYBACK);
					if (null != playbackState){
						Log.d(TAG, "playbackState == "+playbackState);
						int playState = playbackState.getState();
						if (playState == PlaybackState.STATE_PLAYING) {
							isPlaying = true;
						} else if (playState == PlaybackState.STATE_PAUSED) {
							isPlaying = false;
						}
						Intent stateIntent = new Intent(MusicFragment.ACTION_TRACK_EVENT);
						stateIntent.putExtra(MusicFragment.NAME_ISPLAYING, isPlaying);
						sendBroadcast(stateIntent);
					}
				}

4、收到蓝牙音乐播放的广播后就可以开启动画

private final BroadcastReceiver mReceiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			Log.i(TAG, "receiver action == "+action);
			if (ACTION_TRACK_EVENT.equals(action)) {
				boolean isPlaying = intent.getBooleanExtra(NAME_ISPLAYING, false);
				if (isPlaying) {
					if(!isPlayState){
						playorpause.setImageResource(R.mipmap.iv_music_pause);
						startPropertyAnim();
						isPlayState = true;
					}
				} else {
					if(isPlayState){
						stopRotate();
						playorpause.setImageResource(R.mipmap.iv_music_play);
						isPlayState = false;
					}
				}
			} else if (ACTION_ATTR_INFO.equals(action)) {
				String artist = intent.getStringExtra("artist");
				String trackTitle = intent.getStringExtra("trackTitle");
				String album = intent.getStringExtra("album");
				tvSong.setText(trackTitle);
				tvSinger.setText(artist);
				Log.i(TAG, "artist == "+artist+" trackTitle == "+trackTitle+" album == "+album);
			}
		}
	};

4、播放、暂停、上一首、下一首蓝牙音乐。
播放:BluetoothAvrcp.PASSTHROUGH_ID_PLAY
暂停:BluetoothAvrcp.PASSTHROUGH_ID_PAUSE
下一首:BluetoothAvrcp.PASSTHROUGH_ID_BACKWARD
上一首:BluetoothAvrcp.PASSTHROUGH_ID_FORWARD

private void sendPlayPauseCmd() {
		if (BluetoothAdapter.getDefaultAdapter().isEnabled()) {
			if(isPlayState){
				bluetoothManager.sendGroupNavigationCmd(BluetoothAvrcp.PASSTHROUGH_ID_PAUSE);
	            playorpause.setImageResource(R.mipmap.iv_music_play);
				stopRotate();
	            isPlayState = false;
			}else{
				bluetoothManager.sendGroupNavigationCmd(BluetoothAvrcp.PASSTHROUGH_ID_PLAY);
	            playorpause.setImageResource(R.mipmap.iv_music_pause);
				startPropertyAnim();
	            isPlayState = true;
			}
		} else {
			ToastUtil.showToast(getActivity(), R.string.bt_disable);
		}
	}
	
		public void sendGroupNavigationCmd(int keyCode) {
		if (!mAdapter.isEnabled()) {
			ToastUtil.showToast(mContext, mContext.getResources().getString(R.string.bt_disable));
			return;
		}
		if (mAvrcpCt == null) {
			Log.d(TAG, "sendGroupNavigationCmd, proxy is not connected");
			return;
		}
		List<BluetoothDevice> connectDevices = mA2dpSink.getConnectedDevices();
		BluetoothDevice mDevice = null;
		if (!connectDevices.isEmpty()) {
			mDevice = connectDevices.remove(0);
		} else {
			Log.d(TAG, "sendPlayPauseCmd, mDevice is null");
			return;
		}
		Log.i(TAG, "sendGroupNavigationCmd ,keyCode == "+keyCode);
		mAvrcpCt.sendPassThroughCmd(mDevice,keyCode,BluetoothAvrcp.PASSTHROUGH_STATE_PRESS);
		mAvrcpCt.sendPassThroughCmd(mDevice,keyCode,BluetoothAvrcp.PASSTHROUGH_STATE_RELEASE);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值