当我尝试播放 .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
,但是当我尝试转发歌曲时它们会导致问题。
有什么办法可以解决这个问题吗?
如果有人能帮助我就好了。