前段时间分享了,在Jetson nano下USB声卡调试的经历。但是今天将程序开机启动后,发现声音播放不出来。
ssh登录后,使用命令aplay
测试播放正常。 这就尴尬了,软件没法交付了。只有再次解决问题咯。
问题处理
首先查看程序是否正常启动
$ ps -ef|grep aplay_server
root 6446 5941 0 17:30 ? 00:00:13 /home/nano/ros_ws/install/lib/aplay_server/aplay_server_node
一查程序运行正常。突然发现程序是在root
下运行的,是不是用户的问题? 心想:"还有root
执行不了的程序?"。
于是 在终端输入 sudo kill -9 6446
停止运行aplay_server_node
。
将用户切换到root
下 :
$ sudo su
[sudo] password for nano:
root@nano-desktop:/home/nano# aplay leisheng.wav
Playing WAVE 'leisheng.wav' : Unsigned 8 bit, Rate 11025 Hz, Mono
程序运行正常但是没有声音(在nano
用户下播放正常)。
终端中输入命令 pulseaudio
:
root@nano-desktop:/home/nano# pulseaudio
W: [pulseaudio] main.c: This program is not intended to be run as root (unless --system is specified).
W: [pulseaudio] server-lookup.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
W: [pulseaudio] main.c: Unable to contact D-Bus: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
按照提示输入 pulseaudio --system
# pulseaudio --system
W: [pulseaudio] main.c: Running in system mode, but --disallow-exit not set.
W: [pulseaudio] main.c: Running in system mode, but --disallow-module-loading not set.
N: [pulseaudio] main.c: Running in system mode, forcibly disabling SHM mode.
N: [pulseaudio] main.c: Running in system mode, forcibly disabling exit idle time.
W: [pulseaudio] main.c: OK, so you are running PA in system mode. Please make sure that you actually do want to do that.
W: [pulseaudio] main.c: Please read http://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/WhatIsWrongWithSystemWide/ for an explanation why system mode is usually a bad idea.
W: [pulseaudio] authkey.c: Failed to open cookie file '/var/run/pulse/.config/pulse/cookie': No such file or directory
W: [pulseaudio] authkey.c: Failed to load authentication key '/var/run/pulse/.config/pulse/cookie': No such file or directory
W: [pulseaudio] authkey.c: Failed to open cookie file '/var/run/pulse/.pulse-cookie': No such file or directory
W: [pulseaudio] authkey.c: Failed to load authentication key '/var/run/pulse/.pulse-cookie': No such file or directory
在另一终端中输入
# aplay leisheng.wav
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Access denied
aplay: main:788: audio open error: Connection refused
居然命令执行失败。但从上面的信息中可以得知拒绝访问。(还有root
访问不了的)
没有办法那就只好将 root
加入到pulse
和pulse-access
组中,终端中输入:
root@nano-desktop:/home/nano# gpasswd -a root pulse
Adding user root to group pulse
root@nano-desktop:/home/nano# gpasswd -a root pulse-access
Adding user root to group pulse-access
最后,在终端中输入:
root@nano-desktop:/home/nano# aplay leisheng.wav
Playing WAVE 'leisheng.wav' : Unsigned 8 bit, Rate 11025 Hz, Mono
成功播放。
总结:
Jetson nano root下USB声卡播放成功播放声音步骤:
- 配置:
gpasswd -a root pulse
gpasswd -a root pulse-access- 运行:
pulseaudio --system &- 播放:
aplay leisheng.wav
gst-launch-1.0 filesrc location=leisheng.wav ! wavparse ! autoaudiosink
TIPS:
当做了上述操作使用 aplay
与 gst-launch-1.0
还是播放不了的时候,可能需要指定声卡。
使用命令:
pactl set-default-sink `pactl list short sinks | grep usb |awk '{print $2}'`
等同于
- 找到USB声卡的index
$ pacmd list-cards
index: 2
name: <alsa_card.usb-C-Media_Electronics_Inc._USB_Audio_Device-00>- 设置
pacmd set-default-sink 2
在开机启动程序中代码为:
system("pulseaudio --system &");
system("sudo -u '#1000' XDG_RUNTIME_DIR=/run/user/1000 pactl set-default-sink `pactl list short sinks | grep usb |awk '{print $2}'`");
修改配置文件/etc/pulse/default.pa
一劳永逸:
在终端中:输入pacmd
后按tab
键
$ pacmd set-default-sink alsa_output.
alsa_output.platform-sound.analog-stereo
alsa_output.usb-C-Media_Electronics_Inc._USB_Audio_Device-00.analog-stereo
选择 alsa_output.usb-C-Media_Electronics_Inc._USB_Audio_Device-00.analog-stereo
为默认声卡。
使用sudo vi /etc/pulse/default.pa
编辑:
### Make some devices default
#set-default-sink output
#set-default-source input
修改为:
### Make some devices default
#set-default-sink output
set-default-sink alsa_output.usb-C-Media_Electronics_Inc._USB_Audio_Device-00.analog-stereo
#set-default-source input