• <tfoot id='iuoy3'></tfoot>
      <i id='iuoy3'><tr id='iuoy3'><dt id='iuoy3'><q id='iuoy3'><span id='iuoy3'><b id='iuoy3'><form id='iuoy3'><ins id='iuoy3'></ins><ul id='iuoy3'></ul><sub id='iuoy3'></sub></form><legend id='iuoy3'></legend><bdo id='iuoy3'><pre id='iuoy3'><center id='iuoy3'></center></pre></bdo></b><th id='iuoy3'></th></span></q></dt></tr></i><div id='iuoy3'><tfoot id='iuoy3'></tfoot><dl id='iuoy3'><fieldset id='iuoy3'></fieldset></dl></div>

      <small id='iuoy3'></small><noframes id='iuoy3'>

    1. <legend id='iuoy3'><style id='iuoy3'><dir id='iuoy3'><q id='iuoy3'></q></dir></style></legend>
        <bdo id='iuoy3'></bdo><ul id='iuoy3'></ul>
      1. 如何使用 Pyqt5 QtMultimedia 播放声音?

        时间:2023-08-04

            <legend id='Biozz'><style id='Biozz'><dir id='Biozz'><q id='Biozz'></q></dir></style></legend>

              <tfoot id='Biozz'></tfoot>

              <small id='Biozz'></small><noframes id='Biozz'>

                <tbody id='Biozz'></tbody>
                • <bdo id='Biozz'></bdo><ul id='Biozz'></ul>
                  <i id='Biozz'><tr id='Biozz'><dt id='Biozz'><q id='Biozz'><span id='Biozz'><b id='Biozz'><form id='Biozz'><ins id='Biozz'></ins><ul id='Biozz'></ul><sub id='Biozz'></sub></form><legend id='Biozz'></legend><bdo id='Biozz'><pre id='Biozz'><center id='Biozz'></center></pre></bdo></b><th id='Biozz'></th></span></q></dt></tr></i><div id='Biozz'><tfoot id='Biozz'></tfoot><dl id='Biozz'><fieldset id='Biozz'></fieldset></dl></div>
                  本文介绍了如何使用 Pyqt5 QtMultimedia 播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  def play_tts(self,file_path):
                     file = open(file_path)
                     mixer.init()
                     mixer.music.load(file)
                     mixer.music.play()
                     while mixer.music.get_busy():
                         time.sleep(0.03)
                         if window.ttsIs:
                             break
                     mixer.stop()
                     mixer.quit()
                     file.close()
                     remove(file_path)
                  

                  如何用 QtMultimedia 编写上述代码?

                  How do I write the above code with QtMultimedia?

                  你能举个例子吗?

                  推荐答案

                  如果文件是 .wav 则只需使用 QSound:

                  If the file is a .wav then just use QSound:

                  import os
                  import sys
                  
                  from PyQt5 import QtCore, QtMultimedia
                  
                  CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
                  
                  
                  def main():
                      filename = os.path.join(CURRENT_DIR, "beal.wav")
                  
                      app = QtCore.QCoreApplication(sys.argv)
                  
                      QtMultimedia.QSound.play(filename)
                  
                  
                      # end in 5 seconds:
                      QtCore.QTimer.singleShot(5 * 1000, app.quit)
                  
                      sys.exit(app.exec_())
                  
                  
                  if __name__ == "__main__":
                      main()
                  

                  如果你想播放更多格式,那么你应该使用 QMediaPlayer:

                  If you want to play more formats then you should use QMediaPlayer:

                  import os
                  import sys
                  
                  from PyQt5 import QtCore, QtMultimedia
                  
                  CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
                  
                  
                  def main():
                      filename = os.path.join(CURRENT_DIR, "sound.mp3")
                  
                      app = QtCore.QCoreApplication(sys.argv)
                  
                      player = QtMultimedia.QMediaPlayer()
                  
                      def handle_state_changed(state):
                          if state == QtMultimedia.QMediaPlayer.PlayingState:
                              print("started")
                          elif state == QtMultimedia.QMediaPlayer.StoppedState:
                              print("finished")
                              QtCore.QCoreApplication.quit()
                  
                      player.stateChanged.connect(handle_state_changed)
                  
                      url = QtCore.QUrl.fromLocalFile(filename)
                      player.setMedia(QtMultimedia.QMediaContent(url))
                      player.play()
                  
                      sys.exit(app.exec_())
                  
                  
                  if __name__ == "__main__":
                      main()
                  

                  这篇关于如何使用 Pyqt5 QtMultimedia 播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PyQt QLineEdit 与 QValidator 下一篇:Python 3.5 + PyQt5 到独立 exe

                  相关文章

                  最新文章

                  <small id='ikWFu'></small><noframes id='ikWFu'>

                • <legend id='ikWFu'><style id='ikWFu'><dir id='ikWFu'><q id='ikWFu'></q></dir></style></legend>

                      <bdo id='ikWFu'></bdo><ul id='ikWFu'></ul>
                  1. <tfoot id='ikWFu'></tfoot>

                      <i id='ikWFu'><tr id='ikWFu'><dt id='ikWFu'><q id='ikWFu'><span id='ikWFu'><b id='ikWFu'><form id='ikWFu'><ins id='ikWFu'></ins><ul id='ikWFu'></ul><sub id='ikWFu'></sub></form><legend id='ikWFu'></legend><bdo id='ikWFu'><pre id='ikWFu'><center id='ikWFu'></center></pre></bdo></b><th id='ikWFu'></th></span></q></dt></tr></i><div id='ikWFu'><tfoot id='ikWFu'></tfoot><dl id='ikWFu'><fieldset id='ikWFu'></fieldset></dl></div>