Qt/C++ - Lesson 051. QMediaPlayer – simple audio player

QMediaPlayer, QMediaPlaylist, QStandardItemModel, Qt, аудио плеер

I suggest to write a simple audio player for mp3 files using Qt/C++, which will have a playlist, the ability to start / pause / stop tracks, as well as scroll through the tracks.

For the implementation of this ideas in Qt, classes are QMediaPlayer and QMediaPlaylist, which belong to the multimedia module. And to display the playlist and use QStandardItemModel QTableView.

Articles in this series:

Project structure

  • SimplePlayer.pro - the profile of the project;
  • main.cpp - the file with the main function;
  • widget.ui - form of application window;
  • widget.h - header file of the application window;
  • widget.cpp - file source code of the application window;
  • buttons.qrc - resource file application icon buttons.

widget.ui

The application interface is made using a graphic designer, and it have look as follows.

The application interface consists of the following elements:

  • btn_add (QToolButton*) - responsible for adding tracks to the playlist;
  • btn_next (QToolButton*) - responsible for flipping the playlist ahead;
  • btn_previous (QToolButton*) - responsible for flipping playlist ago;
  • btn_play (QToolButton*) - responsible for track playback;
  • btn_pause (QToolButton*) - responsible for setting the track is paused;
  • btn_stop (QToolButton*) - responsible for track stop;
  • currentTrack (QLabel*) - label, which will display the current track;
  • playlistView (QTableView*) - table, which displays the playlist.

SimplePlayer.pro

The project profile that you connect the multimedia module, otherwise QMediaPlayer and QMediaPlaylist classes will be not available.

#-------------------------------------------------
#
# Project created by QtCreator 2016-06-29T11:25:56
#
#-------------------------------------------------

QT       += core gui multimedia

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = SimplePlayer
TEMPLATE = app


SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

FORMS    += widget.ui

RESOURCES += \
    buttons.qrc

widget.h

To implement the playlist display, have to use QStandardItemModel. In it will be placed in the path of audio files and audio file names. The first column is the name of the audio file, and the second will be the full path, but this column will be hidden in a QTableView object, which will be responsible for mapping Playlist.

Also, the path to the file as a media source will need to be put in QMediaPlaylist object that will be placed in QMediaPlayer.

Pointers to these objects are placed in the header file of the application window. Also here there avtogenerirovany through designer interface slot to press the button add tracks to the playlist processing.

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QStandardItemModel>
#include <QMediaPlayer>
#include <QMediaPlaylist>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_btn_add_clicked();              // The slot for the processing of adding tracks through dialog

private:
    Ui::Widget *ui;
    QStandardItemModel  *m_playListModel;   // Data Model for Playlist
    QMediaPlayer        *m_player;          
    QMediaPlaylist      *m_playlist;       
};

#endif // WIDGET_H

widget.cpp

For realization of the player it is necessary to initialize the object QMediaPlayer , QMediaPlaylist and QStandardItemModel , which have been declared in the header file of the application window. In the first half of the designer made customize the appearance of the table to display the playlist, while the second setting of the player. player controlled via the buttons that are connected to the control slots m_playlist (for navigation) and m_player (to start / pause / stop).

If you change the current track, the player will automatically terminates playback of the track that was before the change, and starts to play a new track.

Due to the fact that QMediaPlaylist has no model to be displayed in the table, we use the QStandardItemModel class, so you have to add data about file paths and there and there.

#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QDir>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_playListModel = new QStandardItemModel(this);
    ui->playlistView->setModel(m_playListModel);    
    m_playListModel->setHorizontalHeaderLabels(QStringList()  << tr("Audio Track")
                                                            << tr("File Path"));
    ui->playlistView->hideColumn(1);   
    ui->playlistView->verticalHeader()->setVisible(false);                  
    ui->playlistView->setSelectionBehavior(QAbstractItemView::SelectRows);  
    ui->playlistView->setSelectionMode(QAbstractItemView::SingleSelection); 
    ui->playlistView->setEditTriggers(QAbstractItemView::NoEditTriggers);  
    ui->playlistView->horizontalHeader()->setStretchLastSection(true);

    m_player = new QMediaPlayer(this);          // Init player
    m_playlist = new QMediaPlaylist(m_player);  // Init playlist
    m_player->setPlaylist(m_playlist);          
    m_player->setVolume(70);                    
    m_playlist->setPlaybackMode(QMediaPlaylist::Loop);  // Set circular play mode playlist

    // подключаем кнопки управления к слотам управления
    // Here we note that the navigation is done through the playlist playlist
    // and start / pause / stop via the player itself
    connect(ui->btn_previous, &QToolButton::clicked, m_playlist, &QMediaPlaylist::previous);
    connect(ui->btn_next, &QToolButton::clicked, m_playlist, &QMediaPlaylist::next);
    connect(ui->btn_play, &QToolButton::clicked, m_player, &QMediaPlayer::play);
    connect(ui->btn_pause, &QToolButton::clicked, m_player, &QMediaPlayer::pause);
    connect(ui->btn_stop, &QToolButton::clicked, m_player, &QMediaPlayer::stop);

    // When you doubleclick on the track in the table set the track in the playlist
    connect(ui->playlistView, &QTableView::doubleClicked, [this](const QModelIndex &index){
        m_playlist->setCurrentIndex(index.row());
    });

    // if the current track of the index change in the playlist, set the file name in a special label
    connect(m_playlist, &QMediaPlaylist::currentIndexChanged, [this](int index){
        ui->currentTrack->setText(m_playListModel->data(m_playListModel->index(index, 0)).toString());
    });
}

Widget::~Widget()
{
    delete ui;
    delete m_playListModel;
    delete m_playlist;
    delete m_player;
}

void Widget::on_btn_add_clicked()
{
    // Using the file selection dialog to make multiple selections of mp3 files
    QStringList files = QFileDialog::getOpenFileNames(this,
                                                      tr("Open files"),
                                                      QString(),
                                                      tr("Audio Files (*.mp3)"));

    // Next, set the data names and file paths
    // into the playlist and table displaying the playlist
    foreach (QString filePath, files) {
        QList<QStandardItem *> items;
        items.append(new QStandardItem(QDir(filePath).dirName()));
        items.append(new QStandardItem(filePath));
        m_playListModel->appendRow(items);
        m_playlist->addMedia(QUrl(filePath));
    }
}

Result

Once the app is started, you can open the mp3 tracks and listen to them.

Download Qt Audio Player

Video

10% refund of hotel reservation amount on Booking
10% refund of hotel reservation amount on Booking
We offer a link with a 10% return on the amount of the order when booking a hotel through Booking
Support the author Donate
S

Скажите, пожалуйста, как на линуксе исправить ошибку , связанную с непраильной схемой пути к файлу - "Error: "Invalid URI ...."" и далее соответственно GSteamer; unable to pause...

На официальном форуме указано на то, что "when passing local file to the media player, you need to use the "file://" scheme so it knows it has to look in to the file system." Но что конкретно из этого стоит вынести, не ясно.
Благодарен за внимание.

Ответил на форуме

Comments

Only authorized users can post comments.
Please, Log in or Sign up
D
Aug. 16, 2019, 12:58 p.m.
Damir

C++ - Тест 003. Условия и циклы

  • Result:92points,
  • Rating points8
D
Aug. 16, 2019, 12:46 p.m.
Damir

C++ - Test 005. Structures and Classes

  • Result:75points,
  • Rating points2
u
Aug. 14, 2019, 2:55 p.m.
unrealproro

C++ - Test 005. Structures and Classes

  • Result:83points,
  • Rating points4
Last comments
b
Aug. 18, 2019, 6:09 a.m.
bbb116

cqtdeployer /home/aleks/CQtDeployer/bin/cqtdeployer: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.25' not found (required by /home/aleks/CQtDeployer/lib/libQt5Core.so.5) linux mint …
D
Aug. 17, 2019, 9:04 a.m.
Damir

github ChekableTView Правой групповая смена значения при перетаскивании левой как обычно.
Aug. 16, 2019, 1:03 p.m.
Evgenij Legotskoj

Потому, что в минуте 60 секунд
Aug. 16, 2019, 12:16 p.m.
Dmitrij

а почему делитель 60000, а не 1000?
g
Aug. 14, 2019, 1:27 a.m.
grig_p

Спасибо большое. Получилось
Now discuss on the forum
Aug. 19, 2019, 12:40 a.m.
nayk1982

Программно можно проверить существование таблицы, получить список таблиц БД, получить список полей конкретной таблицы и т.д. Это все на совести программиста и если структура БД может меняться, т…
Aug. 15, 2019, 3:19 a.m.
Mihailll

Плюсы и qml отличаются, с++ логичней
Aug. 14, 2019, 8:20 a.m.
Evgenij Legotskoj

Да это не столько баги QML, сколько поведение JavaScript, который используется в нём. Из-за отсутствия строгой типизации получаем некоторые проблемы с преобразованием типов. в итоге, на первый в…
Aug. 14, 2019, 2:33 a.m.
BlinCT

Ошибка найдена) недосмотрел.
Aug. 13, 2019, 3:52 a.m.
Evgenij Legotskoj

Бери остаток от деления #include <QCoreApplication>#include <QTime>#include <QDebug>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QTime time…
Looking for a Job?
14,000.00 руб. - 40,000.00 руб.
Разработчик Qt
Annino, Moscow Oblast, Russia
5,000.00 руб. - 15,000.00 руб.
Дизайнер
Moskovskiy, Moscow, Russia
25,000.00 руб. - 30,000.00 руб.
Разработчик Qt/C++
Barnaul, Altai Krai, Russia

For registered users on the site there is a minimum amount of advertising

EVILEG
About
Services
© EVILEG 2015-2019
Recommend hosting TIMEWEB