player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <QWidget>
#include <QMediaPlayer>
#include <QMediaPlaylist>
class Player : public QWidget
{
Q_OBJECT
public:
Player(QWidget *parent = 0);
~Player();
private slots:
void playClicked();
private:
QMediaPlayer::State playerState;
QMediaPlayer *player;
QMediaPlaylist *playlist;
signals:
void play();
};
#endif // PLAYER_H
player.cpp
#include "player.h"
#include <QtWidgets>
Player::Player(QWidget *parent) : QWidget(parent)
{
QToolButton *playButton = new QToolButton(this);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(playButton, SIGNAL(clicked()), this, SLOT(playClicked()));
player = new QMediaPlayer(this);
connect(this, SIGNAL(play()), player, SLOT(play()));
playlist = new QMediaPlaylist();
player->setPlaylist(playlist);
QUrl url = QUrl::fromLocalFile("d:/danny/Downloads/media files/Viva La Vida.mp3");
playlist->addMedia(url);
}
Player::~Player()
{
}
void Player::playClicked()
{
switch (playerState) {
case QMediaPlayer::StoppedState:
case QMediaPlayer::PausedState:
emit play();
break;
case QMediaPlayer::PlayingState:
break;
}
}
main.cpp
#include "player.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Player player;
player.show();
return app.exec();
}
dannyplayer.pro
TARGET = DannyPlayer
TEMPLATE = app
QT += network \
xml \
multimedia \
multimediawidgets \
widgets
SOURCES += main.cpp \
player.cpp
HEADERS += \
player.h
FORMS +=