#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();
}
此代码无法播放该文件,我收到此错误:
:: error:collect2:ld返回1退出状态
当我点击按钮时,有人可以帮助我播放文件吗?
感谢。
答案 0 :(得分:2)
我猜在头文件中声明了一个或多个函数,但它们的实体尚未构建。
例如:
//headerfile
class MyClass
{
public: MyClass();
private: void function1();
void function2();
};
//source file
MyClass::MyClass(){}
void MyClass::function1(){ /*do something*/ }
//here function2 is missing.
因此,请检查整个项目中的所有功能是否都有自己的身体。
对于基本的声子媒体播放器,
#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();
}
答案 1 :(得分:1)
正如templatetypedef所评论的,它听起来像链接器错误。确保已将所有必需的库添加到.pro文件中。例如,您需要链接Phonon,因此您的.pro文件必须包含
QT += phonon