MyPlayer.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-04-03T14:07:12
#
#-------------------------------------------------
QT += core gui multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MyPlayer
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
FORMS += dialog.ui
----------------------------------------------------------------------
dialog.h
#ifndef DIALOG_H#define DIALOG_H#include < QDialog>#include< QMediaPlayer>#include< QDebug>namespace Ui {class Dialog;}class Dialog : public QDialog{Q_OBJECTpublic:
explicit Dialog(QWidget *parent = 0);~Dialog();private slots:void on_sliderProgress_sliderMoved(int position);void on_sliderVolume_sliderMoved(int position);void on_pushButton_clicked();void on_pushButton_2_clicked();void on_durationChanged(quint64 position);void on_positionChanged(quint64 position);private:
Ui::Dialog *ui;QMediaPlayer* player;};#endif // DIALOG_H-------------------------------------------------------------dialog.cpp#include " dialog.h"#include " ui_dialog.h"Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog){ui->setupUi(this);player = new QMediaPlayer(this);connect(player,&QMediaPlayer::positionChanged,this,&Dialog::on_positionChanged);connect(player,&QMediaPlayer::durationChanged,this,&Dialog::on_durationChanged);}Dialog::~Dialog(){delete ui;}void Dialog::on_sliderProgress_sliderMoved(int position){player->setPosition(position);}void Dialog::on_sliderVolume_sliderMoved(int position){player->setVolume(position);}void Dialog::on_pushButton_clicked(){// Load Fileplayer->setMedia(QUrl::fromLocalFile("//C://Users/dtsis//Desktop//se.mp3"));player->play();qDebug() < < player->errorString();}void Dialog::on_pushButton_2_clicked(){player->stop();}void Dialog::on_durationChanged(quint64 position){ui->sliderProgress->setMaximum(position);}void Dialog::on_positionChanged(quint64 position){ui->sliderProgress->setValue(position);}----------------------------------------------------------main.cpp#include "dialog.h"#include < QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);Dialog w;w.show();
return a.exec();}----------------------------------------------------------------