Qt+MPlayer music player development notes: compile MPlayer and Demo demo on ubuntu

Posted by Bisdale on Fri, 14 Jan 2022 05:16:30 +0100

If the article is original, please indicate the source of the original
This blog address: https://hpzwl.blog.csdn.net/article/details/118713520

The complete blog of red fat man (red imitation): the collection of development technologies (including Qt practical technology, raspberry pie, 3D, OpenCV, OpenGL, ffmpeg, OSG, single chip microcomputer, combination of software and hardware, etc.) is continuously updated... (click the portal)

Qt development column: third party library development technology


preface

  realize MPlayer player playing music on ubuntu.


Demo

  

  

  
  
  


Mplayer

  MPlayer is an open source multimedia player released under the GNU General Public License. This software can be used in various mainstream operating systems, such as Linux and other Unix like systems, Windows and Mac OS X systems.
   Mplayer is based on the command line interface, and different graphical interfaces can be installed in each operating system. Another major feature of Mplayer is its extensive output device support. It can work under X11, Xv, DGA, OpenGL, SVGAlib, fbdev, AAlib and DirectFB, and can use GGI, SDL and some low-level hardware related driver modes (such as Matrox, 3Dfx, Radeon, Mach64 and Permedia3). Mplayer also supports display through hardware MPEG decoding cards, such as DVB, DXR3 and Hollywood +.
  MPlayer was developed in 2000. The original author was Arpad Gereoffy. MPlayer was originally called "MPlayer - The Movie Player for Linux", but later developers referred to it as "MPlayer - The Movie Player" because MPlayer can be used not only for Linux but also on all platforms.

download

  download address of the latest source code: http://mplayerhq.hu/design7/news-archive.html
   QQ group: 1047134658 (click "file" to search "MPlayer", and the group will be updated synchronously with the blog)


Ubuntu compilation

Step 1: Download and unzip

tar xvf MPlayer-1.4.tar.xz

  

Step 2: configure

cd MPlayer-1.4/
./configure

  

./configure --yasm=''

  

Step 3: make. You need zlib library support and compile zlib library

make

  

   zlib library needs to be compiled first. Please refer to the blog libzip Development Notes (II): introduction to libzip library, ubuntu platform compilation and project template.

sudo ldconfig

Step 4: continue compiling make

make

  

Step 5: install sudo make install

sudo make install

  

Step 6: play test

  
   (Note: if it is a virtual machine, the volume of the virtual machine and the sound card of the host need to be selected)


Demo

Widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QMainWindow>
#include <QThread>
#include "MplayerManager.h"
#include <QFileDialog>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

protected:
    void initControls();

protected slots:
    void slot_durationChanged(int duration);
    void slot_positionChanged(int position);
    void slot_finished();
    void slot_mediaInfo(MplayerManager::MediaInfo mediaInfo);

private slots:
    void on_pushButton_startPlay_clicked();
    void on_pushButton_stopPlay_clicked();
    void on_pushButton_pausePlay_clicked();
    void on_pushButton_resume_clicked();
    void on_horizontalSlider_sliderReleased();
    void on_horizontalSlider_valueChanged(int value);
    void on_pushButton_mute_clicked(bool checked);
    void on_horizontalSlider_position_sliderReleased();
    void on_horizontalSlider_position_sliderPressed();
    void on_pushButton_browserMplayer_clicked();
    void on_pushButton_defaultMplayer_clicked();
    void on_pushButton_browserMusic_clicked();

private:
    Ui::Widget *ui;

    QThread *_pMplayerManagerThread;
    MplayerManager *_pMplayerManager;

    bool _sliderPositionPressed;
};

#endif // WIDGET_H

Widget.cpp

#include "Widget.h"
#include "ui_Widget.h"
#include "MplayerManager.h"

#include <QDebug>
#define LOG qDebug()<<__FILE__<<__LINE__

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    _pMplayerManagerThread(0),
    _pMplayerManager(0),
    _sliderPositionPressed(false)
{
    ui->setupUi(this);

    QString version = "v1.0.0";
    setWindowTitle(QString("mplayer player %1 (Changsha hongpangzi Network Technology Co., Ltd QQ:21497936 WeChat:yangsir198808 Company website: hpzwl.blog.csdn.net)").arg(version));

    // Initialize modbus thread
    _pMplayerManagerThread = new QThread();
    _pMplayerManager = new MplayerManager();
    _pMplayerManager->moveToThread(_pMplayerManagerThread);
    QObject::connect(_pMplayerManagerThread, SIGNAL(started()),
                     _pMplayerManager, SLOT(slot_start()));
    connect(_pMplayerManager, SIGNAL(signal_durationChanged(int)),
            this, SLOT(slot_durationChanged(int)));
    connect(_pMplayerManager, SIGNAL(signal_positionChanged(int)),
            this, SLOT(slot_positionChanged(int)));
    connect(_pMplayerManager, SIGNAL(signal_mediaInfo(MplayerManager::MediaInfo)),
            this, SLOT(slot_mediaInfo(MplayerManager::MediaInfo)));
    connect(_pMplayerManager, SIGNAL(signal_finished()),
            this, SLOT(slot_finished()));
    _pMplayerManagerThread->start();

    initControls();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::initControls()
{
    ui->horizontalSlider->setMinimum(0);
    ui->horizontalSlider->setMaximum(100);
    ui->horizontalSlider->setValue(100);
    ui->label_volume->setText("100");
}

void Widget::slot_durationChanged(int duration)
{
    LOG << duration;
    ui->label_duration->setText(QString("%1%2:%3%4")
                       .arg(duration / 60 / 10)
                       .arg(duration / 60 % 10)
                       .arg(duration % 60 / 10)
                       .arg(duration % 10));
    ui->horizontalSlider_position->setMinimum(0);
    ui->horizontalSlider_position->setMaximum(duration);
}

void Widget::slot_positionChanged(int position)
{
    ui->label_position->setText(QString("%1%2:%3%4")
                       .arg(position / 60 / 10)
                       .arg(position / 60 % 10)
                       .arg(position % 60 / 10)
                       .arg(position % 10));
    if(!_sliderPositionPressed)
    {
        ui->horizontalSlider_position->setValue(position);
    }
}

void Widget::slot_finished()
{
    ui->label_position->setText("00:00");
}

void Widget::slot_mediaInfo(MplayerManager::MediaInfo mediaInfo)
{
    ui->label_title->setText(mediaInfo.title);
    ui->label_album->setText(mediaInfo.album);
    ui->label_year->setText(mediaInfo.year);
    ui->label_artist->setText(mediaInfo.artist);
    ui->label_genre->setText(mediaInfo.genre);
    ui->label_comment->setText(mediaInfo.comment);
}

void Widget::on_pushButton_startPlay_clicked()
{
    _pMplayerManager->startPlay();
}

void Widget::on_pushButton_stopPlay_clicked()
{
    _pMplayerManager->stopPlay();
}

void Widget::on_pushButton_pausePlay_clicked()
{
    _pMplayerManager->pausePlay();
}

void Widget::on_pushButton_resume_clicked()
{
    _pMplayerManager->resumePlay();
}

void Widget::on_horizontalSlider_sliderReleased()
{
    _pMplayerManager->setVolume(ui->horizontalSlider->value());
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label_volume->setText(QString("%1").arg(value));
}

void Widget::on_pushButton_mute_clicked(bool checked)
{
    _pMplayerManager->setMute(checked);
}

void Widget::on_horizontalSlider_position_sliderReleased()
{
    _sliderPositionPressed = false;
    _pMplayerManager->setPosition(ui->horizontalSlider_position->value());
}

void Widget::on_horizontalSlider_position_sliderPressed()
{
    _sliderPositionPressed = true;
}

void Widget::on_pushButton_browserMplayer_clicked()
{
    _pMplayerManager->setMplayerPath(ui->lineEdit_mplayer->text());
}

void Widget::on_pushButton_defaultMplayer_clicked()
{
    ui->lineEdit_mplayer->setText("mplayer");
}

void Widget::on_pushButton_browserMusic_clicked()
{
    QString dir = ui->lineEdit_music->text();
    dir = dir.mid(0, dir.lastIndexOf("/"));
    QString filePath = QFileDialog::getOpenFileName(0,
                                                    "open",
                                                    dir,
                                                    "AAC(*.aac)"
                                                    ";;MP3(*.mp3)"
                                                    ";;WAV(*.wav)"
                                                    ";;WMA(*.wma)");
    if(filePath.isEmpty())
    {
        return;
    }
    ui->lineEdit_music->setText(filePath);
    _pMplayerManager->setFilePath(filePath);
}

MplayerManager.h

#ifndef MPLAYERMANAGER_H
#define MPLAYERMANAGER_H

/************************************************************\
 * Control name: mplayer management class
 * Control Description:
 *          Use the slave mode to control mplayer to play music, and the basic module realizes single song playing
 * Control functions:
 *          1.Basic operation of music player playing music
 *          2.You can get the relevant album, author, year, comment, genre and other information of the song
 * Copyright information
 *      Author: red fat man (AAA red imitation)
 *      Company: Changsha hongpangzi Network Technology Co., Ltd
 *      Website: hpzwl blog. csdn. net
 *      Contact: QQ(21497936) wechat (yangsir198808) Tel (15173255813)
 * Version information
 *       Date version description
 *   2021 July 12, 2011 v1 0.0 foundation template
\************************************************************/

#include <QObject>
#include <QThread>
#include <QProcess>
#include <QtMath>
#include <QTextCodec>

class MplayerManager : public QObject
{
    Q_OBJECT
public:
    enum PLAY_STATE {                   // Playback status
        PLAY_STATE_STOP = 0x00,         // Not playing, stop playing
        PLAY_STATE_PLAY,                // Playing
        PLAY_STATE_PAUSE                // Pause playback
    };

    struct MediaInfo {                  // Multimedia information
        MediaInfo() {}
        QString title;                  // title
        QString artist;                 // artist
        QString album;                  // Album
        QString year;                   // years
        QString comment;                // comment
        QString genre;                  // schools
    };

public:
    explicit MplayerManager(QObject *parent = 0);
    ~MplayerManager();

public:
    QString getMplayerPath()    const;      // Get the player path (can be called when running)
    QString getFilePath()       const;      // Get the current playback file path
    bool getMute()              const;      // Get whether to mute
    int getVolume()             const;      // Get volume
    int getPosition()           const;      // Get current location

public:
    void setMplayerPath(const QString &mplayerPath);    // Set player path
    void setFilePath(const QString &filePath);          // Set playback file
    void setMute(bool mute);                            // Set mute
    void setVolume(int volume);                         // Set volume (0 ~ 100)
    void setPosition(int position);                     // Set current playback position

signals:
    void signal_stateChanged(PLAY_STATE playState);     // Player playback status signal
    void signal_durationChanged(int duration);          // Play song length conversion signal
    void signal_positionChanged(int value);             // Player song position ratio conversion, 1s once
    void signal_finished();                             // Play completion signal
    void signal_mediaInfo(MplayerManager::MediaInfo mediaInfo);     // Various meta information obtained when playing songs

public:
    void startPlay(QString filePath);       // Play the specified song (after calling, or send a message to the playback thread,
                                            // The following four functions also essentially call slo_xxxx
    void startPlay();                       // Play songs
    void pausePlay();                       // Pause playback
    void resumePlay();                      // Resume playback
    void stopPlay();                        // stop playing

public slots:
    void slot_start();                      // Thread on (requires external management QThread)
    void slot_stop();                       // Thread stop
    void slot_startPlay();                  // Start playing
    void slot_pausePlay();                  // Pause playback
    void slot_resumePlay();                 // Resume playback
    void slot_stopPlay();                   // stop playing
    void slot_setPosition(int position);    // Set position
    void slot_setVolume(int volume);        // set volume 
    void slot_setMute(bool mute);           // Set mute
    void slot_getCurrentPosition();         // Get the current position (after calling, the current playback position signal will be forced to be thrown immediately)

protected slots:
    void slot_readyRead();
    void slot_finished(int exitCode, QProcess::ExitStatus exitStatus);

protected:
    void timerEvent(QTimerEvent *event);

private:
    bool _runnig;                   // Run
    QProcess *_pProcess;            // Process control
    QTextCodec *_pTextCodec;        // code

private:
    QString _filePath;              // Playback file path
    QString _mplayerPath;           // mplayer executor

private:
    PLAY_STATE _playState;          // Current playback status
    int _position;                  // Current playback position, unit: s
    bool _mute;                     // Mute
    int _volume;                    // Current volume 0 ~ 100
    int _duration;                  // The length of the current song, in seconds
    int _timerId;                   // Timer to obtain the current playback time
    MediaInfo _mediaInfo;           // Media information when playing songs
};

#endif // MPLAYERMANAGER_H

Engineering formwork

  mplayerDemo_ Foundation template_ v1.0.0.rar


If the article is original, please indicate the source of the original
This blog address: https://hpzwl.blog.csdn.net/article/details/118713520

Topics: Qt