0

我正在使用 python 中的 pyglet 包为音乐播放器编写代码。我可以成功播放一首歌。但是打电话后我不能暂停这首歌pyglet.app.run()

如果我pyglet.app.run()在暂停后打电话,我就无法播放这首歌。如何在我的代码中管理播放和暂停?

我的代码

import pyglet

import time

player = pyglet.media.Player()

player.queue(pyglet.resource.media('1.mp3'))

player.play()

pyglet.app.run()

time.sleep(5)

player.pause()

print player.time()
4

4 回答 4

2
#This should play the video then pause after 12 seconds of
# play. Stay paused for 4 seconds and play again.
#Note AVbin7 needs to be installed to play mp4s

import pyglet
from time import sleep
vidPath="/home/path/to/file.mp4"
window = pyglet.window.Window()
player = pyglet.media.Player()
source = pyglet.media.StreamingSource()
MediaLoad = pyglet.media.load(vidPath)
pt = 0
player.queue(MediaLoad)
player.play()

@window.event
def on_draw():
    global pt
    window.clear()
    if player.source and player.source.video_format:
        player.get_texture().blit(0,0)
        if player.time >= 12 and pt == 0:
            print("pausing")
            player.pause()
            sleep(4)
            player.play()
            pt = 1

pyglet.app.run()
于 2019-01-16T06:52:57.147 回答
0
pyglet.app.run()

这个函数是一个无限循环。您可以创建新线程,您可以睡觉。

class ControllerThread(threading.Thread):
def __init__(self, player):
    threading.Thread.__init__(self)
    self.player = player

def run(self):
    time.sleep(10)
    self.player.pause()

player = pyglet.media.Player()
pyglet.resource.path = [r'path/to/music']
pyglet.resource.reindex()

music = pyglet.resource.media(r'awesomeSong.mp3')
player.queue(music)
player.play()

c = ControllerThread(player)
c.start()

pyglet.app.run()
于 2012-05-11T17:05:31.200 回答
0

以下示例(python 3.7、pyglet 1.5.6)使用 SPACE 暂停/恢复视频播放:

import pyglet
from pyglet.window import key

source = pyglet.media.load(VIDEO_FILE_PATH)
fmt = source.video_format

player = pyglet.media.Player()
player.queue(source)
player.play()

window = pyglet.window.Window(width=fmt.width, height=fmt.height)

@window.event
def on_draw():
    player.get_texture().blit(0, 0)

@window.event
def on_key_press(symbol, modifiers):
    if symbol == key.SPACE:
        if player.playing:
            player.pause()
        else:
            player.play()
        
pyglet.app.run()
于 2020-06-22T00:56:25.427 回答
0

这段代码应该适合你

import pyglet
import threading
import time
class ControllerThread(threading.Thread):
    def __init__(self, player):
    threading.Thread.__init__(self)
        self.player = player
    def run(self):
        self.player.pause()
player = pyglet.media.Player()
pyglet.resource.path = [r'/your/source/path/']
pyglet.resource.reindex()
def muz():
    music = pyglet.resource.media(r'file.mp3')
    player.queue(music)
    player.play()
t=threading.Thread(target=muz)  
a=''
while a!=' ':
   a=raw_input("STOP/Play S/P: ")
   if a=='s':
       c = ControllerThread(player)
       c.start()
   elif a=='p':
       if t.isAlive():
           player.play()
       else:
           t=threading.Thread(target=muz)   
           t.start()
pyglet.app.run()
于 2016-10-30T15:40:54.620 回答