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 Answers
I see no reason for it not to work, if you are initializing the player in the right way.
Since you haven't shared the code you have written (also since I won't be available for the rest of the day to see your response if I leave a comment), I will leave some sample code here. See if the code below works for you.
QMediaPlayer* player = new QMediaPlayer(this, QMediaPlayer::StreamPlayback);
player->setMedia(QUrl("http://vpr.streamguys.net/vpr64.mp3"));
player->setVolume(80);
player->play();
If it does, try the same by changing the url to your stream.
EDIT: I am assuming that the player is exhausting the buffer before you update it. Try keeping an eye on the bufferStatus
and QMediaPlayer::MediaStatus
.
I quote from the documentation:
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.
Via 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);
More info: http://doc.qt.io/qt-5/qaudiooutput.html
QMediaPlayer
withQMediaPlayer::StreamPlayback
option like :QMediaPlayer * player = new QMediaPlayer(0,QMediaPlayer::StreamPlayback)
? – NejatsetMedia
was called. – user2487382