使用 bluealsa 将音频发送到蓝牙扬声器

使用 bluealsa 将音频发送到蓝牙扬声器

我的 NAS 是基于 Cubietruck(Ubuntu Xenial 和 Armbian Linux 3.4.113-sun7i)并集成蓝牙的。现在我正尝试通过 bluealsa 将音频发送到我的蓝牙扬声器。

最初我使用本手册来安装和设置 bluealsa:https://forum.armbian.com/topic/6480-bluealsa-bluetooth-audio-using-alsa-not-pulseaudio/

使用本手册重新编译并再次设置 bluealsa 后,我遇到了与现在相同的问题:https://raspberrypi.stackexchange.com/questions/90267/how-to-stream-sound-to-a-bluetooth-device-from-a-raspberry-pi-zero

这让我取得了更多的进展,例如,我在连接到蓝牙扬声器时可以看到这样的消息:[BH129]# bluealsa: D: ../../src/bluez.c:1267: Signal: org.freedesktop.DBus.Objec tManager.InterfacesAdded()

当我按照第一本手册安装并设置 bluealsa 时,我没有看到这样的消息。

我首先在启动后手动启动 bluealsa。顺便问一下,蓝牙配置文件是否正确:- hsp-hs 耳机 (v1.2)?

 bluealsa -p hsp-hs &

Bluealsa 似乎正在运行:

root@Artisan:~# ps -ef | grep bluealsa
root      2591  2557  0 15:16 pts/0    00:00:00 bluealsa -p hsp-hs
root      2803  2557  1 15:58 pts/0    00:00:00 grep --color=auto bluealsa

以下是我连接扬声器的方法:

[bluetooth]# scan on
Discovery started
[CHG] Controller 43:29:B1:55:01:01 Discovering: yes

[NEW] Device EB:06:EF:47:83:A6 BH129
[bluetooth]# pair EB:06:EF:47:83:A6
Attempting to pair with EB:06:EF:47:83:A6
[CHG] Device EB:06:EF:47:83:A6 Class: 0x240404
[CHG] Device EB:06:EF:47:83:A6 Icon: audio-card
[CHG] Device EB:06:EF:47:83:A6 Connected: yes
[CHG] Device EB:06:EF:47:83:A6 UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device EB:06:EF:47:83:A6 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device EB:06:EF:47:83:A6 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device EB:06:EF:47:83:A6 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device EB:06:EF:47:83:A6 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device EB:06:EF:47:83:A6 UUIDs: 0000180f-0000-1000-8000-00805f9b34fb
[CHG] Device EB:06:EF:47:83:A6 Paired: yes
Pairing successful
[CHG] Device EB:06:EF:47:83:A6 Connected: no
[bluetooth]# trust EB:06:EF:47:83:A6
[CHG] Device EB:06:EF:47:83:A6 Trusted: yes
Changing EB:06:EF:47:83:A6 trust succeeded
[bluetooth]# connect EB:06:EF:47:83:A6
Attempting to connect to EB:06:EF:47:83:A6
[CHG] Device EB:06:EF:47:83:A6 Connected: yes
[BH129]# bluealsa: D: ../../src/bluez.c:1267: Signal: org.freedesktop.DBus.ObjectManager.InterfacesAdded()
Connection successful
[BH129]# exit
[DEL] Controller 43:29:B1:55:01:01 BlueZ 5.37 [default]

这是蓝牙状态

root@Artisan:~# systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-12-18 15:13:37 UTC; 36min ago
     Docs: man:bluetoothd(8)
 Main PID: 1307 (bluetoothd)
   Status: "Running"
   CGroup: /system.slice/bluetooth.service
           └─1307 /usr/lib/bluetooth/bluetoothd --noplugin=sap --plugin=a2dp

Dec 18 15:13:37 Artisan bluetoothd[1307]: Ignoring (cli) thermometer
Dec 18 15:13:37 Artisan bluetoothd[1307]: Ignoring (cli) heartrate
Dec 18 15:13:37 Artisan bluetoothd[1307]: Ignoring (cli) cyclingspeed
Dec 18 15:13:37 Artisan bluetoothd[1307]: Ignoring (cli) sixaxis
Dec 18 15:13:37 Artisan bluetoothd[1307]: Bluetooth management interface 1.0 initialized
Dec 18 15:13:37 Artisan bluetoothd[1307]: Failed to obtain handles for "Service Changed" characterist
Dec 18 15:13:37 Artisan systemd[1]: Started Bluetooth service.
Dec 18 15:14:36 Artisan bluetoothd[1307]: Endpoint registered: sender=:1.52 path=/MediaEndpoint/A2DPS
Dec 18 15:14:36 Artisan bluetoothd[1307]: Endpoint registered: sender=:1.52 path=/MediaEndpoint/A2DPS
Dec 18 15:14:40 Artisan bluetoothd[1307]: Ignoring unsupported UUID for addition

我对“Alsa 配置”部分有点困惑,最后按照以下方式配置它:

root@Artisan:~# cat /usr/share/alsa/alsa.conf.d/20-bluealsa.conf                                     # BlueALSA integration setup

defaults.bluealsa.service "org.bluealsa"
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 20000
defaults.bluealsa.battery "yes"

ctl.bluealsa {
        @args [ SRV BAT ]
        @args.SRV {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.service
                }
        }
        @args.BAT {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.battery
                }
        }
        type bluealsa
        service $SRV
        battery $BAT
}

pcm.bluealsa {
        @args [ SRV DEV PROFILE DELAY ]
        @args.SRV {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.service
                }
        }
        @args.DEV {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.device
                }
        }
        @args.PROFILE {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.profile
                }
        }
        @args.DELAY {
                type integer
                default {
                        @func refer
                        name defaults.bluealsa.delay
                }
        }
        type plug
        slave.pcm {
                type bluealsa
                service $SRV
                device $DEV
                profile $PROFILE
                delay $DELAY
        }
        hint {
                show {
                        @func refer
                        name defaults.namehint.extended
                }
                description "Bluetooth Audio Hub"
        }
}

pcm {
        type bluealsa
        interface "hci0"
        device EB:06:EF:47:83:A6
        profile "a2dp"
}

ctl.!default {
        type bluealsa
        interface "hci0"
}

这里是 ~/asoundrc

root@Artisan:~# cat ~/.asoundrc
defaults.bluealsa.interface "hci0"
defaults.bluealsa.device "EB:06:EF:47:83:A6"
defaults.bluealsa.profile "a2dp"

这是我尝试使用 aplay 播放 mp3 文件时得到的结果:

root@Artisan:~# aplay -D bluealsa:DEV=EB:06:EF:47:83:A6,PROFILE=a2dp,HCI=hci0 /ncsdata/Music/night.mp3
ALSA lib conf.c:4622:(parse_args) Unknown parameter HCI
ALSA lib conf.c:4755:(snd_config_expand) Parse arguments error: No such file or directory
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM bluealsa:DEV=EB:06:EF:47:83:A6,PROFILE=a2dp,HCI=hci0
aplay: main:722: audio open error: No such file or directory
root@Artisan:~# aplay -D bluealsa:DEV=EB:06:EF:47:83:A6,PROFILE=a2dp /ncsdata/Music/night.mp3
bluealsa: D: ../../src/bluealsa-dbus.c:139: Called: org.bluealsa.Manager1.GetPCMs()
ALSA lib ../../../src/asound/bluealsa-pcm.c:755:(_snd_pcm_bluealsa_open) Couldn't get BlueALSA PCM: PCM not found
aplay: main:722: audio open error: No such device

mpd 已针对 bluealsa 配置

audio_output {
        type            "alsa"
        name            "ALSA Bluetooth Headset"
        device          "bluealsa"
        mixer_type    "software"
}

以下是我尝试使用 mpd 播放 mp3 时得到的结果:

root@Artisan:~# /etc/init.d/mpd start
[ ok ] Starting mpd (via systemctl): mpd.service.
root@Artisan:~# mpc clear
volume: 90%   repeat: off   random: off   single: off   consume: off
ERROR: Failed to open audio output

root@Artisan:~# mpc add night.mp3
root@Artisan:~# mpc play
Unknown artist - Night (Original)
[playing] #1/1   0:00/2:46 (0%)
volume: 90%   repeat: off   random: off   single: off   consume: off
root@Artisan:~# mpc stop
volume: 90%   repeat: off   random: off   single: off   consume: off
ERROR: Failed to open audio output

按照第一本和第二本手册设置好一切后,错误消息完全相同。扬声器当然没有声音。我觉得我快找到解决方案了,但不知道我遗漏了什么。谷歌搜索 aplay 或 mpd 消息 - 没有运气。非常感谢您的帮助。

答案1

好吧,经过一番考虑并购买了另一个蓝牙接收器后,我设法通过 bluealsa 和 mpd 在其上传输音频。

以下是我编辑过的配置:

/usr/share/alsa/alsa.conf.d/20-bluealsa.conf

# BlueALSA integration setup

defaults.bluealsa.service "org.bluealsa"
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 10000
defaults.bluealsa.interface "hci0"
defaults.bluealsa.device "EB:06:EF:A6:A9:D4"

ctl.bluealsa {
        @args [ SRV BAT ]
        @args.SRV {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.service
                }
        }
        @args.BAT {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.battery
                }
        }
        type bluealsa
        service $SRV
        battery $BAT
}

pcm.bluealsa {
        @args [ SRV DEV PROFILE DELAY ]
        @args.SRV {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.service
                }
        }
        @args.DEV {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.device
                }
        }
        @args.PROFILE {
                type string
                default {
                        @func refer
                        name defaults.bluealsa.profile
                }
        }
        @args.DELAY {
                type integer
                default {
                        @func refer
                        name defaults.bluealsa.delay
                }
        }
        type plug
        slave.pcm {
                type bluealsa
                service $SRV
                device $DEV
                profile $PROFILE
                delay $DELAY
        }
        hint {
                show {
                        @func refer
                        name defaults.namehint.extended
                }
                description "Bluetooth Audio Hub"
        }
}

pcm {
        type bluealsa
        interface "hci0"
        device "EB:06:EF:A6:A9:D4"
        profile "a2dp"
}

ctl.!default {
        type bluealsa
        interface "hci0"
}

/lib/systemd/system/blueoth.service

[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth

[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap --plugin=a2dp
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1

[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service

/lib/systemd/system/bluealsa.service

[Unit]
Description=BluezAlsa proxy
Requires=bluetooth.service
After=bluetooth.service
[Service]
Type=simple
#User=<user>
#Group=audio
ExecStart=/usr/bin/bluealsa -i hci0 -p a2dp-source -S
[Install]
WantedBy=multi-user.target

不要忘记启用 bluealsa.service:

systemctl enable bluealsa.service

~/.asoundrc 没有必要并且已被删除。

重启后,这个对我有用。另一个问题是它是如何工作的——不幸的是音频非常不稳定。它每半秒就会断一次,这与 NAS 和蓝牙接收器之间的距离无关,也与文件格式无关。不明白为什么,正在寻找解决方案。有什么想法吗?

相关内容