问题
When I try to play a sound which is in .ogg
format , I get an error:
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
Here's the code:
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()
This problem does not seem to occur when I play an ogg file which was already saved, but this problem only occurs when I save a file with pyttsx3 and try to play it.
I also tried using other formats like .wav
, but they are causing problems when I try to forward the song.
Is there any way to fix this problem?
It would be great if anyone could help me out.
来源:https://stackoverflow.com/questions/65769088/pygame-error-not-an-ogg-vorbis-audio-stream