Pyglet library player.play() sometimes does not play music

Asked

Viewed 19 times

-1

** If someone is going to test the code, it is possible that an error:** raise RegexMatchError( pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple. To solve: https://github.com/xioren/pytube/commit/35a653681f595bf975b080772344e32c8a5edf08.

My program read all the contents of a folder that the user chose and stores in a list only those that ends in ". mp3", then I load the songs with

for song in self.songs:
    music = pyglet.media.load(song)
    self.player.queue(music) 

I created a button with Tkinter that when pressed, it calls a function that runs self.player.next_source() and then self.player.play(), but the next song only starts playing IF SHE WANTS (with some tests, apparently, one play, the next one doesn’t. There it is, touch, touch, touch, touch, touch, touch. However, if the song you are playing ends, the next one starts playing when the "next" button is pressed. Is that really how it works or am I doing something wrong? I even tried to pause the one that’s playing and move on to the next one, but then he just plays the one that’s been paused. I looked through the library documentation but I couldn’t find anything to help me.

Ah, my code is missing the pyglet.app.run() because I can’t get him and Tkinter to work together (the window created with Tkinter does not open with the pyglet.app.run() in the code).

Ah, if I’m not mistaken, to make a library work I had to install Avbin and I had to download some "compilation (language) tools C.

Another thing, when pressing the "select download folder" button, even if you previously selected a folder, YOU WILL HAVE TO SELECT AGAIN!

import pydub
from tkinter import filedialog
import tkinter
import pytube
import os
import re
import pyglet


class MyFirstAPP():
    def __init__(self):
        self.directory = 0
        self.path = ''
        self.playerIsExists = False
        self.player = ''
        self.songs = []
        self.music_index = 0
        self.name_music = ''
        self.restart = 0
        self.title = ''
        self.source = ''
        self.playlist = 0
        self.Window()

    def path_download(self):
        self.path = filedialog.askdirectory(
            title='Selecione uma pasta para salvar as musicas'
        )
        if len(self.path) > 0:
            self.directory += 1
        os.chdir(self.path)

    def download_music(self):
        while self.directory == 0:
            self.path = filedialog.askdirectory(
                title='Selecione uma pasta para salvar as musicas')
            if len(self.path) > 0:
                self.directory += 1
                os.chdir(self.path)
        print(self.directory)
        url_str = self.url_text.get()
        music = pytube.YouTube(url_str)
        music_title = re.sub(r'\W+', ' ', music.title)

        music.streams.get_audio_only().download(
            filename=str(music_title) + '.mp4'
        )

        pydub.AudioSegment.from_file(str(music_title) + '.mp4').export(
            str(music_title) + '.mp3', format='mp3')

        self.dir = os.listdir(self.path)
        for file in self.dir:
            if file == f'{music_title}' + '.mp4':
                os.remove(file)

    def prox(self):
        self.player.next_source()
        self.music_index += 1
        if self.music_index + 1 == len(self.songs) + 1:
            print('player deletado')
            self.music_index = 0
            self.player.delete()
            self.playerIsExists = False
            self.name_music["text"] = str(
                self.songs[self.music_index]).replace('.mp3', '')
            self.songs = []
            self.restart += 1
        if self.music_index >= 1 and self.playerIsExists:
            self.name_music["text"] = str(
                self.songs[self.music_index]).replace('.mp3', '')
        self.play_mp3()

    def play_mp3(self):
        print(self.playerIsExists)
        if not self.playerIsExists:
            self.player = pyglet.media.Player()
            self.playerIsExists = True
            self.dir = os.listdir(self.path)
            for file in self.dir:
                if '.mp3' in file:
                    self.songs.append(file)
            for song in self.songs:
                music = pyglet.media.load(song)
                self.player.queue(music)
            print(self.songs)
        print(self.songs[self.music_index])
        if self.music_index == 0 and self.restart == 0:
            self.name_music = tkinter.Label(
                self.window, text=str(
                                        f'{self.songs[0]}').replace(
                                        '.mp3', ''))
            self.name_music.place(x=0, y=150)
            self.name_music.pack(side='left', fill='x')
        self.player.play()

        self.botton_next_source = tkinter.Button(
            self.window, width=10, text='Proxima',
            command=self.prox, fg='black')
        self.botton_next_source.place(x=0, y=70)

        def pause():
            self.player.pause()

        botton_pause = tkinter.Button(self.window, width=10, text='Pausar',
                                      command=pause, fg='blue')
        botton_pause.place(x=2, y=40)

    def quit(self):
        self.window.destroy()

    def Window(self):
        self.window = tkinter.Tk()
        self.window.title('Youtube Music Downloader')
        self.window.geometry('600x420+0+0')
        self.window.resizable(width=False, height=False)
        self.window.iconbitmap("icon.ico")
        self.info_link = tkinter.Label(
            self.window, text='Coloque o link do musica no campo abaixo')
        self.info_link.pack(side='top', fill='both', expand=1)
        self.info_link.place(x=0, y=0)
        self.url_text = tkinter.Entry(self.window, width=100)
        self.url_text.place(x=0, y=20)

        self.botton_download = tkinter.Button(self.window, width=10,
                                              text='BAIXAR', fg='blue',
                                              command=self.download_music)
        self.botton_download.place(x=82, y=40)

        self.botton_quit = tkinter.Button(self.window, width=10, text='FECHAR',
                                          command=self.quit, fg='red')
        self.botton_quit.place(x=162, y=40)

        self.botton_play = tkinter.Button(self.window, width=10, text='PLAY',
                                          command=self.play_mp3, fg='green')
        self.botton_play.place(x=242, y=40)

        self.botton_path = tkinter.Button(self.window, width=30,
                                          text='Selecionar pasta de download',
                                          command=self.path_download,
                                          fg='black')
        self.botton_path.place(x=322, y=40)
        self.window.mainloop()


if __name__ == '__main__':
    MyFirstAPP()

  • The indentation of this python code is correct?

  • Please clarify your specific problem or provide Additional Details to Highlight Exactly what you need. As it’s Currently Written, it’s hard to Tell Exactly what you’re asking.

  • @Danizavtz believe so. I will update the post and put the whole code.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.