0

I have a working Python script that streams an internet radio station with Omxplayer. The script restarts Omxplayer if Omxplayer stops, such as when the internet signal is lost and reconnects. Restarting automatically is the whole point of the script.

‣ Is there a way to send the sound from Omxplayer to a USB card? I don't think there is. [There is a way. See the answer below.] It doesn't affect Omxplayer to run raspi-config and select USB Audio as the default. Omxplayer sends the sound to HDMI.

VLC can send sound to a USB card, but I couldn't make the script work with VLC. VLC stays open when the internet drops, but Omxplayer closes when the internet drops. Also, player.is_playing() is true when VLC is open, even when VLC isn't playing anything.

‣ Is there a way to check whether VLC is idle, that is, open but not playing anything?

‣ Is there a different player that Python can easily check whether it's playing, like Omxplayer, and which sends sound to the USB card, like VLC?

Here is the working script with Omxplayer.

#!/usr/bin/env python3

from omxplayer.player import OMXPlayer
from time import sleep

STREAM = 'https://14523.live.streamtheworld.com/CLASSICALSTREAMAAC.aac'

# Starts playing.
player = OMXPlayer(STREAM)

# Checks whether playing.
while True:
  sleep(10)
  try:
    while player.is_playing():
      sleep(10)
    else:
      sleep(10)
# Tries to start playing again.
  except:
    try:
      player = OMXPlayer(STREAM)
      sleep(10)
    except:
      sleep(10)

To stop the music stream, press Ctrl+C twice. Or, if Terminal is closed, open Terminal and type killall omxplayer.bin && killall python3.

Here is the nonworking script with VLC.

#!/usr/bin/env python3

import vlc
import time

STREAM = 'https://14523.live.streamtheworld.com/CLASSICALSTREAMAAC.aac'

# Starts playing.
player = vlc.MediaPlayer()
media = vlc.Media(STREAM)
player.set_media(media)
player.play()

# Checks whether playing, but doesn't work,
# because is_playing returns True when VLC is open and idle.
while True:
  time.sleep(10)
  try:
    while player.is_playing():
      time.sleep(10)
    else:
      time.sleep(10)
# Tries to start playing again.
  except:
    try:
      player.play()
      time.sleep(10)
    except:
      time.sleep(10)
0

I found how to send the sound from OMXPlayer to a USB card, thanks to a gist from Thijs Triemstra. OMXPlayer has command line option -o for audio out device. In Terminal I ran aplay -l and saw that my USB audio device was card 2. So, a 2 in the argument -o alsa:hw:2,0 sent the sound to my USB card.

The final script also included an initial pause of two minutes, waiting for Wi-Fi when the script runs on startup, and included four URLs to try.

#!/usr/bin/env python3

from time import sleep
from omxplayer import OMXPlayer

# Waits two minutes for the RPi to connect to Wi-Fi.
sleep(120)

# Specifies URLs and where to output the sound.
url1 = 'http://ice2.somafm.com/thistle-128-aac'
url2 = 'http://ice6.somafm.com/thistle-128-aac'
url3 = 'http://ice4.somafm.com/thistle-128-aac'
url4 = 'http://ice1.somafm.com/thistle-128-aac'
output = 'alsa:hw:2,0'

# Starts playing.
url = url1
player = OMXPlayer(url, args=['-o', output])
sleep(30)

# Checks whether playing. Checks every 10 seconds.
while True:
  try:
    player.is_playing()
    sleep(10)
  except:
#   If not playing, tries to start playing again.
    try:
      url = url1
      player = OMXPlayer(url, args=['-o', output])
      sleep(30)
      player.is_playing()
      sleep(10)
    except:
#     If the URL doesn't play, tries another URL.
      try:
        url = url2
        player = OMXPlayer(url, args=['-o', output])
        sleep(30)
        player.is_playing()
        sleep(10)
      except:
#       Tries another URL.
        try:
          url = url3
          player = OMXPlayer(url, args=['-o', output])
          sleep(30)
          player.is_playing()
          sleep(10)
        except:
#         Tries another URL.
          try:
            url = url4
            player = OMXPlayer(url, args=['-o', output])
            sleep(30)
            player.is_playing()
            sleep(10)
          except:
            sleep(10)

Also, I found I needed to change a configuration. In Terminal I ran xdpyinfo | grep dimensions. The resolution of my monitor was shown, 1920x1200. So, I ran sudo raspi-config, selected Display Options, selected Resolution, and selected DMT Mode 59 1920x1200 60 Hz 16:10. (CEA is for TVs and DMT is for monitors.) I selected OK, selected Finish, and selected No for no reboot.

I had to set a specific screen resolution or else I couldn't shutdown, disconnect the monitor, and restart. When the default screen resolution was Monitor preferred resolution, that setting made Raspberry Pi OS hang when booting if a monitor wasn't detected when booting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.