回调函数在Python中URL流开始播放的确切时刻



我目前正在为Raspberry Pi开发Python网络广播播放器。功能很简单:在用户输入时,程序从预定义的列表中随机选择一个URL无线电流,并开始通过omxplayer作为子进程播放。

这是我的代码:

import simpleaudio as sa
import random
import sys, termios, tty, os
import subprocess
radioList = ["https://edge126.rcs-rds.ro/profm/profm.mp3", "https://live.guerrillaradio.ro:8443/guerrilla.aac", "http://aska.ru-hoster.com:8053/autodj", "https://live.rockfm.ro/rockfm.aacp", "https://live.magicfm.ro/magicfm.aacp", "http://zuicast.digitalag.ro:9420/zu"]
class AudioPlayer: # used only for playing noise.wav until radio stream starts 
def __init__(self):
self.play_obj = None
def play(self, filename):
if not self.is_done():
self.play_obj.stop()
wave_obj = sa.WaveObject.from_wave_file(filename)
self.play_obj = wave_obj.play()

def stop(self):
self.play_obj.stop()
def is_done(self):
if self.play_obj:
return not self.play_obj.is_playing()
return True
def getRandomURL(radioList): # returns random URL from provided list
r = random.randint(1, len(radioList)) - 1
return radioList[r]
def getch(): # reads input character from stdin
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch

noisePlayer = AudioPlayer() # create audio instance
omxprocess = ""

while True:
char = getch()

if(char == "p"):
if omxprocess != "":
omxprocess.kill()
omxprocess = subprocess.Popen(['omxplayer', getRandomURL(radioList)], stdin=subprocess.PIPE) # you should change 'omxplayer' to 'cvlc' or 'mplayer' if you're not running this code on a raspi

# noisePlayer.play("noise.wav") # this file should be played from the moment the above process is started until the radio stream is actually loaded and starts playing

if(char == "x"):
if omxprocess != "":
omxprocess.kill()
exit()

然而,我在Raspberry Pi Zero上运行此代码,通常需要3-4秒(甚至更长时间(才能开始播放流。我想做的是播放一个本地音频文件(本例中为noise.wav,可以在以下位置生成:https://www.random.org/audio-noise/)直到流开始播放的确切时刻,所以通勤电台之间不会有无声的死亡时刻。我的问题是,我不知道如何完成这样的事情,因为我没有发现omxplayer触发的任何信号或事件,也没有发现媒体流实际加载并开始播放时的vlc或mplayer。

然而,我确实发现omxplayer会向stdout发送一些信息,这些信息会在流开始播放时打印出来(vlc和mplayer也是如此(。这里有一个例子:

pi@raspi:~ $ omxplayer https://live.rockfm.ro/rockfm.aacp
Audio codec aac channels 2 samplerate 44100 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)
pi@raspi:~ $

我很好奇是否有什么方法可以使用此输出作为触发器来阻止noisePlayer播放(使用noisePlayer.stop(((,或者是否有其他方法可以在广播流开始播放时触发noisePlayer.stop((?我尝试过的一个选项是使用线程模块中的定时器功能,但它经常与无线电流叠加,这样可以更快地开始播放:

Timer(0.5, noisePlayer.play, ["noise.wav"]).start()
Timer(1.5, noisePlayer.stop).start()

我发现的唯一一篇关于我关注的问题的文章是:Python与omxplayer通信然而,它并没有完全回答我的问题。

如果有人能给出这个问题的确切实施方案或如何解决的解决方案,我将不胜感激,因为我根本无法理解:(

您只需要为stdout包含一个subprocess.PIPE
如果您希望测试stderr,还可以包含一个stderr。

import subprocess as sub
stdout, stderr  ##  forward declarations so that these exist within scope
while True:
char = getch()
if(char == "p"):
if omxprocess != "":
omxprocess.kill()
command = ['omxplayer', getRandomURL(radioList)]
omxprocess = sub.Popen(command, stdin=sub.PIPE, stdout=sub.PIPE, stderr=sub.PIPE)
stdout, stderr = omxprocess.communicate()
noisePlayer.play("noise.wav")
if stdout or stderr: noisePlayer.stop()
if(char == "x"):
if omxprocess != "":
omxprocess.kill()
exit()

相关内容

  • 没有找到相关文章

最新更新