0

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
    Non-text files should be opened in binary mode `rb`. Text files can be decoded, but even then you need to know what encoding to use. The default (found with `locale.getpreferredencoding(False)`) is not necessarily correct. But everything can be read in binary mode since no decoding occurs. – Mark Tolonen Sep 18 '22 at 00:18
  • 1
    What sort of useful information do you expect to get from printing the contents of a mp4 file? – Mark Ransom Sep 18 '22 at 00:30
  • My bad I forgot to remove the print because I tested it with python files – Wawayayayayaya Sep 18 '22 at 02:01
  • It was MP3 idk why I said MP4 – Wawayayayayaya Sep 18 '22 at 06:31
  • But still doesn't work even if i'm not doing something useless as just "printing the content" Idk why maybe the mode ? But you said that Non-text files should be opened in binary mode rb. – Wawayayayayaya Sep 18 '22 at 06:32
  • You need to pass the filename or `file` object instead of its content to `mixer.music.load()`. Study `pygame` document. – acw1668 Sep 18 '22 at 23:13

1 Answers1

0

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()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • got this : pygame.error: Failed loading libmpg123-0.dll: The specified module could not be found. – Wawayayayayaya Sep 19 '22 at 03:24
  • @Wawayayayayaya Try to reinstall `pygame`. The mentioned DLL should be included in `pygame` module. It works fine in my Windows 7 with Python 3.8.13 and pygame 2.1.2. – acw1668 Sep 19 '22 at 03:31
  • Ok I'll send you a feedback if it works – Wawayayayayaya Sep 19 '22 at 03:41
  • Tried this pip uninstall pygame then pip install pygame and still doesn't work. – Wawayayayayaya Sep 19 '22 at 03:51
  • Then I don't know why. Make sure you use the same Python environment that you run the `pip install pygame` to run your code. – acw1668 Sep 19 '22 at 03:55
  • you mean pip or pygame ? pygame is 2.1.2 and pip 22.2.2 and python 3.10 – Wawayayayayaya Sep 19 '22 at 04:10
  • I mean that you need to use *the same Python environment* that you use to install `pygame`. Did you get any errors when you run `python -m pygame.tests mixer_music`? If not, then the `pygame` module should be installed properly. – acw1668 Sep 19 '22 at 04:15
  • pygame.mixer.music.queue(filename) pygame.error: Failed loading libvorbisfile-3.dll: The specified module could not be found. ---------------------------------------------------------------------- Ran 19 tests in 1.561s FAILED (errors=10) – Wawayayayayaya Sep 19 '22 at 04:26
  • It may be caused by some dependent DLLs (other than those bundled by `pygame`) that your Windows does not have. I cannot help anymore. – acw1668 Sep 19 '22 at 04:29