欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android 控制车载蓝牙播放音乐详解流程

程序员文章站 2022-03-25 23:41:20
需求:手机端音乐暂停和播放状态从服务端告诉客户端、设备端实现暂停、播放、上一首、下一首等功能代码路径:packages/apps/bluetooth/src/com/android/bluetooth...

需求:手机端音乐暂停和播放状态从服务端告诉客户端、设备端实现暂停、播放、上一首、下一首等功能
代码路径:

packages/apps/bluetooth/src/com/android/bluetooth/avrcpcontroller/avrcpcontrollerservice.java
packages/apps/bluetooth/src/com/android/bluetooth/avrcpcontroller/avrcpcontrollerstatemachine.java
packages/apps/bluetooth/src/com/android/bluetooth/a2dpsink/a2dpsinkservice.java
packages/apps/bluetooth/src/com/android/bluetooth/avrcpcontroller/avrcpcontrollerservice.java
packages/apps/bluetooth/jni/com_android_bluetooth_avrcp_controller.cpp

一、蓝牙音乐播放状态

1、在avrcpcontrollerservice.java文件中onplaystatuschanged()方法就是音乐播放状态改变,该方法是由jni层中com_android_bluetooth_avrcp_controller.cpp中调用java层的

private synchronized void onplaystatuschanged(byte[] address, byte playstatus) {
    if (dbg) {
        log.d(tag, "onplaystatuschanged " + playstatus);
    }
    int playbackstate = playbackstate.state_none;
    switch (playstatus) {
        case jni_play_status_stopped:
            playbackstate = playbackstate.state_stopped;
            break;
        case jni_play_status_playing:
            playbackstate = playbackstate.state_playing;
            break;
        case jni_play_status_paused:
            playbackstate = playbackstate.state_paused;
            break;
        case jni_play_status_fwd_seek:
            playbackstate = playbackstate.state_fast_forwarding;
            break;
        case jni_play_status_rev_seek:
            playbackstate = playbackstate.state_rewinding;
            break;
        default:
            playbackstate = playbackstate.state_none;
    }
    bluetoothdevice device = bluetoothadapter.getdefaultadapter().getremotedevice(address);
    avrcpcontrollerstatemachine statemachine = getstatemachine(device);
    if (statemachine != null) {
        statemachine.sendmessage(
                avrcpcontrollerstatemachine.message_process_play_status_changed, playbackstate);
    }
}

2、在avrcpcontrollerstatemachine.java文件中connected类中

message_process_play_status_changed消息就是处理播放状态的,然后通过广播方式把该状态发送出去

case message_process_play_status_changed:
    if (systemproperties.get("persist.ivi.feature", "0").equals("1")) {
        a2dpsinkservice a2dpsinkservice = a2dpsinkservice.geta2dpsinkservice();
        log.d(tag, "the state: " + msg.arg1);
        if (a2dpsinkservice != null && msg.arg1 != preaudiostatus) {
            preaudiostatus = msg.arg1;
            log.d(tag, "preaudiostatus: " + msg.arg1);
            if (msg.arg1 == playbackstate.state_playing) {
                //播放
                    a2dpsinkservice.informplaystate(mdevice, true);
            } else if (msg.arg1 == playbackstate.state_paused) {
                //暂停
                    a2dpsinkservice.informplaystate(mdevice, false);
            }
        }
    }
    intent intent = new intent("zqc.bluetooth.play_status_changed");
intent.putextra("zqc.bluetooth.playback", msg.arg1);
mservice.sendbroadcast(intent);
    maddressedplayer.setplaystatus(msg.arg1);
    bluetoothmediabrowserservice.notifychanged(maddressedplayer.getplaybackstate());
    if (maddressedplayer.getplaybackstate().getstate()
            == playbackstate.state_playing
            && a2dpsinkservice.getfocusstate() == audiomanager.audiofocus_none
            && !shouldrequestfocus()) {
        sendmessage(msg_avrcp_passthru,
                avrcpcontrollerservice.pass_thru_cmd_id_pause);
    }
    return true;

二、蓝牙音乐中怎么获取音乐信息

1、在avrcpcontrollerservice.java文件中ontrackchanged()方法中是获取jni层上报的音乐信息,该方法是由jni层中com_android_bluetooth_avrcp_controller.cpp中调用java层的

private synchronized void ontrackchanged(byte[] address, byte numattributes, int[] attributes,
        string[] attribvals) {
    if (dbg) {
        log.d(tag, "ontrackchanged");
    }

    bluetoothdevice device = bluetoothadapter.getdefaultadapter().getremotedevice(address);
    avrcpcontrollerstatemachine statemachine = getstatemachine(device);
    //该方法就是把蓝牙音乐的信息传递给java层
    getelementattrrsp(attributes,attribvals,numattributes);
    if (statemachine != null) {
        statemachine.sendmessage(avrcpcontrollerstatemachine.message_process_track_changed,
                trackinfo.getmetadata(attributes, attribvals));
    }
}

2、在avrcpcontrollerservice.java文件中getelementattrrsp()中,然后通过该方法中通过广播告诉出去

private void getelementattrrsp(int[] attr_id,string[] textarray,byte num_attr){
    string artist = null;
    string tracktitle = null;
    string album = null;
    for (int i = 0; i < num_attr; i++){
        switch (attr_id[i]) {
            case jni_media_attr_id_title:
                tracktitle = textarray[i];
                if (tracktitle == null){
                    tracktitle = "unknown";
                }
                break;
            case jni_media_attr_id_artist:
                artist = textarray[i];
                if (artist == null){
                    artist = "unknown";
                }
                break;
            case jni_media_attr_id_album:
                album = textarray[i];
                if (album == null){
                    album = "unknown";
                }
                break;
        }
    }
    intent intent = new intent("com.android.getelementattrrsp");
    intent.putextra("artist", artist);
    intent.putextra("tracktitle",tracktitle);
    intent.putextra("album",album);
    log.d(tag,"getelementattrrsp,artist: " + artist + ",tracktitle: " + tracktitle + ",album: " + album);
    intent.addflags(intent.flag_receiver_foreground);
    sendbroadcast(intent, profileservice.bluetooth_perm);
}

三、设备蓝牙音乐中上一首、下一首、暂停功能控制手机端

1、在avrcpcontrollerservice.java文件中sendpassthroughcmd()方法来控制暂停、播放、上一首、下一首等功能

public synchronized void sendpassthroughcmd(bluetoothdevice device, int keycode, int keystate) {
    log.v(tag, "sendpassthroughcmd keycode: " + keycode + " keystate: " + keystate);
    if (device == null) {
        log.e(tag, "sendpassthroughcmd device is null");
        return;
    }

    avrcpcontrollerstatemachine statemachine = getstatemachine(device);
    if (statemachine != null) {
        statemachine.sendmessage(avrcpcontrollerstatemachine.message_send_pass_through_cmd,
            keycode, keystate, device);
    }
}

2、在avrcpcontrollerstatemachine.java文件中message_send_pass_through_cmd变量中通过jni层来实现功能

case message_send_pass_through_cmd:
    bluetoothdevice device = (bluetoothdevice) msg.obj;
    mservice.sendpassthroughcommandnative(utils.getbyteaddress(device), msg.arg1,
                    msg.arg2);
    return true;

到此这篇关于android 控制车载蓝牙播放音乐详解流程的文章就介绍到这了,更多相关android 控制车载蓝牙 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!