[Use pulseaudio to play sound in WSL2 GUI]

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

Recently, I am working on a Linux project. Since WSL2 needs to use sound playback, I have no choice but to concentrate on research. Later, a compromise method was used instead, that is pulseaudio. At present, WSL2's support for hardware peripherals is still not very friendly. I have discussed USB support in other articles before, so I won't discuss it here.


1. What is pulseaudio?

PulseAudio (formerly Polypaudio) is a cross-platform, network-enabled sound service for Linux and FreeBSD operating systems. It can be used as an easy Improved Open Sound Backstage (ESD) replacement.

PulseAudio runs on Microsoft Windows and POSIX compliant systems such as Linux and FreeBSD. PulseAudio is free software, based on the GNU Lesser General Public License (LGPL) 2.1.

In a nutshell: a cross-platform tool for streaming sound over the web. We will not discuss the implementation principle today, but just talk about how to deploy it.

2. Deployment steps

1. Download PulseAudio

PulseAudio.zip
In order to reduce the configuration as much as possible, I uploaded a compressed package without configuration.

2. Prerequisites

Here, because the test uses ffplay, and ffplay needs to open a graphical interface, if your WSL2 does not have a GUI, one will appear 段错误. So the premise is that your WSL2 needs to support GUI.

In theory, as long as you can call the playback command, even if there is no GUI, it is also possible, but the premise is that you need to implement it through code or other commands. It's just that ffplay is the easiest way I can think of, otherwise you have to install a player.

How to configure the GUI, please see my other articles Windows11 configuration GUI

3. Start configuration

1. Configure pulseaudio

Unzip pulseaudio.zip to the desktop or any accessible directory, here I choose to unzip to the desktop. It needs to be specially stated that it is best to use mine pulseaudio.zip. There are many versions of this software itself, and there is no guarantee that all versions are available. Moreover, my zip can be used after decompression, no additional configuration is required, at least I ran through it once.

Enter after decompression pulseaudio\bin, then open a powershell or terminal in this directory, and then execute pulseaudio.exe under the bin

./pulseaudio.exe
W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
W: [(null)] pulsecore/core.c: failed to allocate shared memory pool. Falling back to a normal memory pool.
W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
W: [(null)] pulsecore/core-util.c: Secure directory creation not supported on Win32.
E: [(null)] daemon/main.c: Failed to load directory.

Don't panic if the above error message appears, it will not affect the sound output, at least my environment has no effect.

注:这个页面不要关掉,它是个服务,需要一直挂在那里,最小化就行了。

2. Configure microphone permissions

On Windows 10 or 11, no special permission is required for external playback, but it needs to be authorized to use the microphone 麦克风权限. Please read below for the specific method.
Windows 11:
First, Win+i to enter the settings page
Second, find it in the left sidebar 隐私和安全性
Finally, swipe down all the way, find and click 麦克风to enter
insert image description here
insert image description here
Windows 10:
First, Win+i to open the settings Second, find and enter
in the panel Finally, in Find the left sidebar隐私
麦克风
insert image description here

insert image description here

注意:一定要在测试前先打开Windows上的pulseaudio

3. Configure WSL2 external release

Here, I am using the Ubuntu-18.04 version, Ubuntu-20.04 should also be possible (no actual test).
open a powershell

#powershell输入wsl进入环境
wsl
#安装多个wsl分发版
wsl -d <分发版>

After entering the wsl environment, if you have not installed ffmpeg, install ffmpeg first.

sudo apt update
sudo apt install ffmpeg

Next configure an environment variable

vim .bashrc
#添加环境变量
export PULSE_SERVER=tcp:$(cat /etc/resolv.conf | grep nameserver | cut -c 12-23)
#保存
wq
#生效
source .bashrc

Check the following variables:

echo $PULSE_SERVER
#我的
tcp:172.20.144.1

Everyone may be different, this is /etc/resolv.confthe insidenameserver
insert image description here

The last step, just find a wav file or MP3 and copy it to the user directory

ffplay -i "1.wav|1.mp3"

注意:如果你的文件包含空格的话,一定要用双引号括住,不然文件会被解析成多个参数。

At this time, you should be able to hear the music. Be sure to turn on the sound in the lower right corner first.

4. Configure the WSL2 microphone

Here I use a snap application audio-recoder, if you happen to open systemd and install snap like me, you can easily install a recording software.

sudo snap install --edge audio-recorder

Open the application after installation:

audio-recorder

insert image description here
Just follow my configuration, click Start recordingto start recording, click Stop recordingto stop recording. It is very simple to play, just use the above ffplay.

Although it is tcp, my computer does not feel too obvious delay, maybe the more powerful the computer, the better the experience.


Summarize

1. Don’t forget to turn on the sound on Windows 11, and don’t forget to give permissions to the microphone
. 2. There may be a certain delay because it is transmitted through the network.
3. The effect is only a matter of opinion. If you can't meet the requirements, install a real Ubuntu!

Guess you like

Origin blog.csdn.net/jiexijihe945/article/details/131680406