如何通过有序,随机和重放来播放歌曲?

我想制作三种可以调节歌曲的模型。
您可以看到on_finish()方法,这是我写的关于播放模式的方法。
这三种模式是ordered,randomreplay
但我得到了一些错误,歌曲不能遵循这些模式的发挥。
如何通过有序,随机和重放来播放歌曲?

例如,
歌曲列表:ABCDEFG,而现在是打
当我点击下令,将在第一播放下一首歌曲(B),然后播放接下来的第三首歌曲(E)。
但它应该首先播放歌曲B,然后播放歌曲C,而不是歌曲E.
而且随机发生的情况也是如此。
然后我试图打印(路径),我发现的代码将立即三次,最后晚上的时间.......

我不知道问题出在哪里执行,然后。

这是我的代码music_1.py

#!/usr/bin/python 

# -*- coding: utf-8 -*-

# music_1.py

import sys

import random

from PyQt4 import QtGui, QtCore

from PyQt4 import phonon

from music_ui import Ui_Form

warmpath_list=[r"C:\Users\Jacky\Desktop\music_list\warm\song1.mp3",

r"C:\Users\Jacky\Desktop\music_list\warm\song2.mp3",

r"C:\Users\Jacky\Desktop\music_list\warm\song3.wma",

r"C:\Users\Jacky\Desktop\music_list\warm\song4.mp3",

r"C:\Users\Jacky\Desktop\music_list\warm\song5.mp3",

r"C:\Users\Jacky\Desktop\music_list\warm\song6.wma",

funnypath_list=[r"C:\Users\Jacky\Desktop\music_list\funny\songa.wma",

r"C:\Users\Jacky\Desktop\music_list\funny\songb.wma",

r"C:\Users\Jacky\Desktop\music_list\funny\songc.wma ",

r"C:\Users\Jacky\Desktop\music_list\funny\songd.wma"]

class Music(QtGui.QDialog):

def __init__(self, parent=None):

QtGui.QDialog.__init__(self, parent)

self.setWindowTitle('Player')

self.ui = Ui_Form()

self.ui.setupUi(self)

self.mediaObject = phonon.Phonon.MediaObject(self)

self.audioOutput = phonon.Phonon.AudioOutput(phonon.Phonon.MusicCategory, self)

phonon.Phonon.createPath(self.mediaObject, self.audioOutput)

self.ui.volumeSlider.setAudioOutput(self.audioOutput)

self.ui.seekSlider.setMediaObject(self.mediaObject)

self.mediaObject.tick.connect(self.tick)

self.timeLcd = QtGui.QLCDNumber(self)

self.timeLcd.display('00:00')

self.timeLcd.setGeometry(480, 330, 100, 50)

self.ui.listWidget.insertItem(0,"warm")

self.ui.listWidget.insertItem(1,"funny")

self.ui.listWidget_3.insertItem(0,"song1")

self.ui.listWidget_3.insertItem(1,"song2")

self.ui.listWidget_3.insertItem(2,"song3")

self.ui.listWidget_3.insertItem(3,"song4")

self.ui.listWidget_3.insertItem(4,"song5")

self.ui.listWidget_3.insertItem(5,"song6")

self.ui.listWidget_2.insertItem(0,"songa")

self.ui.listWidget_2.insertItem(1,"songb")

self.ui.listWidget_2.insertItem(2,"songc")

self.ui.listWidget_2.insertItem(3,"songd")

self.connect(self.ui.listWidget,QtCore.SIGNAL("currentRowChanged(int)"),self.ui.stackedWidget,QtCore.SLOT("setCurrentIndex(int)"))

self.connect(self.ui.listWidget_2,QtCore.SIGNAL("currentRowChanged(int)"),self.openaudio)

self.connect(self.ui.listWidget_3,QtCore.SIGNAL("currentRowChanged(int)"),self.openaudio)

self.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'),self.mediaObject.pause)

self.connect(self.ui.pushButton_4, QtCore.SIGNAL('clicked()'),self.mediaObject.play)

self.connect(self.ui.pushButton_5, QtCore.SIGNAL('clicked()'),self.mediaObject.stop)

self.connect(self.ui.pushButton_2, QtCore.SIGNAL('clicked()'),self.m_previous)

self.connect(self.ui.pushButton_3, QtCore.SIGNAL('clicked()'),self.m_next)

popmenu = QtGui.QMenu(self.ui.pushButton_6)

group = QtGui.QActionGroup(self.ui.pushButton_6)

group.setExclusive(True)

for i in range(1, 4):

if i==1:

action = group.addAction('oordered')

action.setCheckable(True)

#action.setChecked(True)

if i==2:

action = group.addAction('replay')

action.setCheckable(True)

if i==3:

action = group.addAction('random')

action.setCheckable(True)

popmenu.addAction(action)

self.ui.pushButton_6.setMenu(popmenu)

group.triggered.connect(self.sortedEvent) # sortedEvent()

def tick(self, time):

displayTime = QtCore.QTime(0, (time/60000) % 60, (time/1000) % 60)

self.timeLcd.display(displayTime.toString('mm:ss'))

def openaudio(self,path):

self.connect(self.ui.listWidget_2,QtCore.SIGNAL('currentTextChanged(QString)'),self.ui.label_4,QtCore.SLOT('setText(QString)'))

self.connect(self.ui.listWidget_3,QtCore.SIGNAL('currentTextChanged(QString)'),self.ui.label_4,QtCore.SLOT('setText(QString)'))

if self.ui.stackedWidget.currentIndex()==0:

index1=self.ui.listWidget_3.currentRow()

path=warmpath_list[index1]

if self.ui.stackedWidget.currentIndex()==1:

index2=self.ui.listWidget_2.currentRow()

path=funnypath_list[index2]

self.mediaObject.setCurrentSource(phonon.Phonon.MediaSource(path))

self.mediaObject.play()

self.mediaObject.finished.connect(self.on_finish)

def m_next(self): # to play the next song

if self.ui.stackedWidget.currentIndex()==0:

index1=self.ui.listWidget_3.currentRow()

if index1==len(warmpath_list)-1:

path=warmpath_list[0]

self.ui.listWidget_3.setCurrentRow(0)

else:

path=warmpath_list[index1+1]

self.ui.listWidget_3.setCurrentRow(index1+1)

print(path)

self.openaudio(path)

if self.ui.stackedWidget.currentIndex()==1:

index2=self.ui.listWidget_2.currentRow()

if index2==len(funnypath_list)-1:

path=funnypath_list[0]

self.ui.listWidget_2.setCurrentRow(0)

else:

path=funnypath_list[index2+1]

self.ui.listWidget_2.setCurrentRow(index2+1)

self.openaudio(path)

def m_previous(self): # to play the previous song

if self.ui.stackedWidget.currentIndex()==0:

index1=self.ui.listWidget_3.currentRow()

if index1==len(warmpath_list)-len(warmpath_list):

path=warmpath_list[len(warmpath_list)-1]

self.ui.listWidget_3.setCurrentRow(len(warmpath_list)-1)

else:

path=warmpath_list[index1-1]

self.ui.listWidget_3.setCurrentRow(index1-1)

print(path)

self.openaudio(path)

if self.ui.stackedWidget.currentIndex()==1:

index2=self.ui.listWidget_2.currentRow()

if index2==len(funnypath_list)-len(funnypath_list):

path=funnypath_list[len(funnypath_list)-1]

self.ui.listWidget_2.setCurrentRow(len(funnypath_list)-1)

else:

path=funnypath_list[index2-1]

self.ui.listWidget_2.setCurrentRow(index2-1)

print(path)

self.openaudio(path)

def sortedEvent(self,action):

self.whatToDoNext=action.text()

def on_finish(self):

if self.whatToDoNext=="ordered": # Order of play

if self.ui.stackedWidget.currentIndex()==0:

index1=self.ui.listWidget_3.currentRow()

if index1==len(warmpath_list)-1:

path=warmpath_list[0]

self.ui.listWidget_3.setCurrentRow(0)

else:

path=warmpath_list[index1+1]

self.ui.listWidget_3.setCurrentRow(index1+1)

print(path)

self.openaudio(path)

if self.ui.stackedWidget.currentIndex()==1:

index2=self.ui.listWidget_2.currentRow()

if index2==len(funnypath_list)-1:

path=funnypath_list[0]

self.ui.listWidget_2.setCurrentRow(0)

else:

path=funnypath_list[index2+1]

self.ui.listWidget_2.setCurrentRow(index2+1)

self.openaudio(path)

elif self.whatToDoNext=="random": # random of play

if self.ui.stackedWidget.currentIndex()==0:

path = random.choice(warmpath_list)

self.ui.listWidget_3.setCurrentRow(warmpath_list.index(path))

print(path)

self.openaudio(path)

if self.ui.stackedWidget.currentIndex()==1:

path = random.choice(funnypath_list)

self.ui.listWidget_2.setCurrentRow(funnypath_list.index(path))

self.openaudio(path)

else: # replay

self.mediaObject.stop()

self.mediaObject.play()

if __name__ == "__main__":

app = QtGui.QApplication(sys.argv)

myapp = Music()

myapp.show()

sys.exit(app.exec_())

这里是我的music_ui .py

from PyQt4 import QtCore, QtGui 

try:

_fromUtf8 = QtCore.QString.fromUtf8

except AttributeError:

def _fromUtf8(s):

return s

try:

_encoding = QtGui.QApplication.UnicodeUTF8

def _translate(context, text, disambig):

return QtGui.QApplication.translate(context, text, disambig, _encoding)

except AttributeError:

def _translate(context, text, disambig):

return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):

def setupUi(self, Form):

Form.setObjectName(_fromUtf8("Form"))

Form.resize(600, 400)

self.seekSlider = phonon.Phonon.SeekSlider(Form)

self.seekSlider.setGeometry(QtCore.QRect(230, 250, 361, 20))

self.seekSlider.setObjectName(_fromUtf8("seekSlider"))

self.volumeSlider = phonon.Phonon.VolumeSlider(Form)

self.volumeSlider.setGeometry(QtCore.QRect(20, 360, 151, 22))

self.volumeSlider.setObjectName(_fromUtf8("volumeSlider"))

self.pushButton = QtGui.QPushButton(Form)

self.pushButton.setGeometry(QtCore.QRect(420, 280, 40, 40))

font = QtGui.QFont()

font.setFamily(_fromUtf8("Agency FB"))

font.setBold(True)

font.setWeight(75)

self.pushButton.setFont(font)

self.pushButton.setObjectName(_fromUtf8("pushButton"))

self.pushButton_2 = QtGui.QPushButton(Form)

self.pushButton_2.setGeometry(QtCore.QRect(300, 280, 40, 40))

font = QtGui.QFont()

font.setFamily(_fromUtf8("Agency FB"))

font.setPointSize(18)

font.setBold(True)

font.setWeight(75)

self.pushButton_2.setFont(font)

self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))

self.pushButton_3 = QtGui.QPushButton(Form)

self.pushButton_3.setGeometry(QtCore.QRect(540, 280, 40, 40))

font = QtGui.QFont()

font.setFamily(_fromUtf8("Agency FB"))

font.setPointSize(18)

font.setBold(True)

font.setWeight(75)

self.pushButton_3.setFont(font)

self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))

self.pushButton_4 = QtGui.QPushButton(Form)

self.pushButton_4.setGeometry(QtCore.QRect(480, 280, 40, 40))

font = QtGui.QFont()

font.setFamily(_fromUtf8("Agency FB"))

font.setPointSize(10)

font.setBold(True)

font.setWeight(75)

self.pushButton_4.setFont(font)

self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))

self.pushButton_5 = QtGui.QPushButton(Form)

self.pushButton_5.setGeometry(QtCore.QRect(360, 280, 40, 40))

font = QtGui.QFont()

font.setFamily(_fromUtf8("Agency FB"))

font.setPointSize(9)

font.setBold(True)

font.setWeight(75)

self.pushButton_5.setFont(font)

self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))

self.label_3 = QtGui.QLabel(Form)

self.label_3.setGeometry(QtCore.QRect(490, 30, 81, 71))

font = QtGui.QFont()

font.setFamily(_fromUtf8("Agency FB"))

font.setPointSize(16)

self.label_3.setFont(font)

self.label_3.setFrameShape(QtGui.QFrame.StyledPanel)

self.label_3.setTextFormat(QtCore.Qt.AutoText)

self.label_3.setAlignment(QtCore.Qt.AlignCenter)

self.label_3.setObjectName(_fromUtf8("label_3"))

self.label_2 = QtGui.QLabel(Form)

self.label_2.setGeometry(QtCore.QRect(230, 60, 161, 61))

font = QtGui.QFont()

font.setPointSize(12)

font.setBold(True)

font.setWeight(75)

self.label_2.setFont(font)

self.label_2.setObjectName(_fromUtf8("label_2"))

self.label_4 = QtGui.QLabel(Form)

self.label_4.setGeometry(QtCore.QRect(230, 130, 341, 31))

font = QtGui.QFont()

font.setPointSize(18)

font.setBold(True)

font.setWeight(75)

self.label_4.setFont(font)

self.label_4.setObjectName(_fromUtf8("label_4"))

self.groupBox = QtGui.QGroupBox(Form)

self.groupBox.setGeometry(QtCore.QRect(10, 30, 201, 321))

font = QtGui.QFont()

font.setPointSize(12)

font.setBold(True)

font.setWeight(75)

self.groupBox.setFont(font)

self.groupBox.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)

self.groupBox.setFlat(False)

self.groupBox.setCheckable(False)

self.groupBox.setObjectName(_fromUtf8("groupBox"))

self.listWidget = QtGui.QListWidget(self.groupBox)

self.listWidget.setGeometry(QtCore.QRect(10, 30, 171, 51))

self.listWidget.setFrameShape(QtGui.QFrame.NoFrame)

self.listWidget.setFrameShadow(QtGui.QFrame.Plain)

self.listWidget.setViewMode(QtGui.QListView.ListMode)

self.listWidget.setObjectName(_fromUtf8("listWidget"))

self.stackedWidget = QtGui.QStackedWidget(self.groupBox)

self.stackedWidget.setGeometry(QtCore.QRect(10, 90, 181, 221))

self.stackedWidget.setFrameShape(QtGui.QFrame.StyledPanel)

self.stackedWidget.setObjectName(_fromUtf8("stackedWidget"))

self.page = QtGui.QWidget()

self.page.setObjectName(_fromUtf8("page"))

self.listWidget_3 = QtGui.QListWidget(self.page)

self.listWidget_3.setGeometry(QtCore.QRect(0, 0, 181, 221))

self.listWidget_3.setFrameShape(QtGui.QFrame.NoFrame)

self.listWidget_3.setObjectName(_fromUtf8("listWidget_3"))

self.stackedWidget.addWidget(self.page)

self.page_2 = QtGui.QWidget()

self.page_2.setObjectName(_fromUtf8("page_2"))

self.listWidget_2 = QtGui.QListWidget(self.page_2)

self.listWidget_2.setGeometry(QtCore.QRect(0, 0, 181, 221))

self.listWidget_2.setFrameShape(QtGui.QFrame.NoFrame)

self.listWidget_2.setObjectName(_fromUtf8("listWidget_2"))

self.stackedWidget.addWidget(self.page_2)

self.pushButton_6 = QtGui.QPushButton(Form)

self.pushButton_6.setGeometry(QtCore.QRect(240, 280, 40, 40))

font = QtGui.QFont()

font.setFamily(_fromUtf8("Agency FB"))

font.setPointSize(10)

font.setBold(True)

font.setWeight(75)

self.pushButton_6.setFont(font)

self.pushButton_6.setObjectName(_fromUtf8("pushButton_6"))

self.retranslateUi(Form)

self.stackedWidget.setCurrentIndex(0)

QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):

Form.setWindowTitle(_translate("Form", "Form", None))

self.pushButton.setText(_translate("Form", "| |", None))

self.pushButton_2.setText(_translate("Form", "<", None))

self.pushButton_3.setText(_translate("Form", ">", None))

self.pushButton_4.setText(_translate("Form", "►", None))

self.pushButton_5.setText(_translate("Form", "■", None))

self.label_2.setText(_translate("Form", "Now is playing........", None))

self.label_4.setText(_translate("Form", "m u s i c", None))

self.groupBox.setTitle(_translate("Form", " music list ", None))

self.pushButton_6.setText(_translate("Form", "▦", None))

from PyQt4 import phonon

回答:

有没有在你的代码中的至少两个问题代码:

  • m_next(),你打电话self.ui.listWidget_3.setCurrentRow(0) 然后播放下一首歌与self.openaudio(path)

    的问题是,你以前的 listWidget行改变的信号连接到播放歌曲的功能:

    self.connect(self.ui.listWidget_3,QtCore.SIGNAL( “currentRowChanged(INT)”) ,self.openaudio)

    您打电话self.openaudio(path)两次,所以你得到一些 意外的行为。

  • 您连接到相同的插槽和信号的多个时间
    在功能openaudio(self,path),你连接到mediaObject.finishedself.on_finish。每次调用函数时,都会添加一个连接。
    如果您尝试下面的例子中,你会看到,槽与按钮点击叫了两声:

    button=QtGui.QPushButton("my button") 

    button.clicked.connect(on_click)

    button.clicked.connect(on_click)

    def on_click():

    print("slot called")

通常情况下,你只连接信号和槽在__init__功能,因为这个函数只被调用一次。

以上是 如何通过有序,随机和重放来播放歌曲? 的全部内容, 来源链接: utcz.com/qa/267294.html

回到顶部