带有PyQt5的python3.x Mp3播放器

时间:2018-03-17 22:11:28

标签: python python-3.x pygame pyqt5

我尝试为我的学校项目制作mp3混音器程序。我以为我们可以找到混合两首歌的新歌

我在玩普顿时遇到麻烦。每当我点击它时,都会发生错误

import sys,os
from PyQt5.QtWidgets import QWidget,QApplication,QPushButton,QVBoxLayout,QFileDialog,QHBoxLayout
from pygame import mixer
class Window(QWidget):
    def __init__(self):

        super().__init__()

        self.init_ui()
    def init_ui(self):
        self.song1 = QPushButton("song1")
        self.song2 = QPushButton("song2")
        self.play_it = QPushButton("Play")
        h_box = QHBoxLayout()
        h_box.addWidget(self.song1)
        h_box.addWidget(self.song2)
        h_box.addWidget(self.play_it)
        v_box = QVBoxLayout()
        v_box.addLayout(h_box)
        self.setLayout(v_box)
        self.setWindowTitle("Song Mixer 1.0")
        self.song1.clicked.connect(self.song1_open)
        self.play_it.clicked.connect(self.play_the_songs)
        self.show()
    def song2_open(self):
        file_name = QFileDialog.getOpenFileName(self,"Open",os.getenv("HOME"))
        self.data2 = file_name[0]

    def song1_open(self):
        file_name = QFileDialog.getOpenFileName(self,"Open",os.getenv("HOME"))
        self.data1 = file_name[0]

    def play_the_songs(self):
        mixer.init()

        s1 =mixer.Sound(self.data1)
        s2 =mixer.Sound(self.data2)
        s1.play()

app = QApplication(sys.argv)

pencere = Window()


sys.exit(app.exec_())

我很感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

试一试:

!!!调音台还有一个特殊的流媒体通道。这是用于音乐播放,可通过pygame.mixer.musicpygame模块访问,以控制流式音频模块。

import sys,os
from PyQt5.QtWidgets import (QWidget,QApplication,QPushButton,
                             QVBoxLayout,QFileDialog,QHBoxLayout)
#from pygame import mixer                                         # ---
import pygame                                                     # +++
pygame.init()                                                     # +++

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.playsound = None                                     # +++
        self.pause     = None                                     # +++

        self.init_ui()

    def init_ui(self):
        self.song1 = QPushButton("LoadingSound")
        self.pause = QPushButton("Pause")
        self.play_it = QPushButton("Play")
        h_box = QHBoxLayout()
        h_box.addWidget(self.song1)
        h_box.addWidget(self.play_it)
        h_box.addWidget(self.pause)
        v_box = QVBoxLayout()
        v_box.addLayout(h_box)
        self.setLayout(v_box)
        self.setWindowTitle("Song Mixer 1.0")

        self.song1.clicked.connect(self.song1_open)
        self.pause.clicked.connect(self.pause_the_songs)            
        self.play_it.clicked.connect(self.play_the_songs)

        self.show()

    def pause_the_songs(self):
        if self.playsound is None:
            self.pause.setText("UnPause")
            self.playsound = "pause"
            pygame.mixer.music.pause()
        else:
            self.pause.setText("Pause")
            self.playsound = None  
            pygame.mixer.music.unpause()            

    def song1_open(self):
        file_name = QFileDialog.getOpenFileName(self,"Open",os.getenv("HOME"))
        self.data1 = file_name[0]

    def play_the_songs(self):                                     # +++
        self.playsound = pygame.mixer.init()        
        pygame.mixer.music.load(self.data1)
        pygame.mixer.music.play()


app = QApplication(sys.argv)
pencere = Window()
sys.exit(app.exec_())