【发布时间】:2015-01-28 20:21:56
【问题描述】:
我已经走了很长一段路,而且我快到了。我已经从使用 Thread 转换为 Threading,现在可以在播放过程中切换视频,但我仍然无法杀死或停止第一个视频。基本上,我正在使用 OMXplayer 制作一个由 Raspberry Pi 上的按钮控制的视频播放器。目前,我必须等待一个视频播放完毕才能按下另一个按钮,否则它会因为同时播放多个视频而崩溃。
非常感谢你们提供的任何帮助。
#!/usr/bin/python
import RPi.GPIO as GPIO
import subprocess
import threading
import time
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup(9, GPIO.IN) # Button 1
GPIO.setup(10, GPIO.IN) # Button 2
def shoppingcart():
global current_video
while True:
if GPIO.input(9):
#current_video.terminate()
#current_video.kill()
print "Play Shoppingcart"
time.sleep(1)
current_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/shoppingcart.mp4'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
def dodgeballs():
global current_video
while True:
if GPIO.input(10):
#current_video.terminate()
#current_video.kill()
print "Play Dodgeballs"
time.sleep(1)
current_video=subprocess.Popen(['omxplayer','-b','Desktop/videos/dodgeballs.mp4'],
stdin=subprocess.PIPE,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,close_fds=True)
v1 = threading.Thread( name='shoppingcart', target=shoppingcart ) # Videos thread
v2 = threading.Thread( name='dodgeballs', target=dodgeballs ) # Videos thread
v1.start()
v2.start()
while True:
pass
GPIO.cleanup() #Reset GPIOs
【问题讨论】:
-
使用
thread.stop()。 -
我读了你的一些代码......为什么不使用同一个线程?而且python中没有
thread.stop():docs.python.org/2/library/threading.html#thread-objects -
我的意思是你可以通过继承 Thread 并使用布尔哨兵来实现它。
-
是的,你可以......但我想他想做的下一件事是打开另一个线程。何必? :)
-
@ReutSharabani 我只是尝试将它们放在同一个线程中,但我仍然需要一种方法来在下一个视频播放之前停止子进程或视频。我将它们放在单独的线程中,因为我试图杀死线程而不是子进程。
标签: python multithreading raspberry-pi gpio omxplayer