用BlueZ A2DP Profile播放音乐

Bluetooth支持两种Profiles来播放音乐,分别是HSP和A2DP。HSP(handset profile)是单声道的,A2DP是双声道的。本文主要介绍如何在Linux下用A2DP来播放音乐。
A2DP
准备
1. 一个蓝牙耳机和一台Linux机器,Linux上默认会安装BlueZ和PulseAudio。
2. 下载一段时间较长的wav格式的音频文件,测试时会用到。
3. 确保你的蓝牙耳机和PC已经配对成功。如果没有的话,可以通过bluetooth-applet界面查找耳机并配对。
配置
1. 在/etc/pulse/default.pa,注释掉以下两行
bluetooth-discover
module-suspend-on-idle
2.  播放一段较长的wav格式音乐。
paplay 75.wav
3. 用pacmd查看该input的index,在下面的例子中index是2。
xxx@xxx:~/$ pacmd
>>> list-sink-inputs
1 sink input(s) available.
    index: 2
    driver:
    flags:
    state: RUNNING
    sink: 1
    volume: 0: 100% 1: 100%
            0: 0.00 dB 1: 0.00 dB
            balance 0.00
    muted: no
    current latency: 1997.28 ms
    requested latency: 39.51 ms
    sample spec: aLaw 2ch 44100Hz
    channel map: front-left,front-right
                 Stereo
    resample method: copy
    module: 7
    client: 6
    properties:
        media.format = "WAV (Microsoft)"
        application.name = "paplay"
        media.name = "75.wav"
        native-protocol.peer = "UNIX socket client"
        native-protocol.version = "16"
        application.process.id = "6509"
        application.process.user = "xxx"
        application.process.host = "xxx-host"
        application.process.binary = "pacat"
        application.language = "en_US.utf8"
        window.x11.display = ":0.0"
        application.process.machine_id = "aed85c03e5ef20ad3d980db14b34d474"
        application.process.session_id = "aed85c03e5ef20ad3d980db14b34d474-1283675281.437759-895707289"
        module-stream-restore.id = "sink-input-by-application-name:paplay"
4. 用test-audio脚本连接耳机。在下面的例子中,耳机的地址(bdaddr)是00:16:44:21:8D:91。
cd bluez/test;
~bluez/test$ ./test-audio connect 00:16:44:21:8D:91
5. 在pulseaudio console下load module-bluetooth-device模块
>>> load-module module-bluetooth-device profile="a2dp" address="00:16:44:21:8D:91"
在上面的例子中,我们用了A2DP profile加载bluetooth模块,要使用hsp的话,替换参数成profile=”hsp”即可。
6. 在pulseaudio console下list-sink,能够显示出bluetooth的device来。在下面的例子中,index是1。
>>>list-sinks
    index: 1
    name:
    driver:
    flags: HARDWARE DECIBEL_VOLUME LATENCY
    state: RUNNING
    suspend cause:
    priority: 9030
    volume: 0:   7% 1:   7%
            0: -70.57 dB 1: -70.57 dB
            balance 0.00
    base volume: 100%
                 0.00 dB
    volume steps: 65537
    muted: no
    current latency: 44.27 ms
    max request: 2 KiB
    max rewind: 0 KiB
    monitor source: 2
    sample spec: s16le 2ch 44100Hz
    channel map: front-left,front-right
                 Stereo
    used by: 1
    linked by: 1
    fixed latency: 39.51 ms
    card: 1
    module: 16
    properties:
        bluetooth.protocol = "a2dp"
        device.description = "DELL BH200"
        device.string = "00:16:44:21:8D:91"
        device.api = "bluez"
        device.class = "sound"
        device.bus = "bluetooth"
        device.form_factor = "headset"
        bluez.path = "/org/bluez/5373/hci0/dev_00_16_44_21_8D_91"
        bluez.class = "0x640404"
        bluez.name = "DELL BH200"
        device.icon_name = "audio-headset-bluetooth"
7. 在pulseaudio console下,用move-sink-input 即可将声音导到蓝牙耳机上。
>>> move-sink-input 2 1
好了,你应该能从蓝牙耳机上听到音乐了。;-)

你可能感兴趣的:(蓝牙,休闲,bluez,a2dp,立体声音乐)