2
votes

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
Have you tried to construct QMediaPlayer with QMediaPlayer::StreamPlayback option like : QMediaPlayer * player = new QMediaPlayer(0,QMediaPlayer::StreamPlayback) ?Nejat
I tried it and it still plays only till the position when setMedia was called.user2487382
When 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.hyde
Yes, you are right, sorry.user2487382

2 Answers

2
votes

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.

0
votes

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