QT_PC学习_多媒体入门_Avdio

来源:互联网 发布:react.js angular.js 编辑:程序博客网 时间:2024/05/17 23:32

本文中内容参见QT 官方参考文档:

1. 播放压缩音频文件

player = new QMediaPlayer;// ...player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));player->setVolume(50);player->play();

QMediaPlayer也可以播放在线的音频文件按Qurl("http:://example.com/flie1.mp3")

使用QMediaPlaylist(player), 播放时可以设置播放的序列, 播放时playerList,setCurrentIndex(1);设置播放的顺序

常见的信号处理:

positionChanged()

2.录音

audioRecorder = new QAudioRecorder;
QAudioEncoderSettings audioSettings;audioSettings.setCodec("audio/amr");audioSettings.setQuality(QMultimedia::HighQuality);audioRecorder->setEncodingSettings(audioSettings);audioRecorder->setOutputLocation(QUrl::fromLocalFile("test.amr"));audioRecorder->record();



3.音效

QSoundEffect effect;effect.setSource(QUrl::fromLocalFile("engine.wav"));effect.setLoopCount(QSoundEffect::Infinite);effect.setVolume(0.25f);effect.play();

4. 数据流控制

常用于和上层类QMediaPlayer QAudioRecorder QCamer数据交互,用于获取QAudioProbe处理的缓存数据。

audioProbe = new QAudioProbe(this);if (audioProbe->setSource(audioRecorder)) {<span style="white-space:pre"></span>// Probing succeeded, audioProbe->isValid() should be true.    connect(audioProbe, SIGNAL(audioBufferProbed(QAudioBuffer)),        this, SLOT(calculateLevel(QAudioBuffer)));}

5. 音频数据解码
QAudioFormat desiredFormat;desiredFormat.setChannelCount(2);desiredFormat.setCodec("audio/x-raw");desiredFormat.setSampleType(QAudioFormat::UnSignedInt);desiredFormat.setSampleRate(48000);desiredFormat.setSampleSize(16);
QAudioDecoder *decoder = new QAudioDecoder(this);decoder->setAudioFormat(desiredFormat);decoder->setSourceFilename("level1.mp3");connect(decoder, SIGNAL(bufferReady()), this, SLOT(readBuffer()));decoder->start();

注意bufferReady()信号发出之后,QIODevice进行读取,这里读取缓存 decoder->read();

      

0 0