我发现,pulseaudio5当前存在一些问题,尤其是在通过蓝牙播放音频时。因此,我建议不必在调试时调试它们,而只需将PulseAudio6用于所需的内容即可。
我创建了一个可自动执行以下所有操作的存储库,因此您无需进行所有工作,但是如果您仍然愿意进行此操作,请继续执行以下操作。
回购:https : //github.com/BaReinhard/a2dp_bluetooth
安装过程:
git clone https://github.com/bareinhard/a2dp_bluetooth
cd a2dp_bluetooth/a2dp_source
./configure
等待安装过程完成并重新启动。完成后,您将需要初始化,配对,信任和连接设备。初始时间过后,您只需要打开设备。
配对,信任和连接:
sudo bluetoothctl
[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# default-agent
[bluetooth]# scan on
[bluetooth]# pair XX:XX:XX:XX:XX
[bluetooth]# trust XX:XX:XX:XX:XX
[bluetooth]# connect XX:XX:XX:XX:XX
[bluetooth]# exit
--------------------完整演练:--------------------
编译PulseAudio 6
添加以下文件
/etc/init.d/pulseaudio
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: pulseaudio esound
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: udev network-manager
# Should-Stop: udev network-manager
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the PulseAudio sound server
# Description: System mode startup script for
# the PulseAudio sound server.
### END INIT INFO
DAEMON=/usr/local/bin/pulseaudio
PIDDIR=/var/run/pulse
PIDFILE=$PIDDIR/pid
DAEMONUSER=pulse
PATH=/sbin:/bin:/usr/sbin:/usr/bin
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
pulseaudio_start () {
log_daemon_msg "Starting system PulseAudio Daemon"
if [ ! -d $PIDDIR ]; then
mkdir -p $PIDDIR
chown $DAEMONUSER:$DAEMONUSER $PIDDIR
fi
start-stop-daemon -x $DAEMON -p $PIDFILE --start -- --system --disallow-exit --disallow-module-loading=0 --daemonize --log-target=syslog --high-priority
status=$?
if [ -e /var/run/pulse/.esd_auth ]; then
chown pulse:pulse-access /var/run/pulse/.esd_auth
chmod 640 /var/run/pulse/.esd_auth
fi
if [ -e /var/run/pulse/.pulse-cookie ]; then
chown pulse:pulse-access /var/run/pulse/.pulse-cookie
chmod 640 /var/run/pulse/.pulse-cookie
fi
log_end_msg ${status}
}
pulseaudio_stop () {
log_daemon_msg "Stopping system PulseAudio Daemon"
start-stop-daemon -p $PIDFILE --stop --retry 5 || echo -n "...which is not running"
log_end_msg $?
}
case "$1" in
start|stop)
pulseaudio_${1}
;;
restart|reload|force-reload)
if [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE) >/dev/null 2>&1; then
pulseaudio_stop
pulseaudio_start
fi
;;
force-stop)
pulseaudio_stop
killall pulseaudio || true
sleep 2
killall -9 pulseaudio || true
;;
status)
status_of_proc -p $PIDFILE "$DAEMON" "system-wide PulseAudio" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/pulseaudio {start|stop|force-stop|restart|reload|force-reload|status}"
exit 1
;;
esac
exit 0
/etc/init.d/bluetooth
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: bluetooth
# Required-Start: $local_fs $syslog dbus
# Required-Stop: $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts bluetooth daemons
### END INIT INFO
. /lib/lsb/init-functions
DESC=bluetoothd
DAEMON=/usr/libexec/bluetooth/bluetoothd
#SSD_OPTIONS="--oknodo --quiet --exec $DAEMON --plugin=a2dp"
SSD_OPTIONS="--oknodo --quiet --exec $DAEMON" #Change to this if you want media control using DBus at the expense of volume control
HCI=hci0
case "${1}" in
start)
log_daemon_msg "Starting Bluetooth daemon bluetoothd..."
start-stop-daemon --start --background $SSD_OPTIONS
log_progress_msg "${DAEMON}"
hciconfig $HCI up > /dev/null 2>&1
log_end_msg 0
;;
stop)
log_daemon_msg "Stopping Bluetooth daemon bluetoothd..."
start-stop-daemon --stop $SSD_OPTIONS
log_progress_msg "${DAEMON}"
log_end_msg 0
;;
restart)
${0} stop
sleep 1
${0} start
;;
status)
status_of_proc "$DAEMON" "$DESC" && exit 0 || exit $?
;;
*)
echo "Usage: ${0} {start|stop|restart|status}"
exit 1
;;
esac
exit 0
启用新的init.d服务并使其可执行
sudo chmod +x /etc/init.d/bluetooth
sudo chmod +x /etc/init.d/pulseaudio
sudo update-rc.d bluetooth defaults
sudo update-rc.d pulseaudio defaults
确保我们拥有所有必要的模块
sudo apt-get install bluez pulseaudio-module-bluetooth python-dbus libtool intltool libsndfile-dev libcap-dev libjson0-dev libasound2-dev libavahi-client-dev libbluetooth-dev libglib2.0-dev libsamplerate0-dev libsbc-dev libspeexdsp-dev libssl-dev libtdb-dev libbluetooth-dev intltool autoconf autogen automake build-essential libasound2-dev libflac-dev libogg-dev libtool libvorbis-dev pkg-config python -y
转到主目录并从git源安装json-c(PA6必需)
cd ~
git clone https://github.com/json-c/json-c.git
cd json-c
./configure
make
sudo make install
转到主目录并从git源安装libsndfile
git clone git://github.com/erikd/libsndfile.git
cd libsndfile
./autogen.sh
./configure --enable-werror
make
sudo make install
确保正在搜索蓝牙(sudo hciconfig hci0 piscan
已弃用)
cat << EOT | sudo tee -a /etc/bluetooth/main.conf
[Policy]
AutoEnable=true
EOT
导航到主目录并从git源安装PulseAudio 6
git clone --branch v6.0 https://github.com/pulseaudio/pulseaudio
cd pulseaudio
sudo ./bootstrap.sh
sudo make
sudo make install
sudo ldconfig
确保脉搏在所有必要组中
sudo addgroup --system pulse
sudo adduser --system --ingroup pulse --home /var/run/pulse pulse
sudo addgroup --system pulse-access
sudo adduser pulse audio
sudo adduser root pulse-access
sudo adduser pulse lp
更新/etc/pulse/system.pa
并/etc/pulse/daemon.conf
如下所示:
/etc/pulse/system.pa
#!/usr/bin/pulseaudio -nF
#
# This file is part of PulseAudio.
#
# PulseAudio is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PulseAudio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PulseAudio; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
# This startup script is used only if PulseAudio is started in system
# mode.
### Automatically load driver modules depending on the hardware available
.ifexists module-udev-detect.so
#load-module module-udev-detect
load-module module-udev-detect tsched=0
.else
### Use the static hardware detection module (for systems that lack udev/hal support)
load-module module-detect
.endif
### Load several protocols
.ifexists module-esound-protocol-unix.so
load-module module-esound-protocol-unix
.endif
load-module module-native-protocol-unix
### Automatically restore the volume of streams and devices
load-module module-stream-restore
load-module module-device-restore
### Automatically restore the default sink/source when changed by the user
### during runtime
### NOTE: This should be loaded as early as possible so that subsequent modules
### that look up the default sink/source get the right value
load-module module-default-device-restore
### Automatically move streams to the default sink if the sink they are
### connected to dies, similar for sources
load-module module-rescue-streams
### Make sure we always have a sink around, even if it is a null sink.
load-module module-always-sink
### Automatically suspend sinks/sources that become idle for too long
load-module module-suspend-on-idle
### Enable positioned event sounds
load-module module-position-event-sounds
### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif
load-module module-bluetooth-policy
load-module module-switch-on-connect
/etc/pulse/daemon.conf
# This file is part of PulseAudio.
#
# PulseAudio is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PulseAudio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PulseAudio; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
## Configuration file for the PulseAudio daemon. See pulse-daemon.conf(5) for
## more information. Default values are commented out. Use either ; or # for
## commenting.
; daemonize = no
; fail = yes
; allow-module-loading = yes
; allow-exit = yes
; use-pid-file = yes
; system-instance = no
; local-server-type = user
; enable-shm = yes
; shm-size-bytes = 0 # setting this 0 will use the system-default, usually 64 MiB
; lock-memory = no
; cpu-limit = no
; high-priority = yes
; nice-level = -15
; realtime-scheduling = yes
; realtime-priority = 5
exit-idle-time = -1
; scache-idle-time = 20
; dl-search-path = (depends on architecture)
; load-default-script-file = yes
; default-script-file = /etc/pulse/default.pa
; log-target = auto
; log-level = notice
; log-meta = no
; log-time = no
; log-backtrace = 0
# resample-method defaults to speex-float-1 on most architectures,
# speex-fixed-1 on ARM
; resample-method = speex-float-1
resample-method = ffmpeg
enable-remixing = no
enable-lfe-remixing = no
; flat-volumes = yes
; rlimit-fsize = -1
; rlimit-data = -1
; rlimit-stack = -1
; rlimit-core = -1
; rlimit-as = -1
; rlimit-rss = -1
; rlimit-nproc = -1
; rlimit-nofile = 256
; rlimit-memlock = -1
; rlimit-locks = -1
; rlimit-sigpending = -1
; rlimit-msgqueue = -1
; rlimit-nice = 31
; rlimit-rtprio = 9
; rlimit-rttime = 1000000
default-sample-format = s16le
default-sample-rate = 44100
;alternate-sample-rate = 48000
default-sample-channels = 2
; default-channel-map = front-left,front-right
default-fragments = 10
default-fragment-size-msec = 10
; enable-deferred-volume = yes
; deferred-volume-safety-margin-usec = 8000
; deferred-volume-extra-delay-usec = 0
设置udev规则
编辑/etc/udev/rules.d/99-com.rules
并添加以下两行:
SUBSYSTEM=="input", GROUP="input", MODE="0660"
KERNEL=="input[0-9]*", RUN+="/usr/local/bin/bluez-udev"
创建 /usr/local/bin/bluez-udev
/ usr / local / bin / bluez-udev
#!/bin/bash
name=$(sed 's/\"//g' <<< $NAME)
#exit if not a BT address
if [[ ! $name =~ ^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$ ]]; then exit 0; fi
bt_name=`grep Name /var/lib/bluetooth/*/$name/info | awk -F'=' '{print $2}'`
audio_sink=bluez_source.$(sed 's/:/_/g' <<< $name)
action=$(expr "$ACTION" : "\([a-zA-Z]\+\).*")
logger "Action: $action"
if [ "$action" = "add" ]; then
logger "[$(basename $0)] Bluetooth device is being added [$name] - $bt_name"
logger "[$(basename $0)] Patching $audio_source into ALSA sink #$audio_sink"
#hciconfig hci0 noscan
bluetoothctl << EOT
discoverable off
EOT
# Grab Card Number
PACARD=`pactl list cards | grep "Card #" | sed "s/Card #//"`
# Grab Sink Input if it exists
audio_source=`pactl pactl list sink-inputs | grep "Sink Input" | sed "s/Sink Input #//"`
if [ $audio_source = "" ];then
sleep 5
audio_source=`pactl pactl list sink-inputs | grep "Sink Input" | sed "s/Sink Input #//"`
fi
pactl set-sink-volume $audio_sink 65537
if [ $audio_source != "" ]; then
pactl set-source-volume $audio_source 90%
fi
pactl set-card-profile $PACARD a2dp_sink
pactl set-default-sink $audio_sink
# loop back this source to the default sink
handle=$(pactl load-module module-loopback source=$audio_source sink=$audio_sink)
logger "[$(basename $0)] PulseAudio module-loopback returned handle [$handle]"
logger "$bt_name"
fi
if [ "$action" = "remove" ]; then
# Grab Sink Input if it exists
audio_source=`pactl pactl list sink-inputs | grep "Sink Input" | sed "s/Sink Input #//"`
if [ $audio_source = "" ];then
sleep 5
audio_source=`pactl pactl list sink-inputs | grep "Sink Input" | sed "s/Sink Input #//"`
fi
pactl set-sink-volume 0 65537
if [ $audio_source = "" ]; then
# pactl set-default-sink 0
pactl set-source-volume $audio_source 90%
else
pactl move-sink-input $audio_source 0
fi
logger "[$(basename $0)] Bluetooth device is being removed [$name] - $bt_name"
#hciconfig hci0 pscan
bluetoothctl << EOT
discoverable on
EOT
# remove any loopback modules assigned to this source
# only required for USB sound cards, which PulseAudio will not automatically remove
for handle in $(pactl list short modules | grep module-loopback | grep source=$audio_source | cut -f 1); do
logger "[$(basename $0)] Unloading module-loopback with handle [$handle]"
pactl unload-module $handle
done
sleep 5
amixer cset numid=3 80%
amixer cset numid=3 80%
fi
确保bluez-udev是可执行的
sudo chmod +x /usr/local/bin/bluez-udev
摘要
在这里做什么?
- 为蓝牙和PulseAudio创建init.d服务并启用它们
- 安装PulseAudio6的依赖项
- 编译PulseAudio6并将Pulse用户添加到必要的组(大多数操作已经完成)
- 设置daemon.conf和system.pa以加载适当的模块
- 创建udev规则,以在每次连接设备时运行bluez-udev。bluez-udev检查该设备是否为蓝牙设备,如果是,它将尝试将当前播放的音频连接到pulseaudio创建的蓝牙设备接收器。蓝牙断开连接后,它将把数据流移回默认接收器,或接收器0。在那里,有了所有连接的蓝牙设备后,bluez-udev规则将自动将播放音乐连接到新接收器。连接的蓝牙设备。当然,如果这看起来令人生畏