程序笔记   发布时间:2022-07-17  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Qt+MPlayer音乐播放器开发笔记(一):ubuntu上编译MPlayer以及Demo演示大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

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

 

Demo

  

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

  

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

  

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

  

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

  

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

 
@H_492_1@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

  

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

步骤二:configure

cd MPlayer-1.4/
./configure

  

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

./configure --yasm=’’

  

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

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

@H_432_71@make

  

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

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

sudo ldconfig

步骤四:继续编译make

@H_432_71@make

  

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

步骤五:安装sudo make install

sudo make install

  

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

步骤六:播放测试

  

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

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

 

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);
}
@H_964_5@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);       // 播放指定歌曲(调用后,或发送消息给播放线程,
                                            // 以下@R_197_10401@同样,本质调用了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

 

若该文为原创文章,转载请注明原文出处本文章博客地址:https://hpzwl.blog.csdn.net/article/details/118713520

大佬总结

以上是大佬教程为你收集整理的Qt+MPlayer音乐播放器开发笔记(一):ubuntu上编译MPlayer以及Demo演示全部内容,希望文章能够帮你解决Qt+MPlayer音乐播放器开发笔记(一):ubuntu上编译MPlayer以及Demo演示所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。