python3.x Mp3 播放器与 PyQt5
Posted
技术标签:
【中文标题】python3.x Mp3 播放器与 PyQt5【英文标题】:python3.x Mp3 Player with PyQt5 【发布时间】:2018-03-17 22:11:27 【问题描述】:我尝试为我的学校项目制作 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_())
感谢您的帮助!
【问题讨论】:
错误是秘密吗?如果没有,为什么不将其复制并粘贴到问题中? @PatrickArtner 它没有像 Windows 错误那样响应 对于这些情况,像这样封装你的代码:Try: your code Except Exception as e: print(e)
(sn-p 2 at ***.com/questions/18982610/…) 它会打印你失败的地方。
【参考方案1】:
试试看:
!!!混音器还有一个特殊的流媒体频道。这用于音乐播放,可通过 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_())
【讨论】:
如果我可以同时打开更多歌曲但使用不同的按钮,这可能会发生以上是关于python3.x Mp3 播放器与 PyQt5的主要内容,如果未能解决你的问题,请参考以下文章
QMediaPlayer setPlaybackRate > 1 导致高音音频