视频不支持静态源

2024-10-02 06:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Pyglet播放一首歌,但遇到了这个错误

NotImplementedError:视频还不支持静态源。你知道吗

但是文件是mp3格式的。我已经安装了AVbin11-win64.exe(avbin64.dll),它被复制到“C:\Windows\SysWOW64”文件夹中,从https://github.com/AVbin/AVbin/downloads下载。你知道吗

以下是我使用的脚本:

import pyglet
player = pyglet.media.Player()
source = pyglet.media.load(r'C:\Users\MANDAV\Desktop\New folder (2)\Diamond-
Platnumz-All-The-Way-Up-v2.mp3', streaming=False)
player.play()
player.app.run()

Tags: 文件视频格式错误静态mediamp3pyglet
1条回答
网友
1楼 · 发布于 2024-10-02 06:22:47

您的代码包含一些问题。
第一个是player.app.run(),player没有.app—您可能正在寻找pyglet.app.run()。你知道吗

第二件事是你试图调用player.play(),但是你从来没有调用过player.queue(source)。所以要么做source.play()要么做player.queue(source)(两者的作用完全相同,直接播放源代码将在后台创建一个播放器)

第三件事是streaming=False。avbin(至少10个)在将mp3解码为静态源时遇到问题。不太清楚为什么,但是这个项目已经死了将近一个世纪(是的,在短短的几年里,这个代码已经死了10年)。你知道吗

下面是一个最小的工作示例:

import pyglet
source = pyglet.media.load('epic.mp3')
source.play()
pyglet.app.run()

不过,你可能需要一些更多的功能,同时发挥你的东西。下面是一个更大的示例,说明如何为音乐播放应用程序创建一个起点:

import pyglet
from pyglet.gl import *
from collections import OrderedDict

key = pyglet.window.key

class main(pyglet.window.Window):
    def __init__ (self, width=800, height=600, fps=False, *args, **kwargs):
        super(main, self).__init__(width, height, *args, **kwargs)

        self.keys = OrderedDict() # This just keeps track of which keys we're holding down. In case we want to do repeated input.
        self.alive = 1 # And as long as this is True, we'll keep on rendering.

        ## Add more songs to the list, either here, via input() from the console or on_key_ress() function below.
        self.songs = ['A.wav', 'B.wav', 'C.wav']
        self.song_pool = None

        self.player = pyglet.media.Player()
        for song in self.songs:
            media = pyglet.media.load(song)
            if self.song_pool is None:
                ## == if the Song Pool hasn't been setup,
                ##    we'll set one up. Because we need to know the audio_format()
                ##    we can't really set it up in advance (consists more information than just 'mp3' or 'wav')
                self.song_pool = pyglet.media.SourceGroup(media.audio_format, None)
            ## == Queue the media into the song pool.
            self.song_pool.queue(pyglet.media.load(song))

        ## == And then, queue the song_pool into the player.
        ##    We do this because SourceGroup (song_pool) as a function called
        ##    .has_next() which we'll require later on.
        self.player.queue(self.song_pool)
        ## == Normally, you would do self.player.eos_action = self.function()
        ##    But for whatever fucky windows reasons, this doesn't work for me in testing.
        ##    So below is a manual workaround that works about as good.

        self.current_track = pyglet.text.Label('', x=width/2, y=height/2+50, anchor_x='center', anchor_y='center')
        self.current_time = pyglet.text.Label('', x=width/2, y=height/2-50, anchor_x='center', anchor_y='center')


    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = 0

    def on_key_release(self, symbol, modifiers):
        try:
            del self.keys[symbol]
        except:
            pass

    def on_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE: # [ESC]
            self.alive = 0

        elif symbol == key.SPACE:
            if self.player.playing:
                self.player.pause()
            else:
                self.player.play()

        elif symbol == key.RIGHT:
            self.player.seek(self.player.time + 15)

        ## == You could check the user input here,
        ##    and add more songs via the keyboard here.
        ##    For as long as self.song_pool has tracks,
        ##    this player will continue to play.

        self.keys[symbol] = True

    def end_of_tracks(self, *args, **kwargs):
        self.alive=0

    def render(self):
        ## Clear the screen
        self.clear()

        ## == You could show some video, image or text here while the music plays.
        ##    I'll drop in a example where the current Track Name and time are playing.

        ## == Grab the media_info (if any, otherwise this returns None)
        media_info = self.player.source.info
        if not media_info:
            ## == if there were no meta-data, we'll show the file-name instead:
            media_info = self.player.source._file.name
        else:
            ## == But if we got meta data, we'll show "Artist - Track Title"
            media_info = media_info.author + ' - ' + media_info.title

        self.current_track.text = media_info
        self.current_track.draw()

        ## == This part exists of two things,
        ## 1. Grab the Current Time Stamp and the Song Duration.
        ##    Check if the song_pool() is at it's end, and if the track Cur>=Max -> We'll quit.
        ## *  (This is the manual workaround)
        cur_t, end_t = int(self.player.time), int(self.player.source._get_duration())
        if self.song_pool.has_next() is False and cur_t >= end_t:
            self.alive=False

        ## 2. Show the current time and maximum time in seconds to the user.
        self.current_time.text = str(cur_t)+'/'+str(end_t) + 'seconds'
        self.current_time.draw()

        ## This "renders" the graphics:
        self.flip()

    def run(self):
        while self.alive == 1:
            self.render()

            #      -> This is key <     
            # This is what replaces pyglet.app.run()
            # but is required for the GUI to not freeze
            #
            event = self.dispatch_events()

x = main()
x.run()

如果您只是对播放声音感兴趣。
我衷心推荐Pydub。你知道吗

它不仅使用了libav,这在编码和解码时更像是一个实际的标准,而且还具有丰富的特性。例如将.mp3转换成.wav,您可以控制音频增益,同时播放两个音源等

相关问题 更多 >

    热门问题