UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1973: character maps to <undefined> but Only With MP3 Files

Viewed 84

The error above appear but only with MP3 Files and not when I open a python File instead

 from pygame import mixer 

 mixer.init() 


 mp3button = Button(master, text="Choose MP3", highlightthickness=0,bd=0, bg='black
',activebackground="black",fg="white",  command = lambda : see_check())




  checkvar = IntVar()
    checkplay = Checkbutton(master, text="Play MP3?",variable=checkvar ,onvalue=1 ,offvalue=0 ,font=('Arial',12), bg='blue', fg='white', activebackground='blue', activeforeground='white')



  def see_check(): 
        if checkvar.get() == 1: 
           
                file = askopenfile(mode='r',filetypes= [('MP3 Files', '*.mp3')])
                if file is not None: 
                    content = file.read()
                    mixer.music.load(content)
                    mixer.music.play()

How I get rid of this ? Thanks in advance :)

EDIT : Not working with mixer the sound does not play

EDIT 2 : It just display x00\x00\x00\x00\x00\x00\x00\x00$\x05\xc0\x00\x00\x00\x00\x00\x00\x11\x83B\xb6\x10\xe0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfb\x10D\x00\x0f\xf0\x00\x00i\x00\x00\x00\x08\x00\x00\x0f\xf0\x00\x00\x01\x00\x00\x01\xfe\x00\x00\x00 \x00\x00?\xc0\x00\x00\x04\xff\xff\xfa\x98\x95\xa8\x9f\xff\xfc\xeb\x1cD\xfcn<\x18t\x and more even if I want to play the content not printing it

1 Answers

You need to pass the filename or the open file object (file for your case) instead of the content of the file.

Also I would suggest to use askopenfilename() instead of askopenfile():

def see_check():
    if checkvar.get() == 1:
        # use askopenfilename() instead
        filename = askopenfilename(filetypes= [('MP3 Files', '*.mp3')])
        if filename is not None:
            # pass the selected filename to mixer.music.load()
            mixer.music.load(filename)
            mixer.music.play()
Related