0

私は長い道のりを歩んできました、そして私はほとんどそこにいます。Thread の使用から Threading の使用に切り替え、再生中にビデオを切り替えることができるようになりましたが、最初のビデオを強制終了または停止するのにまだ問題があります。基本的に、OMXplayer を使用して Raspberry Pi のボタンで制御されるビデオ プレーヤーを作成しています。現時点では、別のボタンを押す前に 1 つのビデオの再生が終了するのを待たなければなりません。そうしないと、複数のビデオが同時に再生されてクラッシュします。

皆さんが提供できるあらゆる助けに感謝します。

#!/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
4

1 に答える 1

3

独自のスレッドを実装する必要があります。

class RaspberryThread(threading.Thread):
    def __init__(self, function):
        self.running = False
        self.function = function
        super(RaspberryThread, self).__init__()

    def start(self):
        self.running = True
        super(RaspberryThread, self).start()

    def run(self):
        while self.running:
            self.function()

    def stop(self):
        self.running = False

次に、while ループを関数から削除して、それらをスレッドに渡すことができるようにします。

v1 = RaspberryThread(function = shoppingcart)
v2 = RaspberryThread(function = dodgeballs)

v1.start()
v2.start()

その後、いつでもスレッドを停止できます

v1.stop()
v2.stop()
于 2015-01-28T21:07:23.093 に答える