Search code examples
c++qtqt4phonon

Qt phonon media object error


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <Phonon/MediaSource>
#include <QUrl>
#include <Phonon/MediaObject>


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

}

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

void MainWindow::on_pushButton_clicked()
{
    QUrl url("http://www.example.com/music.ogg");
    Phonon::MediaObject *wow =
             Phonon::createPlayer(Phonon::NoCategory,
                                  Phonon::MediaSource(url));
         wow->play();

   }

This code won't play the file, and I'm getting this error:

:: error: collect2: ld returned 1 exit status

Can anyone help me get the file to play when I click the button?

Thanks.


Solution

  • I guess that there are one or more functions declared in the header file but their bodies haven't been built yet.

    for example:

    //headerfile
    class MyClass
    {
        public: MyClass();
        private: void function1();
                 void function2();
    };
    
    //source file
    MyClass::MyClass(){}
    void MyClass::function1(){ /*do something*/ }
    //here function2 is missing.
    

    So, please check that all the functions in the whole project have their bodies.

    For a basic phonon media player,

    #ifndef MYVIDEOPLAYER_H
    #define MYVIDEOPLAYER_H
    
    #include <QWidget>
    #include <QPushButton>
    #include <Phonon/VideoPlayer>
    #include <QVBoxLayout>
    
    class MyVideoPlayer : public QWidget
    {
         Q_OBJECT
    public:
          explicit MyVideoPlayer(QWidget *parent = 0);
    private:
          Phonon::VideoPlayer *videoPlayer;
          QPushButton *btnButton;
          QVBoxLayout layout;
    
    private slots:
          void onPlay();
    };
    #endif // MYVIDEOPLAYER_H
    
    #include "myvideoplayer.h"
    
    MyVideoPlayer::MyVideoPlayer(QWidget *parent) :
        QWidget(parent)
    {
        videoPlayer=new Phonon::VideoPlayer(Phonon::VideoCategory,this);
        btnButton=new QPushButton("Play",this);
    
        layout.addWidget(btnButton);
        layout.addWidget(videoPlayer);
    
        setLayout(&layout);
    
        connect(btnButton,SIGNAL(clicked()),this,SLOT(onPlay()));
    }
    
    void MyVideoPlayer::onPlay()
    {
        videoPlayer->load(Phonon::MediaSource("movie.mp4"));
        videoPlayer->play();
    }