I have audio stream from server that I want to play with QMediaPlayer
in my program. Everything works when I first download file to QBuffer
and then call setMedia
and play
method from the player. But if I want to play music while stream is still working, media player only plays sound to the position when setMedia
method was called and then stoppes.
Is there any possible ways to make it work like I want to? Thank you.
2 回答
如果您以正确的方式初始化播放器,我认为它没有理由不工作。
由于您尚未共享您编写的代码(另外,如果我发表评论,我将在剩下的时间里无法查看您的回复),我将在此处留下一些示例代码。看看下面的代码是否适合你。
QMediaPlayer* player = new QMediaPlayer(this, QMediaPlayer::StreamPlayback);
player->setMedia(QUrl("http://vpr.streamguys.net/vpr64.mp3"));
player->setVolume(80);
player->play();
如果是这样,请通过将 url 更改为您的流来尝试相同的操作。
编辑:我假设玩家在更新之前已经耗尽了缓冲区。尝试密切关注bufferStatus
and 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.
通过 QAudioOutput:
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);