Solving the 2-second sound delay with XBMC and HDMI

On my HTPC, XBMC outputs sound over HDMI to a Panasonic TX-37LZD70 TV. This works great in most respects. However, there is a 2-second delay between when XBMC starts playing audio (music or video with sound), and when it actually outputs from the TV speakers. The sound is in sync with the video; it just takes 2 seconds to start. It’s annoying to always hear a theme song start part-way into the first few notes.

This is a known issue, but isn’t really XBMC’s fault. Basically, this TV (and many like it) doesn’t start playing sound right away. But there is a simple workaround – keep the channel open, by always playing silence. You can do this without any performance penalty.

Run this in a terminal (or in /etc/rc.local, or ~/.gnomerc, or however you like):

aplay -c2 -r48000 -fS16_LE < /dev/zero &

This will constantly play silence to the ALSA sound system (in the case of Ubuntu, via Pulse). As far as I can tell, the sound server just drops it, or consumes very few resources to do it. The system load never increases nor does it appear as active in top.

I've implemented this as a more sophisticated solution. I have created my own xbmc script, which then calls the real XBMC.

#!/bin/bash
# if xbmc already running, exit
if (ps -ef | grep '[/]usr/lib/xbmc/xbmc.bin' >/dev/null 2>&1) ; then
   zenity --error --title "XBMC" --text "XBMC is already running. Please select it from the task bar above."
   die "XBMC is already running!"
fi

# Keep sound channel open (play silence), to prevent 2-second HDMI delay
aplay -c2 -r48000 -fS16_LE < /dev/zero &
APLAY_PID=$!

/usr/bin/xbmc "$@"

kill $APLAY_PID

Copy this script to ~/bin or /usr/local/bin and make it executable. It does two things:

  1. Prevent launching more than one copy of XBMC. An easy mistake to make since Gnome provides no launch feedback and XBMC takes some time to start. A more elegant solution would restore the other XBMC instance instead of nagging the user.
  2. Starts playing silence before starting XBMC, then kills it afterward.

A better solution would be for XBMC to maintain the channel itself (which must be what my DVD player does), but I'm happy with this until they release an official fix.

Tags: ,

  1. Julian’s avatar

    Unfortunately this doesn’t work for me. My decoderbox still doesn’t get any signal when I’m using aplay.

    Reply

    1. Tyler Wagner’s avatar

      “Still doesn’t get any signal”? Meaning it never gets sound? That’s a different problem.

      Reply

    2. Øyvind’s avatar

      This kind of helps in my case. My starting point is no sound and really choppy video for a few seconds while my shittysonic TV syncs up. With this trick, the video isn’t choppy, but I still have no sound for two seconds.

      Reply

      1. Tyler Wagner’s avatar

        Strange. Perhaps the TV is monitoring for a signal containing more than silence? Probably above some dB threshold. Try testing the aplay command but with actual noise, and see if the 2-second delay disappears. If so, that’s the problem (but I have no suggestion to fix it). If not, it must be XBMC itself.

        Next I buy a TV, I’m taking my HTPC into the showroom and testing for this shit. TV manufacturers should fire their TV dev teams and replace them with their PC monitor dev teams.

        Reply

      2. Kieron’s avatar

        I have tried this on my Ubuntu XBMC HTPC, to no avail. Should the command be different if one is using toslink/optical audio?

        Thanks, hope you can help, this has been driving me mad for almost a year now..

        Reply

        1. Tyler Wagner’s avatar

          To answer your question, no. The command just plays audio. How it routes through the sound system is another problem.

          There are many reasons that you might experience a delay or other sound issue. It took me over a year just to be sure that the issue was the TV. I can only advise that you start with your ALSA configuration.

          Reply

        2. Cillian’s avatar

          Absolutely fantastic stuff! I have been seeing this problem with my raspberry pi / xbmc / sound bar set up for months now and had always pegged it to some issue with the rpi.

          Many thanks for this workaround!

          Reply

        3. jobski’s avatar

          Thanks for this fix. Works on XBMC under Arch Linux

          What I did is I created a screen session with just the command running in it.

          Reply

        4. zbig’s avatar

          Thanks a lot for this solution. I have an Onkyo AV receiver connected through HDMI to my laptop with nVidia graphic card, where I run Ubuntu 14.04 for the time being. I don’t use XBMC often, but the problem is very annoying during playing of any kind of audio, no matter if it’s music played in Clementine, YouTube video – first 2-3 seconds are always gone. The bug also “cuts off” all short system sounds.
          Your short terminal command works like a charm, but it was tricky to have it automatically started on every boot. I’ve tried adding it in three places:
          1. to crontab, with @reboot
          2. to the /etc/rc.local
          3. to the ~/.gnomerc

          1st and 2nd methods didn’t do anything good. I think that the silent stream has been active (AV receiver has been indicating that there is active signal), but it wasn’t possible to play any sound in Ubuntu. All output devices disappeared from sound settings (there’s been “dummy output” instead).
          Luckily the 3rd option works like a charm :)

          Reply

          1. Tyler Wagner’s avatar

            I’m glad I could help!

            Solutions #1 and #2 would run as root, which probably ties up the sound device. Solution #3 runs as your user, which is what you want. You should also be able to create a “Startup Application” via the Gnome Control Centre. This creates a file such as ~/.config/autostart/playsilence.desktop :

            [Desktop Entry]
            Name=Play Silence
            Comment=Work around sound output delay bug.
            TryExec=aplay
            Exec=aplay -c2 -r48000 -fS16_LE < /dev/zero
            Icon=gnome-sound-recorder
            Type=Application
            Categories=AudioVideo;
            X-GNOME-Autostart-enabled=true

            Reply

          2. Riccardo’s avatar

            Hı Tyler,

            I have a android mini pc (mk908). Having the same first 2-3 seconds delay on playing f.e. youtube or after I have paused and play it again. Your solution should work, but I cannot figure it out how to write the script for Initial.d on android 4.2.2.

            Script (filename: start-sound):
            #!/bin/sh
            # /etc/init.d/start-sound
            echo “playing sound”
            aplay -c2 -r48000 -fS16_LE < /dev/zero &

            To apply the scripts, I placed the script in the /system/etc/init.d folder and set permissions to rwxrwxrwx then rebooted the machine.

            Can you help me out please. I am out of solutions.

            Regards,
            Riccardo

            Reply

            1. Tyler Wagner’s avatar

              The first question is “Is it a problem with the script itself, or a problem auto-starting it?”

              Is aplay even installed on your android device? Does that script do what you expect when you run it as root?

              If the script works as expected and you just can’t get it to autostart, look for one of the many “run arbitrary commands on restart” tools in the Google Play store.

              Reply

            2. Riccardo’s avatar

              Auto start (init.d) works well with other scripts. Aplay is installed on my (rooted) android system. I think the script doesn’t work. I am a noob with writing scripts. Hope you have an answer in steps.

              Thx for the quick reply.

              Reply

              1. Tyler Wagner’s avatar

                Riccardo,

                Sorry, I can’t help you with basic scripting. Start by reading how to do it from the many online sources. Scripting on Android will be different, though, since all the binaries are in difference places. /system/bin/sh instead of /bin/sh, for instance.

                Using a terminal, run your script as root. If it doesn’t work, debug a line at a time.

                Reply

              2. Riccardo’s avatar

                Any help?

                Reply

              3. Matan’s avatar

                Hi Tyler,
                I have the exact same issue, except, I’m using a Dell Inspiron Laptop connected to a Dell Moniter via HDMI. I have no idea how to input any of those codes/commands. Would you mind giving me some direction?

                Reply

                  1. Matan’s avatar

                    I forgot to mention… I’m running Windows 10

                    Reply

                    1. Tyler Wagner’s avatar

                      Sorry, these instructions assume you are running a Unix-compatible OS like Linux or Mac OSX. If you develop your own solution using Windows PowerShell, I suggest you post it for the world to share.

                      Reply

                    2. Paul’s avatar

                      This issue where hdmi audio cuts the first second has been annoying me on my ubuntu 14.04 LTS system with a Toshiba TV for over a year. Creating a startup application with the “aplay” command you suggested did the trick. Note that Windows doesn’t have this issue with the same TV.

                      Thanks for posting this workaround!

                      Reply

Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.