PDA

View Full Version : playing music from a database



Baasie
14th January 2010, 09:07
Goodday fellow QT'ers, i have searched the forums and internet for my problem but it could not be found.

First of all what i am trying to achieve:
I got a music database where it is needed to store the mp3 file into it, i work with a sqlite database. Everything work fine, i can input the files intp the database as blob and also can grab it out of it, store it into a new file and play it.

But here is what i am trying to do now, when i grab the file from my db i store it in a QByteArray and after put it into an file, but what i want to do is play it directly from that QByteArray wich is possible using QBuffer as i have read in the docs.

i tried this approach but it does not seem to want to work, here is my code:



QSqlQuery blob = dataBase->getQuery("SELECT id_music, data FROM user_getBlob WHERE (id_music=10)");
blob.next();
QByteArray soundByte = blob.value(1).toByteArray();
QCoreApplication::processEvents();
blob.clear();
soundBuffer = new QBuffer(&soundByte);
QCoreApplication::processEvents();
mediaPlayer->mediaObject->setCurrentSource(soundBuffer);



if i do this i get the following error:

No combination of filters could be found to render the stream. (0x80040218)

but here is the odd part, the error is written in my native language wich is dutch (i translated it).. and there is the strange part.

so does anyone here know what the problem might be, i know the solution is just to store the bytearray into a file and play the file, but if there is a way to make it work like this it would be much better.

thanx in advance,

Baasie

numbat
15th January 2010, 08:24
I think you have to call soundBuffer.open(QIODevice::ReadOnly); after creating it. Also, be sure that soundByte is not going out of scope while the buffer is being read.

Baasie
18th January 2010, 11:32
Thank you for your reply,

it seems that it goes better now only it gives me a QThread::wait: Thread tried to wait on itself.

My code :


"Query: SELECT id_music, data FROM user_getBlob WHERE (id_music=35)"

core class


blobString.append("SELECT id_music, data FROM user_getBlob WHERE (id_music="+newMusicModel->musicList.at(charts->rowNewSongIndex()).getID_Music()+")");
QSqlQuery blob = dataBase->getQuery(blobString);
blob.next();
QByteArray soundByte = blob.value(1).toByteArray();
QCoreApplication::processEvents();
blob.clear();
soundBuffer = new QBuffer(&soundByte);
QCoreApplication::processEvents();
mediaPlayer->inputUrl(soundBuffer);



MediaPlayer class


void MediaPlayer::inputUrl(QBuffer *buffer)
{
buffer->open(QIODevice::ReadOnly);
Phonon::MediaSource source(buffer);
mediaObject->stop();
mediaObject->clearQueue();
mediaObject->setCurrentSource(source);
audioOutput->setVolume(0.0);
mediaObject->play();
playTimer = new QTimer(this);
connect(playTimer, SIGNAL(timeout()), this, SLOT(stopped()));
playTimer->start(20000);
emit output("MediaPlayer::inputUrl >> ", "Loaded mp3");
}


anyone know what this can mean...

the QTimer is not the problem, i use it only to play just 20s of the son....

thanx in advance

p.s. how do you prevent it from not getting out of scope?

numbat
18th January 2010, 11:55
Just create it on the heap:


QByteArray * soundByte = new QByteArray(blob.value(1).toByteArray());

P.S QByteArray uses implicit data sharing so don't worry about having multiple copies.

Baasie
18th January 2010, 13:20
Never thought about doing it like that,

BUT thanx A LOT it works perfectly :D

case closed .... :cool:

Baasie
19th January 2010, 10:48
okey case not close :(...

it has been wroking great but i checked the internal memory use of my program and everytime it call this function:



QSqlQuery blob = dataBase.getQuery("SELECT id_music, data FROM user_getBlob WHERE (id_music="+selectedAlbumModel.selectedAlbumList.at(index.row ()).getID_Music()+")");
blob.next();
QByteArray * soundByte = new QByteArray(blob.value(1).toByteArray());
blob.clear();
soundBuffer = new QBuffer(soundByte);
mediaPlayer.inputUrl(soundBuffer);


it extends the internal memory use of the program... anyone has any clue how this is possible?

thanx in advance