android上的qt5不用QAudioRecorder记录PCM wav文件

时间:2018-02-08 08:22:34

标签: android qt5 pyqt5 qtmultimedia qaudiorecorder

我在pyqt5上编写代码,将PCM编解码器中的音频文件录制为wav格式。它在PC上很好,但在android上它总是用amr编解码器写入mp4文件。我该如何解决?

这是代码

recorder=QAudioRecorder(w)

audioSettings=QAudioEncoderSettings()
audioSettings.setCodec("audio/PCM")
audioSettings.setSampleRate(16000)
recorder.setAudioSettings(audioSettings);

recorder.setContainerFormat("wav");
recorder.setOutputLocation(QUrl.fromLocalFile("/sdcard/test"))

(Pyqt5从piproid 2应用程序安装在Android上,qt5安装在Ministro II应用程序中)

1 个答案:

答案 0 :(得分:0)

不幸的是,我没有用QAudioRecorder解决问题。我找到了用QAudioInput编写无头signed-int 16bit 16khz little endian PCM的方法:

#!/usr/bin/env python2
from PyQt5.QtCore import QFile
from PyQt5.QtMultimedia import QAudioInput,QAudioFormat,QAudio
import sys
import time
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton

format=QAudioFormat()
format.setSampleRate(16000);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm")

format.setByteOrder(QAudioFormat.LittleEndian)
format.setSampleType(QAudioFormat.SignedInt)

audio=QAudioInput(format)

def onBtn():
    if audio.state()==QAudio.StoppedState:
        audio.start(destFile)
        print "started"
    else:
        audio.stop()
        print "stopped"
pass

app = QApplication(sys.argv)
w = QWidget()
btnRec = QPushButton('Rec|stop', w)
btnRec.clicked.connect(onBtn)
w.show()
app.exec_()

destFile.close()