6. Audio and video playback of Qt Project

Audio and video playback 

Here is a simple music player. The interface design of the player is as follows:

Step1: Here is the HTML file corresponding to the interface:

View Code

The related modules of Qt's metadata are used here, and the metadata library needs to be called

Add the following to the MusicPlayer.pro file:

QT += core gui multimedia

This adds the audio and video module multimedia required by the project

Step2: We need to add the slot function declaration corresponding to each key signal in the mainwindow.h header file and the data structure of the multimedia objects used in the project as follows (content of the mainwindow.h file):

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMediaPlayer> #include <QMediaPlaylist> #include <QMultimedia> #include <QMediaMetaData> #include <QTimer> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_NextSong_clicked(bool checked); //下一首-槽函数声明 void on_PrevSong_clicked(bool checked); // 上一首-槽函数声明 void on_Volume_valueChanged(int value); // 音量改变的槽函数申明 void on_SongChoose_sliderMoved(int position); void on_openlocal_media(); void on_Play_Puase_clicked(bool checked); void on_playProgressUpdate(); voidon_MetaDateUpdate(); private : Ui::MainWindow * ui; QMediaPlayer *mediaPlayer; // Multimedia object variable declaration QMediaPlaylist *localMediaPlaylist; // Multimedia object list object declaration QTimer *progressTimer; // Timer object declaration }; #endif // MAINWINDOW_H

Step3: Implementation of the corresponding slot function:

 func1:on_NextSong_clicked   func2:on_PrevSong_clicked   func3:on_Volume_valueChanged   func4:on_SongChoose_sliderMoved   

 func5:on_openlocal_media     func6:on_Play_Puase_clicked    func7:on_playProgressUpdate    func8:on_MetaDateUpdate

View Code

Step4: After completing the implementation of the slot function, connect the corresponding model and the slot function to connect:

connect(this->ui->NextSong,SIGNAL(clicked(bool)),this,SLOT(on_NextSong_clicked())); //Single connect to SLOT
connect(this->ui->PrevSong,SIGNAL(clicked(bool)),this,SLOT(on_PrevSong_clicked()));
connect(this->ui->Volume,SIGNAL(valueChanged(int)),this,SLOT(on_Volume_valueChanged()));
connect(this->ui->SongChoose,SIGNAL(sliderMoved(int)),this,SLOT(on_SongChoose_sliderMoved()));
connect(this->ui->actionOpenLocalMedia,SIGNAL(triggered(bool)),this,SLOT(on_openlocal_media())); connect(this->ui->Play_Puase,SIGNAL(clicked(bool)),this,SLOT(on_Play_Puase_clicked())); connect(this->progressTimer,SIGNAL(timeout()),this,SLOT(on_playProgressUpdate())); connect(this->mediaPlayer,SIGNAL(metaDataChanged()),this,SLOT(on_MetaDateUpdate()));

Step5: Initialize the variables defined in the mainwindow.h header file:

this->mediaPlayer = new QMediaPlayer(this);
this->localMediaPlaylist = new QMediaPlaylist(this);
this->mediaPlayer->setPlaylist(this->localMediaPlaylist);
this->mediaPlayer->setVolume(50); //Set default Volume Value

this->progressTimer = new QTimer(this); this->progressTimer->setInterval(100); //100ms this->progressTimer->start();

After completing the above work, the entire project can be compiled and run, which is the production of a simple multimedia player.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886943&siteId=291194637