微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame.error: 不是 Ogg Vorbis 音频流

如何解决pygame.error: 不是 Ogg Vorbis 音频流

当我尝试播放 .ogg 格式的声音时,出现错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\default\AppData\Local\Programs\Python\python39\lib\tkinter\__init__.py",line 1884,in __call__
    return self.func(*args)
  File "C:\Users\default\PycharmProjects\pythonProject1\main.py",line 17,in read
    pygame.mixer.music.load(outfile)
pygame.error: Not an Ogg Vorbis audio stream

代码如下:

from tkinter import *
import pyttsx3
import pygame

pygame.mixer.init()
engine = pyttsx3.init()
func_count = 0
song_pos = 0

root = Tk()
outfile = "temp.ogg"

def read():
    global func_count,song_pos
    engine.save_to_file(text.get('1.0',END),outfile)
    engine.runAndWait()
    pygame.mixer.music.load(outfile)
    pygame.mixer.music.play()
    song_pos = 0
    func_count += 1
    print(song_pos)
    stopwatch()

def stopwatch():
    global func_count,song_pos
    if func_count == 2: # checking to see if this function is running multiple times
        func_count = 1
        return
    song_pos += 1
    print(song_pos)
    root.after(1000,stopwatch)

def forward():
    global song_pos,func_count
    song_pos += 10
    print(song_pos)
    func_count += 1
    pygame.mixer.music.load(outfile)
    pygame.mixer.music.play(start = song_pos)
    stopwatch()

text = Text(width=65,height=20,font="consolas 14")
text.pack()

text.insert(END,"This is a text widget\n"*10)

read_button = Button(root,text="Read aloud",command=read)
read_button.pack(pady=20)

forward_button = Button(root,text = "Forward Song",command = forward)
forward_button.pack()

mainloop()

当我播放已经保存的ogg文件时似乎不会出现这个问题,但是只有当我用pyttsx3保存文件并尝试播放时才会出现这个问题。

我也尝试过使用其他格式,例如 .wav,但是当我尝试转发歌曲时它们会导致问题。

有什么办法可以解决这个问题吗?

如果有人能帮助我就好了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。