Linux 的常见声音问题

原文链接

Linux上播放声音有时会遇到没声音的问题。下面是我所遇到的问题的一些总结。

以Ubuntu为例,其他系统可能会有所差别。Ubuntu的声音控制程序有两层。一个是pulseaudio,一个是alsa。其中pulseaudio是在alsa基础上建立的。pulseauido为了安全性会要求必须用户登录后才能播放声音。这样就会导致,我们使用robot_upstart软件包把把程序作为systemd 服务时有时候会没有声音。而且即使用户已经登录,当使用su切换用户之后也是无法发出声音的。比如我们的用户是xiaoqiang。在登录之后打开一个terminal

mplayer test.mp3

输入上面的指令进行测试,test.mp3是一个测试音频文件。正常情况下此时应该是有声音的。

sudo su xiaoqiang
mplayer test.mp3

如果执行上面的指令就不会正常播放声音了。虽然也是在用xiaoqiang账号执行指令。

不仅如此,pulseaudio默认禁用root用户播放声音。这有时候会比较麻烦。

而且pulseaudio需要在系统中设置正确的声音输出设备才会有声音。如果系统中的声音设备设置错误,那么我们是无法通过程序选择设备进行播放的。而pulseaudio的声音输出设备即使设置好,可能重启多次之后默认设备还是会变。这样就经常导致我们的程序没有声音。

如果我们想要程序在更宽松的条件下播放声音,就可以和更底层的程序alsa来交互。aplay 就是常用的交互指令

aplay test.mp3

上面是一个简单声音播放指令。这个指令可以在用户未登录的情况下播放声音,同时也可以用root用户播放声音。因为aplay是直接和alsa进行交互的。如果系统中的声音设备选择有问题,我们也可以通过指令直接指定播放设备。

首先列出声音设备

aplay -L

default
    Playback/recording through the PulseAudio sound server
surround21
    2.1 Surround output to Front and Subwoofer speakers
surround40
    4.0 Surround output to Front and Rear speakers
surround41
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50
    5.0 Surround output to Front, Center and Rear speakers
surround51
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
null
    Discard all samples (playback) or generate zero samples (capture)
samplerate
    Rate Converter Plugin Using Samplerate Library
speexrate
    Rate Converter Plugin Using Speex Resampler
jack
    JACK Audio Connection Kit
oss
    Open Sound System
pulse
    PulseAudio Sound Server
upmix
    Plugin for channel upmix (4,6,8)
vdownmix
    Plugin for channel downmix (stereo) with a simple spacialization
hdmi:CARD=HDMI,DEV=0
    HDA Intel HDMI, HDMI 0
    HDMI Audio Output
hdmi:CARD=HDMI,DEV=1
    HDA Intel HDMI, HDMI 1
    HDMI Audio Output
hdmi:CARD=HDMI,DEV=2
    HDA Intel HDMI, HDMI 2
    HDMI Audio Output
hdmi:CARD=HDMI,DEV=3
    HDA Intel HDMI, HDMI 3
    HDMI Audio Output
hdmi:CARD=HDMI,DEV=4
    HDA Intel HDMI, HDMI 4
    HDMI Audio Output
dmix:CARD=HDMI,DEV=3
    HDA Intel HDMI, HDMI 0
    Direct sample mixing device
dmix:CARD=HDMI,DEV=7
    HDA Intel HDMI, HDMI 1
    Direct sample mixing device
dmix:CARD=HDMI,DEV=8
    HDA Intel HDMI, HDMI 2
    Direct sample mixing device
dmix:CARD=HDMI,DEV=9
    HDA Intel HDMI, HDMI 3
    Direct sample mixing device
dmix:CARD=HDMI,DEV=10
    HDA Intel HDMI, HDMI 4
    Direct sample mixing device
dsnoop:CARD=HDMI,DEV=3
    HDA Intel HDMI, HDMI 0
    Direct sample snooping device
dsnoop:CARD=HDMI,DEV=7
    HDA Intel HDMI, HDMI 1
    Direct sample snooping device
dsnoop:CARD=HDMI,DEV=8
    HDA Intel HDMI, HDMI 2
    Direct sample snooping device
dsnoop:CARD=HDMI,DEV=9
    HDA Intel HDMI, HDMI 3
    Direct sample snooping device
dsnoop:CARD=HDMI,DEV=10
    HDA Intel HDMI, HDMI 4
    Direct sample snooping device
hw:CARD=HDMI,DEV=3
    HDA Intel HDMI, HDMI 0
    Direct hardware device without any conversions
hw:CARD=HDMI,DEV=7
    HDA Intel HDMI, HDMI 1
    Direct hardware device without any conversions
hw:CARD=HDMI,DEV=8
    HDA Intel HDMI, HDMI 2
    Direct hardware device without any conversions
hw:CARD=HDMI,DEV=9
    HDA Intel HDMI, HDMI 3
    Direct hardware device without any conversions
hw:CARD=HDMI,DEV=10
    HDA Intel HDMI, HDMI 4
    Direct hardware device without any conversions
plughw:CARD=HDMI,DEV=3
    HDA Intel HDMI, HDMI 0
    Hardware device with all software conversions
plughw:CARD=HDMI,DEV=7
    HDA Intel HDMI, HDMI 1
    Hardware device with all software conversions
plughw:CARD=HDMI,DEV=8
    HDA Intel HDMI, HDMI 2
    Hardware device with all software conversions
plughw:CARD=HDMI,DEV=9
    HDA Intel HDMI, HDMI 3
    Hardware device with all software conversions
plughw:CARD=HDMI,DEV=10
    HDA Intel HDMI, HDMI 4
    Hardware device with all software conversions
usbstream:CARD=HDMI
    HDA Intel HDMI
    USB Stream Output
sysdefault:CARD=PCH
    HDA Intel PCH, ALC892 Analog
    Default Audio Device
front:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    Front speakers
surround21:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    4.0 Surround output to Front and Rear speakers
surround41:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Digital
    IEC958 (S/PDIF) Digital Audio Output
dmix:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    Direct sample mixing device
dmix:CARD=PCH,DEV=1
    HDA Intel PCH, ALC892 Digital
    Direct sample mixing device
dsnoop:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    Direct sample snooping device
dsnoop:CARD=PCH,DEV=1
    HDA Intel PCH, ALC892 Digital
    Direct sample snooping device
hw:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    Direct hardware device without any conversions
hw:CARD=PCH,DEV=1
    HDA Intel PCH, ALC892 Digital
    Direct hardware device without any conversions
plughw:CARD=PCH,DEV=0
    HDA Intel PCH, ALC892 Analog
    Hardware device with all software conversions
plughw:CARD=PCH,DEV=1
    HDA Intel PCH, ALC892 Digital
    Hardware device with all software conversions
usbstream:CARD=PCH
    HDA Intel PCH
    USB Stream Output
usbstream:CARD=CAMERA
    KINGSEN CAMERA
    USB Stream Output

比如我选择hw:CARD=PCH,DEV=0作为播放设备

aplay -D hw:CARD=PCH,DEV=0 tes.mp3

这样就可以正常播放声音了。当然具体的设备和你的系统有关系。

如何通过命令行设置默认语音设备

如上所说,在系统声音输出设备设置错误的情况下我们无法播放声音。在工控机安装在机器人上的情况下,我们大部分情况是没有办法获得一个可用的图形界面的。这时候需要通过命令行去设置默认声音设备。

首先通过下面的指令列出所有的声音设备

pacmd list-cards

]

上图就是我的部分输出。注意这个设备的index是2,如果我们想要设置这个设备为默认设备就可以通过下面的指令

pacmd set-card-profile  2 output:analog-stereo

其中2是设备的index, output:analog-stereo是profile。

audio_play中的设置

如果我们使用audio_play作为ros中声音输出程序。那么我们就可以采用下面的设置

<launch>
  <node name="audio_play" pkg="audio_play" type="audio_play" output="screen">
    <param name="dst" value="alsasink" />
    <param name="device" value="hw:CARD=PCH,DEV=0"/>
    <param name="do_timestamp" value="false"/>
    <param name="format" value="wave"/>
    <param name="channels" value="1"/>
    <param name="sample_rate" value="48000"/>
    <param name="sample_format" value="S16LE" />
    <param name="depth" value="16" />
    <remap from="audio" to="audio/audio_raw" />
  </node>
</launch>

其中比较关键的是dst和device。dst设置为alsasink说明是使用alsa声音程序。device即是上面aplay使用的声音设备。

但是即使按照上面的设置在机器人重启后有时还是会没有声音。这是audio_play自身程序的问题。可以采用这里的修改版本 audio_common

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值