Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How to import QtMultimedia module in ubuntu 16.04 in a python program
Forum Updated to NodeBB v4.3 + New Features

How to import QtMultimedia module in ubuntu 16.04 in a python program

Scheduled Pinned Locked Moved Solved Installation and Deployment
7 Posts 2 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sahil713
    wrote on last edited by sahil713
    #1

    I encountered this error while compiling one of the examples given in the qt websites.
    I have updated pyqt4,tried installing all the qt libraries from synaptic package manager, installed gstreamer 1.0.

    ![alt text](0_1521313332682_Screenshot.png image url)```

    The code:
    //

    from math import pi, sin
    import struct, sys

    from PyQt4.QtCore import QBuffer, QByteArray, QIODevice, Qt
    from PyQt4.QtGui import QApplication, QFormLayout, QLineEdit, QHBoxLayout,
    QPushButton, QSlider, QVBoxLayout, QWidget
    from PyQt4.QtMultimedia import QAudio, QAudioDeviceInfo, QAudioFormat, QAudioOutput

    class Window(QWidget):

       def __init__(self, parent = None):
    
           QWidget.__init__(self, parent)
    
           format = QAudioFormat()
           format.setChannels(1)
           format.setFrequency(22050)
           format.setSampleSize(16)
           format.setCodec("audio/pcm")
           format.setByteOrder(QAudioFormat.LittleEndian)
           format.setSampleType(QAudioFormat.SignedInt)
           self.output = QAudioOutput(format, self)
    
           self.frequency = 440
           self.volume = 0
           self.buffer = QBuffer()
           self.data = QByteArray()
    
           self.deviceLineEdit = QLineEdit()
           self.deviceLineEdit.setReadOnly(True)
           self.deviceLineEdit.setText(QAudioDeviceInfo.defaultOutputDevice().deviceName())
    
           self.pitchSlider = QSlider(Qt.Horizontal)
           self.pitchSlider.setMaximum(100)
           self.volumeSlider = QSlider(Qt.Horizontal)
           self.volumeSlider.setMaximum(32767)
           self.volumeSlider.setPageStep(1024)
    
           self.playButton = QPushButton(self.tr("&Play"))
    
           self.pitchSlider.valueChanged.connect(self.changeFrequency)
           self.volumeSlider.valueChanged.connect(self.changeVolume)
           self.playButton.clicked.connect(self.play)
    
           formLayout = QFormLayout()
           formLayout.addRow(self.tr("Device:"), self.deviceLineEdit)
           formLayout.addRow(self.tr("P&itch:"), self.pitchSlider)
           formLayout.addRow(self.tr("&Volume:"), self.volumeSlider)
    
           buttonLayout = QVBoxLayout()
           buttonLayout.addWidget(self.playButton)
           buttonLayout.addStretch()
    
           horizontalLayout = QHBoxLayout(self)
           horizontalLayout.addLayout(formLayout)
           horizontalLayout.addLayout(buttonLayout)
    
       def changeFrequency(self, value):
    
           self.frequency = 440 + (value * 2)
    
       def play(self):
    
           if self.output.state() == QAudio.ActiveState:
               self.output.stop()
    
           if self.buffer.isOpen():
               self.buffer.close()
    
           self.createData()
    
           self.buffer.setData(self.data)
           self.buffer.open(QIODevice.ReadOnly)
           self.buffer.seek(0)
    
           self.output.start(self.buffer)
    
       def changeVolume(self, value):
    
           self.volume = value
    
       def createData(self):
    
           # Create 2 seconds of data with 22050 samples per second, each sample
           # being 16 bits (2 bytes).
    
           self.data.clear()
           for i in xrange(2 * 22050):
               t = i / 22050.0
               value = int(self.volume * sin(2 * pi * self.frequency * t))
               self.data.append(struct.pack("<h", value))
    

    if name == "main":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      That should more likely be PyQt5.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        That should more likely be PyQt5.

        S Offline
        S Offline
        sahil713
        wrote on last edited by sahil713
        #3

        @SGaist I am new to pyqt and qt programming. This example was given in a python website. Here's the link:

        Playing a sound with QtMultimedia

        However i tried using Pyqt5 but there is this error.

        0_1521358962446_Screenshot -2new.png

        Edit:

        I tried solving this error.

        ImportError: cannot import name QApplication
        

        QApplication is in QtWidgets module so it should be

        from PyQt5.QtWidgets import QApplication
        

        But now I get this error

        AttributeError: 'QAudioFormat' object has no attribute 'setChannels'
        
        S 1 Reply Last reply
        0
        • S sahil713

          @SGaist I am new to pyqt and qt programming. This example was given in a python website. Here's the link:

          Playing a sound with QtMultimedia

          However i tried using Pyqt5 but there is this error.

          0_1521358962446_Screenshot -2new.png

          Edit:

          I tried solving this error.

          ImportError: cannot import name QApplication
          

          QApplication is in QtWidgets module so it should be

          from PyQt5.QtWidgets import QApplication
          

          But now I get this error

          AttributeError: 'QAudioFormat' object has no attribute 'setChannels'
          
          S Offline
          S Offline
          sahil713
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            It's setChannelCount.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            0
            • SGaistS SGaist

              It's setChannelCount.

              S Offline
              S Offline
              sahil713
              wrote on last edited by
              #6

              @SGaist Thank you very much.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You're welcome !

                Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved