PDA

View Full Version : Problem with phonon in another class



matulik
2nd December 2012, 18:35
Hello. I want to do a litte music player based on phonon libs.

I want to make another class with operations like play, stop etc.


#include "plik.h"

#include <QString>
#include <QDebug>

#include <phonon/Global>
#include <phonon/MediaObject>
#include <phonon/AudioOutput>
#include <phonon/MediaSource>
#include <phonon/Path>


Phonon::MediaObject *mediaObject;
Phonon::AudioOutput *audioOutput;


Plik::Plik()
{
this->next = NULL;
this->prev = NULL;
}

void Plik::setPath(QString path)
{
this->path = path;
return;
}

QString Plik::returnPath()
{
return this->path;
}

void Plik::playFile()
{
mediaObject = new Phonon::MediaObject(this);
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory,this);

mediaObject->setCurrentSource(Phonon::MediaSource("/home/matulik/test.mp3"));
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory,this);
Phonon::Path path = Phonon::createPath(mediaObject,audioOutput);

mediaObject->setTickInterval(1000);
audioOutput->setVolume(100);
audioOutput->setMuted(FALSE);
mediaObject->play();

}

And in mainwindow:


Plik p1;
p1.playFile();


And this doesnt work.

I dont know where is the problem, becouse the same source code coping to mainwindow works..



#include "odplayer.h"
#include "ui_odplayer.h"
#include "plik.h"

#include <QString>
#include <QDebug>

#include <phonon/Global>
#include <phonon/MediaObject>
#include <phonon/AudioOutput>
#include <phonon/MediaSource>
#include <phonon/Path>


Phonon::MediaObject *mediaObject;
Phonon::AudioOutput *audioOutput;


odplayer::odplayer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::odplayer)
{
ui->setupUi(this);

mediaObject = new Phonon::MediaObject(this);
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory,this);

mediaObject->setCurrentSource(Phonon::MediaSource("/home/matulik/test.mp3"));
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory,this);
Phonon::Path path = Phonon::createPath(mediaObject,audioOutput);

mediaObject->setTickInterval(1000);
audioOutput->setVolume(100);
audioOutput->setMuted(FALSE);
mediaObject->play();
}

odplayer::~odplayer()
{
delete ui;
}


Someone can help?

amleto
2nd December 2012, 19:03
In this code, what is wrong?


void func()
{
Plik p1;
p1.playFile();
}



fyi:
posting code like this


Plik p1;
p1.playFile();

is not helpful at all because there is no context, e.g. class + method name

matulik
2nd December 2012, 19:44
Its like this:



#include "odplayer.h"
#include "ui_odplayer.h"
#include "plik.h"

#include <QString>
#include <QDebug>

#include <phonon/Global>
#include <phonon/MediaObject>
#include <phonon/AudioOutput>
#include <phonon/MediaSource>
#include <phonon/Path>


Phonon::MediaObject *mediaObject;
Phonon::AudioOutput *audioOutput;


odplayer::odplayer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::odplayer)
{
ui->setupUi(this);
Plik p1;
p1.playFile();
}

odplayer::~odplayer()
{
delete ui;
}

amleto
2nd December 2012, 19:46
yeah, now tell me what is wrong with this


odplayer::odplayer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::odplayer)
{
ui->setupUi(this);
Plik p1;
p1.playFile();
}

Hint: It's a c++ 'thing'.

matulik
2nd December 2012, 20:35
Ok, I know where is problem:


File* f1 = new File;
f1->setPath("/home/matulik/test.mp3");
qDebug()<<f1->returnPath();
f1->playFile();


Thanks very much for hint :)

amleto
2nd December 2012, 21:17
what's a `File`? I haven't seen that in your previous code. And what did I just say about posting code with no context?

By the way, you now have a memory leak.