1

I need a voice recorder as part of the program. Below is the test part for voice recorder

import os
import sys
from PyQt6.QtMultimedia import QMediaRecorder, QMediaCaptureSession, QAudioInput, QMediaFormat
from PyQt6.QtWidgets import QMainWindow, QApplication, QPushButton
from PyQt6 import QtCore


# Для тестирования
class MainForm(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.audio_recorder_init('files/pretest')
        self.button_start.clicked.connect(self.start_record)
        self.button_stop.clicked.connect(self.stop_record)

    def initUI(self):
        self.setGeometry(100, 100, 1000, 500)
        self.setFixedSize(self.width(), self.height())
        self.button_start = QPushButton('Start', self)
        self.button_stop = QPushButton('Stop', self)
        self.button_stop.move(0, 50)

    def audio_recorder_init(self, filename):
        mcs = QMediaCaptureSession(self)
        self.inp = QAudioInput(self)
        self.inp.setVolume(1.0)
        mcs.setAudioInput(self.inp)
        self.ardRecorder = QMediaRecorder(self)
        mcs.setRecorder(self.ardRecorder)
        self.ardRecorder.setOutputLocation(QtCore.QUrl.fromLocalFile(os.path.abspath(filename)))
        # media_format = QMediaFormat(QMediaFormat.FileFormat.MP3)
        # media_format.setAudioCodec(QMediaFormat.AudioCodec.MP3)
        # self.ardRecorder.setMediaFormat(media_format)
        self.ardRecorder.setQuality(QMediaRecorder.Quality.LowQuality)
        self.ardRecorder.setEncodingMode(QMediaRecorder.EncodingMode.ConstantQualityEncoding)
        self.ardRecorder.setAudioChannelCount(1)
        self.ardRecorder.setAudioSampleRate(-1)

    def start_record(self):
        self.ardRecorder.record()

    def stop_record(self):
        self.ardRecorder.stop()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MF = MainForm()
    MF.show()
    sys.exit(app.exec())

The program works and records sound in .m4a format. But I want to get .mp3 or .wav files.

If you uncomment the lines

        # media_format = QMediaFormat(QMediaFormat.FileFormat.MP3)
        # media_format.setAudioCodec(QMediaFormat.AudioCodec.MP3)
        # self.ardRecorder.setMediaFormat(media_format)

then on startup it outputs to the console [mp3_mf @ 0000024738C00AC0] MFT name: 'MP3 Encoder ACM Wrapper MFT'. This creates a file with the .m4a extension, but the player refuses to play it (but the sound is still recorded, I tried to convert this file through an online converter to mp3 and I managed to play it). If you change the file format and audio codec from MP3 to Wave, then the program generally hangs. What could be the problem? And how can you solve it?

JIST
  • 1,139
  • 2
  • 8
  • 30
  • Did you check if you can actually use the selected file format and audio codec with `supportedFileFormats()` and `supportedAudioCodecs()`? – musicamante Jul 20 '23 at 17:50
  • Note that the Qt Multimedia API is still evolving (it was practically rewritten from scratch starting with Qt6), has received lots of changes and updates in the meantime (for instance, since Qt 6.5 it always uses the ffmpeg backend by default on all platforms, instead of the native backend), and may still change in the upcoming versions. Which means that it's not yet stable and robust, and there can be bugs that may disappear or appear again (regressions) across versions. – musicamante Jul 20 '23 at 17:59
  • @musicamante Thank you for your reply! supportedFileFormats(): [, , , , , , , , , , ] supportedAudioCodecs(): [, , , , , , ] I think you are right: the formats I need are still in development. But it works for WMA. – Владимир Jul 21 '23 at 08:27
  • The fact that a format is not listed doesn't mean that it is "in development", it may just be not available for your platform/backend. Check the [QtMultimedia platform and backend notes](https://doc.qt.io/qt-6/qtmultimedia-index.html#target-platform-and-backend-notes). – musicamante Jul 21 '23 at 16:28

0 Answers0