我有来自服务器的音频流,我想在程序中使用QMediaPlayer
播放。当我第一次将文件下载到QBuffer
,然后从播放器调用setMedia
和play
方法时,一切都正常。但如果我想在流仍在工作时播放音乐,媒体播放器只将声音播放到调用setMedia
方法时的位置,然后停止。有什么可能的方法让它像我想的那样工作吗?非常感谢。
如果您以正确的方式初始化播放器,我认为没有理由不工作。
由于你还没有分享你写的代码(如果我留下评论,我今天剩下的时间都看不到你的回复),我会在这里留下一些示例代码。看看下面的代码是否适合您。
QMediaPlayer* player = new QMediaPlayer(this, QMediaPlayer::StreamPlayback);
player->setMedia(QUrl("http://vpr.streamguys.net/vpr64.mp3"));
player->setVolume(80);
player->play();
如果是这样的话,可以通过更改流的url来尝试同样的操作。
编辑:我假设玩家在更新之前已经耗尽了缓冲区。试着关注bufferStatus
和QMediaPlayer::MediaStatus
。我引用文件:
bufferStatus : const int
This property holds the percentage of the temporary buffer filled before playback begins or resumes, from (empty) to (full).
When the player object is buffering; this property holds the percentage of the temporary buffer that is filled. The buffer will need to reach 100% filled before playback can start or resume, at which time mediaStatus() will return BufferedMedia or BufferingMedia. If the value is anything lower than 100, mediaStatus() will return StalledMedia.
通过音频输出:
QByteArray* yourSoundData = blah blah...;
QBuffer* buffer = new QBuffer;
buffer->setData(yourSoundData);
buffer->open(QBuffer::ReadOnly);
QAudioFormat format; // According to your sound format (e.g. wav)
format.setSampleRate(22050);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/wav");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
qWarning() << "Raw audio format not supported by backend, cannot play audio.";
return;
}
QAudioOutput* audio = new QAudioOutput(format, this);
audio->start(buffer);
更多信息:http://doc.qt.io/qt-5/qaudiooutput.html