PDA

View Full Version : Unable to play audio from memory.



LaurentDuroisin
28th January 2016, 10:10
Hi!

I want to load a sound from memory and play it but I don't heard anything.

Here is the code :



#include <QCoreApplication>
#include <QMediaPlayer>
#include <QBuffer>
#include <QIODevice>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
unsigned short samples[1000000];
for (unsigned int i = 0; i < sizeof(samples) / sizeof(float); i++) {
samples[i] = 1000;
}
QByteArray byteArray((const char*) samples,sizeof(samples));
QBuffer mediaStream(&byteArray);
QMediaPlayer mp;
mp.setMedia(QMediaContent(),&mediaStream);
mp.play();
return a.exec();
}


Here is the error message :


GStreamer; Unable to pause - ""
Error : "No URI set"

d_stranz
28th January 2016, 17:53
Nothing happens because you are calling play() before you have an event loop running (a.exec()). Move your media player code out of main() and into some GUI class (like a QDialog). After that GUI element is visible, then you should be able to start the media player and play a sound.

LaurentDuroisin
28th January 2016, 18:11
Ok, I'll try that thing, thanks.

anda_skoa
28th January 2016, 19:51
If it is an event loop issue, just call the play() slot via a single shot timer.

Cheers,
_

LaurentDuroisin
29th January 2016, 12:09
Hi, what is a "shot timer" ?

I've tried to call the play function into a QDialog.



#include "mydialog.h"
#include <QMediaPlayer>
#include <QBuffer>
MyDialog::MyDialog()
{
unsigned short samples[1000000];
for (unsigned int i = 0; i < sizeof(samples) / sizeof(float); i++) {
samples[i] = 1000;
}
QByteArray byteArray((const char*) samples,sizeof(samples));
QBuffer mediaStream(&byteArray);
QMediaPlayer mp;
mp.setMedia(QMediaContent(),&mediaStream);
mp.play();
}




#include <QApplication>
#include "mydialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog dialog;
return a.exec();
}


But I've always this message, even if I set play into a loop.



GStreamer; Unable to pause - ""

anda_skoa
29th January 2016, 13:48
Hi, what is a "shot timer" ?

A single shot timer is a timer that only fires once.
See QTimer::singleShot().



I've tried to call the play function into a QDialog.



#include "mydialog.h"
#include <QMediaPlayer>
#include <QBuffer>
MyDialog::MyDialog()
{
unsigned short samples[1000000];
for (unsigned int i = 0; i < sizeof(samples) / sizeof(float); i++) {
samples[i] = 1000;
}
QByteArray byteArray((const char*) samples,sizeof(samples));
QBuffer mediaStream(&byteArray);
QMediaPlayer mp;
mp.setMedia(QMediaContent(),&mediaStream);
mp.play();
}


Your "mp" is a variable on the stack of the MyDialog constructor. It will be destroyed once the constructor ends.

Cheers,
_

d_stranz
29th January 2016, 17:18
Your "mp" is a variable on the stack of the MyDialog constructor. It will be destroyed once the constructor ends.

Not only that, but if you are constructing the MyDialog instance on the stack in main() *before* calling app.exec(), then you *still* don't have an event loop running at the point the constructor code is executing.

Add a button to your dialog and a slot in which you execute the play code. Connect the button's clicked() signal to this slot in the MyDialog constructor.

LaurentDuroisin
30th January 2016, 09:20
Ok I'll try that.