Qt+MPlayer音乐播放器开发笔记(一):ubuntu上编译MPlayer以及Demo演示

简介: Qt+MPlayer音乐播放器开发笔记(一):ubuntu上编译MPlayer以及Demo演示

前言

  在ubuntu上实现MPlayer播放器播放音乐。


Demo

  

  

  

  

  


Mplayer

  MPlayer是一款开源多媒体播放器,以GNU通用公共许可证发布。此款软件可在各主流操作系统使用,例如Linux和其他类Unix系统、Windows及Mac OS X系统。

  MPlayer基于命令行界面,在各操作系统也可选择安装不同的图形界面。mplayer的另一个大的特色是广泛的输出设备支持。它可以在X11、Xv、DGA、OpenGL、SVGAlib、fbdev、AAlib、DirectFB下工作,且能使用GGI和SDL和一些低级的硬件相关的驱动模式(比如Matrox、3Dfx和Radeon、Mach64、Permedia3)。MPlayer还支持通过硬件MPEG解码卡显示,如DVB 和DXR3与Hollywood+。

  MPlayer的开发始于2000年。最初的作者是 Arpad Gereoffy。MPlayer最初的名字叫"MPlayer - The Movie Player for Linux",不过后来开发者们简称其为"MPlayer - The Movie Player",原因是MPlayer已经不仅可以用于Linux而可以在所有平台上运行。

下载

  最新源码下载地址: http://mplayerhq.hu/design7/news-archive.html

  QQ群:1047134658(点击“文件”搜索“MPlayer”,群内与博文同步更新)


Ubuntu编译

步骤一:下载解压

tar xvf MPlayer-1.4.tar.xz

  

步骤二:configure

cd MPlayer-1.4/
./configure

  

./configure --yasm=’’

  

步骤三:make,需要zlib库支撑,编译zlib库

make

  

  需要编译zlib库,需要先编译,请参照博文《libzip开发笔记(二):libzip库介绍、ubuntu平台编译和工程模板》。

sudo ldconfig

步骤四:继续编译make

make

  

步骤五:安装sudo make install

sudo make install

  

步骤六:播放测试

  

  (注意:若是虚拟机,虚拟机的音量和选用主机的声卡需要选择下)


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播放器 %1 (长沙红胖子网络科技有限公司 QQ:21497936 微信:yangsir198808 公司网址: hpzwl.blog.csdn.net)").arg(version));
    // 初始化modbus线程
    _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
/************************************************************\
 * 控件名称: mplayer管理类
 * 控件描述:
 *          使用slave模式控制mplayer播放音乐,基础模块实现单曲播放
 * 控件功能:
 *          1.音乐播放器播放音乐的基础操作
 *          2.可以获取歌曲的相关专辑,作者,年代,评论,流派等信息
 * 著作权信息
 *      作者:红胖子(AAA红模仿)
 *      公司:长沙红胖子网络科技有限公司
 *      网址:hpzwl.blog.csdn.net
 *      联系方式:QQ(21497936) 微信(yangsir198808) 电话(15173255813)
 * 版本信息
 *       日期             版本           描述
 *   2021年07月12日      v1.0.0        基础模板
\************************************************************/
#include <QObject>
#include <QThread>
#include <QProcess>
#include <QtMath>
#include <QTextCodec>
class MplayerManager : public QObject
{
    Q_OBJECT
public:
    enum PLAY_STATE {                   // 播放状态
        PLAY_STATE_STOP = 0x00,         // 未播放,停止播放
        PLAY_STATE_PLAY,                // 正在播放
        PLAY_STATE_PAUSE                // 暂停播放
    };
    struct MediaInfo {                  // 多媒体信息
        MediaInfo() {}
        QString title;                  // 标题
        QString artist;                 // 艺术家
        QString album;                  // 专辑
        QString year;                   // 年代
        QString comment;                // 评论
        QString genre;                  // 流派
    };
public:
    explicit MplayerManager(QObject *parent = 0);
    ~MplayerManager();
public:
    QString getMplayerPath()    const;      // 获取播放器路径(运行则可调用)
    QString getFilePath()       const;      // 获取当前播放文件路径
    bool getMute()              const;      // 获取是否静音
    int getVolume()             const;      // 获取音量
    int getPosition()           const;      // 获取当前位置
public:
    void setMplayerPath(const QString &mplayerPath);    // 设置播放器路径
    void setFilePath(const QString &filePath);          // 设置播放文件
    void setMute(bool mute);                            // 设置静音
    void setVolume(int volume);                         // 设置音量(0~100)
    void setPosition(int position);                     // 设置当前播放位置
signals:
    void signal_stateChanged(PLAY_STATE playState);     // 播放器播放状态信号
    void signal_durationChanged(int duration);          // 播放歌曲长度变换信号
    void signal_positionChanged(int value);             // 播放器歌曲位置比变换,1s一次
    void signal_finished();                             // 播放完成信号
    void signal_mediaInfo(MplayerManager::MediaInfo mediaInfo);     // 播放歌曲时,获取的各种元信息
public:
    void startPlay(QString filePath);       // 播放指定歌曲(调用后,或发送消息给播放线程,
                                            // 以下4个函数同样,本质调用了slo_xxxx
    void startPlay();                       // 播放歌曲
    void pausePlay();                       // 暂停播放
    void resumePlay();                      // 恢复播放
    void stopPlay();                        // 停止播放
public slots:
    void slot_start();                      // 线程开启(需要外部管理QThread)
    void slot_stop();                       // 线程停止
    void slot_startPlay();                  // 开始播放
    void slot_pausePlay();                  // 暂停播放
    void slot_resumePlay();                 // 恢复播放
    void slot_stopPlay();                   // 停止播放
    void slot_setPosition(int position);    // 设置位置
    void slot_setVolume(int volume);        // 设置音量
    void slot_setMute(bool mute);           // 设置静音
    void slot_getCurrentPosition();         // 获取当前位置(调用后,会强制立即抛出当前播放位置信号)
protected slots:
    void slot_readyRead();
    void slot_finished(int exitCode, QProcess::ExitStatus exitStatus);
protected:
    void timerEvent(QTimerEvent *event);
private:
    bool _runnig;                   // 是否运行
    QProcess *_pProcess;            // 进程控制
    QTextCodec *_pTextCodec;        // 编码
private:
    QString _filePath;              // 播放文件路径
    QString _mplayerPath;           // mplayer执行程序
private:
    PLAY_STATE _playState;          // 当前播放状态
    int _position;                  // 当前播放位置,单位s
    bool _mute;                     // 是否静音
    int _volume;                    // 当前音量0~100
    int _duration;                  // 当前歌曲的长度,单位s
    int _timerId;                   // 定时器,获取当前播放时间
    MediaInfo _mediaInfo;           // 播放歌曲时候的媒体信息
};
#endif // MPLAYERMANAGER_H


工程模板

  mplayerDemo_基础模板_v1.0.0.rar


相关文章
|
6天前
|
Ubuntu 数据可视化 开发工具
【VTK】ubuntu手动编译VTK9.3 Generating qmltypes file 失败
通过以上步骤,您可以成功解决在Ubuntu上编译VTK 9.3时遇到的 `Generating qmltypes file`失败的问题。关键在于确保系统正确安装了所需的Qt库,并通过CMake配置正确的路径。编译完成后,您将拥有一个功能完备的VTK库,可以用于各种可视化任务。
36 14
|
2月前
|
Ubuntu 开发工具 git
Ubuntu编译ffmpeg解决错误:ERROR: avisynth/avisynth_c.h not found
通过本文的详细指导,您可以顺利地在Ubuntu系统上配置和编译FFmpeg,并解决Avisynth头文件缺失的问题。
144 27
|
2月前
|
Ubuntu 计算机视觉 C++
Ubuntu系统下编译OpenCV4.8源码
通过上述步骤,你可以在Ubuntu系统上成功编译并安装OpenCV 4.8。这种方法不仅使你能够定制OpenCV的功能,还可以优化性能以满足特定需求。确保按照每一步进行操作,以避免常见的编译问题。
92 43
|
2月前
|
Ubuntu 计算机视觉 C++
Ubuntu系统下编译OpenCV4.8源码
通过上述步骤,你可以在Ubuntu系统上成功编译并安装OpenCV 4.8。这种方法不仅使你能够定制OpenCV的功能,还可以优化性能以满足特定需求。确保按照每一步进行操作,以避免常见的编译问题。
92 30
|
9天前
|
Ubuntu NoSQL JavaScript
在Ubuntu上安装MEAN Stack的4个步骤
本指南介绍了在Ubuntu上安装MEAN Stack的四个步骤。MEAN Stack是一种基于JavaScript的开发堆栈,包含MongoDB、ExpressJS、AngularJS和NodeJS。步骤包括:1. 更新系统并准备安装MEAN;2. 从官方源安装最新版MongoDB;3. 安装NodeJS、Git和NPM;4. 克隆mean.io仓库并使用NPM安装剩余依赖项。通过这些步骤,您可以快速搭建基于MEAN Stack的应用开发环境。
24 2
|
1天前
|
Ubuntu 安全 调度
在Ubuntu下安装Debian包:dpkg与apt命令的深度解构。
安装Debian包的知识,就像掌握了海上的航行技术,虽然起初会让人感到陌生甚至困惑,但只要你积累熟练,就能在Ubuntu的世界里畅游无阻。就像每一位成功的航海家,掌握好这些工具,去探索属于你的Ubuntu新世界吧!
47 21
|
7天前
|
Ubuntu Linux Shell
Ubuntu gnome WhiteSur-gtk-theme类mac主题正确安装和卸载方式
通过这个过程,用户不仅可以定制自己的桌面外观,还可以学习到更多关于 Linux 系统管理的知识,从而更好地掌握系统配置和主题管理的技巧。
41 12
|
1月前
|
缓存 Ubuntu Linux
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
154 25
|
16天前
|
NoSQL Ubuntu 网络安全
在 Ubuntu 20.04 上安装和配置 Redis
在 Ubuntu 20.04 上安装和配置 Redis 的步骤如下:首先更新系统包,然后通过 `apt` 安装 Redis。安装后,启用并启动 Redis 服务,检查其运行状态。可选配置包括修改绑定 IP、端口等,并确保防火墙设置允许外部访问。最后,使用 `redis-cli` 测试 Redis 功能,如设置和获取键值对。
29 1
|
22天前
|
Ubuntu TensorFlow 算法框架/工具
如何在Ubuntu上安装TensorFlow 24.04
如何在Ubuntu上安装TensorFlow 24.04
43 1