我的软件有问题.在我的钢琴软件中,如果我一直按下键盘上的一个键,那么它会使该特定键重复多次相同的音调.但实际上我需要一个单一的音调,直到该特定键的释放.我提供了一部分代码来检测keyPress事件并调用相应的方法.那么我应该对我的代码进行哪些更改?
def keyPressEvent(self, event):
if type(event) == QtGui.QKeyEvent and event.key() == QtCore.Qt.Key_A :
self.Playnote('/home/hemanth/hemanth/Piano/C.mp3')
self.ui.pushButton.animateClick(100)
if type(event) == QtGui.QKeyEvent and event.key() == QtCore.Qt.Key_S:
self.Playnote('/home/hemanth/hemanth/Piano/D.mp3')
self.ui.pushButton_2.animateClick(100)
Run Code Online (Sandbox Code Playgroud) 我在PyQt中开发了一个播放声音的软件.我正在使用Phonon Library播放声音,但它有一些滞后.所以如何在不使用Phonon Library的情况下在PyQt中播放声音文件.
这就是我目前使用Phonon的方式:
def Playnote(self,note_id):
global note
note = note_id
self.PlayThread = PlayThread()
self.PlayThread.start()
class PlayThread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
global note
self.m_media = Phonon.MediaObject(self)
audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)
Phonon.createPath(self.m_media, audioOutput)
self.m_media.setCurrentSource(Phonon.MediaSource(Phonon.MediaSource(note)))
self.m_media.play()
Run Code Online (Sandbox Code Playgroud)
现在滞后减少了.但问题是我在短时间内按下两个或更多键,这是新音符开销并停止前一个音符.我需要播放前一个音符直到它结束.
class PlayThread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
self.m_media = Phonon.MediaObject(self)
self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)
Phonon.createPath(self.m_media, self.audioOutput)
def __del__(self):
self.wait()
def play(self, note):
self.m_media.setCurrentSource(Phonon.MediaSource(Phonon.MediaSource(note)))
self.m_media.play()
def run(self):pass
Run Code Online (Sandbox Code Playgroud)