5

I have a problem with audio streaming that I can not listen audio from Pulseaudio when using VNC with a Linux Desktop Environment.

However it works if I export the audio to XSDL. But the problem is that after I exit the application the sound does not work in the background.

I know that Pulseaudio only supports Xorg servers, so how to stream audio to VNC or any other application on Android and to run it in the background.

I know that there is an application Simple Protocol Player that listens to pulseaudio sound from Xorg Server, but it does not work for me and I do not know the reason.

1
  • Which is your client, Linux or Termux? Which computer are you trying to get the audio to work on, the client or the server? Which device is PulseAudio installed on? Commented Jul 23, 2022 at 0:49

3 Answers 3

3

There are two possibilities I know of, to play sound from a Linux environment on Android device:

  • Play through native ALSA libraries
  • Stream through PulseAudio
    • Forward to X Server (on UNIX or TCP socket)
    • Stream over IP (TCP or UDP) e.g. when using VNC

First method works natively through ALSA kernel module/drivers while second and third work through PulseAudio libraries i.e. different modules and protocols (pulseaudio package should be installed on Termux / Linux distro).

Mostly Android OEMs base their sound architecture on ALSA, though not necessary:

"You can use Advanced Linux Sound Architecture (ALSA), Open Sound System (OSS), or a custom driver"

In case of ALSA kernel lists devices in /dev/snd/ which can be manipulated to direct sound towards sound card. Complete tutorial can be found here. It works, but not for all sound formats. It's because ALSA drivers loaded on Android devices can't be controlled (at least I could not) through standard Linux userspace tools in straightforward manner because they aren't compatible (refer to changes/additions in Linux kernel by Google/OEMs, the closed source proprietary HALs in userspace, and differences in ALSA vs. TinyALSA). Also access to /proc/asound/ is not possible without root, see details in this answer. ALSA project is here.

Also there are more customized solutions that work with ALSA and PulseAudio both such as Music Player Daemon.

When using X Server:
XSDL Server is the only well developed X server Android app I know of, no more developed unfortunately. It has a built-in PulseAudio server with module-cli-protocol-tcp already loaded, listening on standard port 4712 by default. Once the X Session is started on Linux, we can direct sound from Linux PulseAudio client libraries to XDSL PulseAudio server by setting the following environment variable on Linux:

$ export PULSE_SERVER=tcp:127.0.0.1:4712

* 127.0.0.1 indicates that Linux and XDSL are running on same device

Or to set permanently, edit file:

# /etc/pulse/client.conf or ~/.config/pulse/client.conf

default-server = tcp:127.0.0.1:4712

Now any media player running in this environment that makes use of PulseAudio client libraries, will stream its audio to XDSL app. You can forward X session display and sound to X server running on a PC as well but that needs a more complicated setup.

When using VNC:
VNC protocol by default doesn't support sound. However, PulseAudio server can stream over TCP/UDP using different protocols. There are apps available for Android that can play simple protocol (e.g. Simple Protocol Player) and real-time transport protocol (e.g. VLC). For this we need to load the related PulseAudio module.
To setup local PulseAudio daemon, first of all comment out following lines:

# /etc/pulse/default.pa

#ifexists module-console-kit.so
#load-module module-console-kit
#.endif

Otherwise daemon will fail to start if /usr/lib/pulse*/modules/module-console-kit.so exists but consolekit package isn't installed (which is deprecated, replaced by systemd-login and no more present on many Linux repos).

Ensure that autospawn is set to yes so that we don't need to start daemon manually and it is auto started by Desktop Environment. Not necessary if we start daemon manually.

# /etc/pulse/client.conf

# Autospawn a PulseAudio server/daemon when needed
autospawn = yes

Now start pulseaudio daemon and note the value of source, for me it's auto_null.monitor:

$ pulseaudio --start && pactl list | grep -A2 -i RUNNING
$ DISPLAY=:0 pulseaudio --start

Load simple-protocol module:

$ DISPLAY=:0 pactl load-module module-simple-protocol-tcp rate=48000 format=s16le channels=2 source=auto_null.monitor record=true port=8000 listen=127.0.0.1

Or to set permanently, edit file:

$ echo 'load-module module-simple-protocol-tcp rate=48000 format=s16le channels=2 source=auto_null.monitor record=true port=8000 listen=127.0.0.1' >> /etc/pulse/default.pa

Following should be enabled in DE startup settings, necessary because some programs such as emixer on Enlightenment Desktop Environment works only if PulseAudio server in local X server is published:

$ DISPLAY=:0 start-pulseaudio-x11

Start Simple Player app and match bitrate and port set in above command i.e. 48000 and 8000 in my case. Don't use listen=127.0.0.1 if you want to listen on PC too.

I have tested both of PulseAudio based solutions on ArchLinuxARM and Ubuntu, and they work without root access. However SELinux may restrict access to certain resources if running in non-root context. Also, in order to get network access and create sockets, you have to be member of inet (3003) group necessarily. All apps with android.permission.INTERNET granted are member of this group.

16
  • Do you also work on Userland ? ... Or just on Termux
    – Maher_2000
    Commented Dec 25, 2018 at 15:24
  • @Maher_2000 I tested LinuxDeploy and a few other apps but not UserLAnd. I have developed a Linux environment myself in chroot and namespaces, and I use through ssh and Termux. Commented Dec 25, 2018 at 15:44
  • Unfortunately, I tried several times to play the sound on a simple protocol player but the sound does not work and the application shows me the message "unable to stream" and also made a mistake in pulseaudio I do not know what the reason and I tried on Termux and Useland but anyway thank you for helping me , Note : I'm new to using Linux and I use the languages translator to speak with you in English so you will notice that my English is not good or understandable for you
    – Maher_2000
    Commented Dec 25, 2018 at 22:44
  • @Maher_2000 are you sure you entered IP, port, bitrate correctly in Simple Protocol Player? If so, the problem is with your Linux configuration. Execute all commands and edit files with great care. Sometimes bad permissions set on files may cause such problems. Since you are not running as root, there is possibility that that you lack permissions for certain tasks. SELinux can be a reason, set it permissive if you can, or view avc denials. Commented Dec 26, 2018 at 8:28
  • 1
    @user3123159 play-audio is a Termux package, so it addresses the first half of the question: How to Play Sound from Termux..., not the second half: ... when using Linux?. ALSA and PA in a Linux environment give low level control to audio framework, though one may opt to use high level library like OpenSL ES (which play-audio uses). Commented Jun 4, 2021 at 12:53
1

Good news, I've been able to extract audio to Simple Protocol Player using PulseAudio for Android on Termux, but the problem is that this method does not work on the official PulseAudio because it does not have access privileges for root only. If you have root on your system, you can extract the audio of the Simple Protocol Player.

WARNING: If the audio does not work, check the link below because there are some notes I did not mention

On Termux

  1. Run pactl list sources short (visit the link below to find out the usefulness of this command before you complete reading)

  2. Download Simple Protocol Player

  3. Run

    echo -e "\n# Simple Protocol Player\n\nload-module module-simple-protocol-tcp source=OpenSL_ES_sink.monitor record=true port=12345 rate=44100" >> $PREFIX/etc/pulse/default.pa
    
  4. Restart Termux

  5. Close all windows in Termux

  6. Run either

    • pulseaudio -k
      pulseaudio --start
      

    or

    • only: pulseaudio --start
  7. Install sox: apt install sox

  8. Play sound: play [file.mp3]

For more information visit, kaytat's blog - Simple Protocol Player

1
  • @IrfanLatif It is not worth placing as a best answer because this method works on Termux only, which can not be used even from within the Linux environment installed on Termux. This is due to the Termux developers who have customized the Pulseaudio package that works well on their app only but cannot works on others applications example Userland or will working if them have access to permission Root
    – Maher_2000
    Commented Dec 28, 2018 at 1:21
0

Mplayer works fine for me:

pkg install mplayer;
mplayer sound.mp3;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .