UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 6: invalid start byte

35 Views Asked by At

Hello,

I am developing a project with Python, and I need to read the response I receive from an API aloud. It was working before, but suddenly it stopped working. What could be the issue?

Python Version: 3.9.13 | Playsound Version: 1.2.2 | Operating System: Windows 11

Code:

import requests
import random
from playsound import playsound
import os
from colorama import Fore,init
init(autoreset=True)
import json
from datetime import datetime
import time

def speak(txt):
    
    nows = datetime.now()
    
    with open("Library_9500.json", "r", encoding="utf-8") as f:
        data = json.load(f)

    CHUNK_SIZE = 1024
    url = "https://api.elevenlabs.io/v1/text-to-speech/{}".format(data["tts_voice"])

    headers = {
    "Accept": "audio/mpeg",
    "Content-Type": "application/json",
    "xi-api-key": "{}".format(data["tts_api"])
    }

    data = {
    "text": "{}".format(txt),
    "model_id": "eleven_multilingual_v2",
    "voice_settings": {
        "stability": 0.5,
        "similarity_boost": 0.5
        }
    }

    response = requests.post(url, json=data, headers=headers)
    rand = random.randint(0, 10000)
    file_name = "audio_" + str(rand) + ".mp3"

    with open(file_name, 'wb') as f:
        for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
            if chunk:
                f.write(chunk)
                
    time.sleep(1)
    print(Fore.GREEN+"SB-9500: " + Fore.WHITE + "{}".format(txt))
    logs4 = open("SystemLog.txt", "a", encoding="utf-8")
    logs4.write("[{}] > SB-9500: {}\n\n".format(nows, txt))
    logs4.close()
    playsound(file_name)
    os.remove(file_name)
    
speak("Hello World!")

Error:

SB-9500: Hello World!
Traceback (most recent call last):
  File "c:\Users\pc\OneDrive\Masaüstü\SB-9500\tts_api.py", line 63, in <module>
    speak("Hello World!")
  File "c:\Users\pc\OneDrive\Masaüstü\SB-9500\tts_api.py", line 60, in speak
    playsound(file_name)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 35, in _playsoundWin
    winCommand('open "' + sound + '" alias', alias)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 30, in winCommand
    '\n    ' + errorBuffer.value.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 6: invalid start byte

It was working before. Now, such an error has occurred. How can I fix it?

0

There are 0 best solutions below