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.
-
1Have you tried to construct `QMediaPlayer` with `QMediaPlayer::StreamPlayback` option like : `QMediaPlayer * player = new QMediaPlayer(0,QMediaPlayer::StreamPlayback)` ? – Nejat Oct 15 '14 at 11:54
-
I tried it and it still plays only till the position when `setMedia` was called. – Oct 15 '14 at 12:10
-
1When you are asking, why some code does not work, it's good idea to attach the code... At least a good, relevant snippet, if not a complete [MVCE](http://stackoverflow.com/help/mcve). – hyde Oct 15 '14 at 14:28
-
Yes, you are right, sorry. – Oct 15 '14 at 15:19
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.

- 1,580
- 12
- 21
-
Yes. That's what it looks like. See if [this](http://stackoverflow.com/questions/21404500/qmediaplayer-streaming-from-a-custom-qiodevice-with-encryption-on-mac-os-10-9) post is of any help to you. – Nithish Oct 16 '14 at 10:08
-
I am not sure if this will make any difference. But try passing an `QMediaContent` instance as the first parameter to `QMediaPlayer::setMedia` instead of `NULL`. i.e., `mediaPlayer.setMedia(QMediaContent(), song);` – Nithish Oct 16 '14 at 10:22
-
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