个人技术分享

        对于蓝牙音乐的开发来说,播放进度是一个比较重要的数据参数,这里我们就来分析一下蓝牙音乐播放进度的相关回调。

一、回调流程

1、AvrcpControllerService

源码位置:/packages/apps/Bluetooth/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerService.java

// 由JNI基于计时器周期性地调用以更新播放位置
private synchronized void onPlayPositionChanged(byte[] address, int songLen, int currSongPosition) {
    if (DBG) {
        Log.d(TAG, "onPlayPositionChanged pos " + currSongPosition);
    }
    BluetoothDevice device = getAnonymousDevice(address);
    AvrcpControllerStateMachine stateMachine = getStateMachine(device);
    if (stateMachine != null) {
        stateMachine.sendMessage(AvrcpControllerStateMachine.MESSAGE_PROCESS_PLAY_POS_CHANGED,
                        songLen, currSongPosition);
    }
}

        该回调主要接收状态机返回的进度信息,记住这里的相关 Log,对 Bug 分析很有帮助。这里通过 MESSAGE_PROCESS_PLAY_POS_CHANGED 消息将当前进度发送到状态机。

2、AvrcpControl