Qt Multimedia offers a range of audio classes, covering both low and high level approaches to audio input, output and processing. In addition to traditional audio usage, the Qt Audio Engine QML types offer high level 3D positional audio for QML applications. See that documentation for more information.
对于播放不简单媒体或音频文件、未压缩音频,可以使用 QMediaPlayer C++ 类,或 音频 and MediaPlayer QML types. The QMediaPlayer 类和关联 QML 类型还能播放 video , if required. The compressed audio formats supported does depend on the operating system environment, and also what media plugins the user may have installed.
这里是如何使用 C++ 播放本地文件:
player = new QMediaPlayer; // ... player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); player->setVolume(50); player->play();
You can also put files (even remote URLs) into a playlist:
player = new QMediaPlayer; playlist = new QMediaPlaylist(player); playlist->addMedia(QUrl("http://example.com/myfile1.mp3")); playlist->addMedia(QUrl("http://example.com/myfile2.mp3")); // ... playlist->setCurrentIndex(1); player->play();
For recording audio to a file, the QAudioRecorder class allows you to compress audio data from an input device and record it.
audioRecorder = new QAudioRecorder; QAudioEncoderSettings audioSettings; audioSettings.setCodec("audio/amr"); audioSettings.setQuality(QMultimedia::HighQuality); audioRecorder->setEncodingSettings(audioSettings); audioRecorder->setOutputLocation(QUrl::fromLocalFile("test.amr")); audioRecorder->record();
In addition to the raw access to sound devices described above, the QSoundEffect 类 (和 SoundEffect QML type) offers a slightly higher level way to play sounds. These classes allow you to specify a WAV format file which can then be played with low latency when necessary. Both QSoundEffect and SoundEffect have essentially the same API.
You can adjust the number of loops a sound effect is played, as well as the volume (或 muting ) of the effect.
For older, Qt 4.x based applications QSound is also available. Applications are recommended to use QSoundEffect where possible.
The QAudioProbe class allows you to monitor audio data being played or recorded in the higher level classes like QMediaPlayer , QCamera and QAudioRecorder . After creating your high level class, you can simply set the source of the probe to your class, and receive audio buffers as they are processed. This is useful for several audio processing tasks, particularly for visualization or adjusting gain. You cannot modify the buffers, and they may arrive at a slightly different time than the media pipeline processes them.
Here's an example of installing a probe during recording:
audioRecorder = new QAudioRecorder; QAudioEncoderSettings audioSettings; audioSettings.setCodec("audio/amr"); audioSettings.setQuality(QMultimedia::HighQuality); audioRecorder->setEncodingSettings(audioSettings); audioRecorder->setOutputLocation(QUrl::fromLocalFile("test.amr")); audioProbe = new QAudioProbe(this); if (audioProbe->setSource(audioRecorder)) { // Probing succeeded, audioProbe->isValid() should be true. connect(audioProbe, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(calculateLevel(QAudioBuffer))); } audioRecorder->record(); // Now audio buffers being recorded should be signaled // by the probe, so we can do things like calculating the // audio power level, or performing a frequency transform
Qt Multimedia offers classes for raw access to audio input and output facilities, allowing applications to receive raw data from devices like microphones, and to write raw data to speakers or other devices. Generally these classes do not do any audio decoding, or other processing, but they can support different types of raw audio data.
The QAudioOutput 类提供原生音频数据输出,而 QAudioInput offers raw audio data input. Both classes have adjustable buffers and latency, so they are suitable for both low latency use cases (like games or VOIP) and high latency (like music playback). The available hardware determines what audio outputs and inputs are available.
低级音频类可以运转于 2 种模式下
push
and
pull
。在
pull
模式,音频设备的启动是通过将它赋予
QIODevice
。对于输出设备,
QAudioOutput
类将 pull (拉出) 数据从
QIODevice
(使用
QIODevice::read
()) 当要求更多音频数据时。相反,对于
pull
模式采用
QAudioInput
,当音频数据可用时,数据将被直接写入
QIODevice
.
在
push
模式,音频设备提供
QIODevice
instance that can be written or read to as needed. Typically this results in simpler code but more buffering, which may affect latency.
In some cases you may want to decode a compressed audio file and do further processing yourself (for example, mixing multiple samples or using custom digital signal processing algorithms). QAudioDecoder 支持解码本地文件或数据流从 QIODevice 实例。
这里是本地文件解码范例:
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(); // Now wait for bufferReady() signal and call decoder->read()
存在 C++ 和 QML 两种可用范例。
QAbstractAudioDeviceInfo | 音频后端基类 |
QAudio | 包含用于音频类的枚举 |
QAudioBuffer | 表示具有特定格式和采样率的一批音频样本 |
QAudioDecoder | 允许解码音频 |
QAudioDeviceInfo | 查询音频设备及其功能的接口 |
QAudioFormat | 存储音频流参数信息 |
QAudioInput | 从音频输入设备接收音频数据的接口 |
QAudioOutput | 把音频数据发送到音频输出设备的接口 |
QAudioProbe | 允许监视正播放 (或录制) 音频 |
QAudioSystemPlugin | 音频插件抽象基 |
QSound | 播放 .wav 声音文件的方法 |
QSoundEffect | 播放低延迟音效的办法 |
QAudioBuffer::StereoFrame | 立体声音频帧的简单包裹器 |
音频 | 把音频回放添加到场景 |
MediaPlayer | 把媒体回放添加到场景 |
Playlist | 针对要播放的指定媒体列表 |
PlaylistItem | 定义播放列表项 |
SoundEffect | 提供在 QML 中播放音效方式的类型 |