前言

  在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

 

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

Qt+MPlayer音乐播放器开发笔记(一):ubuntu上编译MPlayer以及Demo演示的更多相关文章

  1. android音乐播放器开发教程

    android音乐播放器开发教程 Android扫描sd卡和系统文件 Android 关于录音文件的编解码 实现米聊 微信一类的录音上传的功能 android操作sdcard中的多媒体文件——音乐列表 ...

  2. 仿酷狗音乐播放器开发日志十九——CTreeNodeUI的bug修复二(附源码)

    转载请说明原出处,谢谢 今天本来打算把仿酷狗播放列表的子控件拖动插入功能做一下,但是仔细使用播放列表控件时发现了几个逻辑错误,由于我的播放 列表控件是基于CTreeViewUI和CTreeNodeUI ...

  3. 仿酷狗音乐播放器开发日志二十四 选项设置窗体的实现(附328行xml布局源码)

    转载请说明原出处,谢谢~~ 花了两天时间把仿酷狗的选项设置窗体做出来了,当然了只是做了外观.现在开学了,写代码的时间减少,所以整个仿酷狗的工程开发速度减慢了.今天把仿酷狗的选项设置窗体的布局代码分享出 ...

  4. Android音乐播放器开发

    今日看书,看到这个播放器,我就写了个例子,感觉还行,这个播放器能播放后缀是.MP3的音乐,这个例子在main.xml设置listView的时候,注意:android:id="@+id/and ...

  5. Redrain仿酷狗音乐播放器开发完毕,发布测试程序

    转载请说明原出处,谢谢~~ 从暑假到现在中秋刚过,我用duilib开发仿酷狗播放器大概经历了50天.做仿酷狗的意图只是看原酷狗的界面比较漂亮,想做个完整一些的工程来练习一下duilib.今天把写好的程 ...

  6. 仿酷狗音乐播放器开发日志二十七 用ole为窗体增加文件拖动功能(附源码)

    转载请说明原出处,谢谢~~ 中秋到了,出去玩了几天.今天把仿酷狗程序做了收尾,已经开发完成了,下一篇博客把完结的情况说一下.在这篇博客里说一下使用OLE为窗体增加文件拖拽的功能.使用播放器,我更喜欢直 ...

  7. android音乐播放器开发 SweetMusicPlayer 播放本地音乐

    上一篇写了载入歌曲列表,http://blog.csdn.net/huweigoodboy/article/details/39856411,如今来总结下播放本地音乐. 一,MediaPlayer 首 ...

  8. android音乐播放器开发 SweetMusicPlayer 实现思路

    一,实现效果 眼下还不是特别完好,主要有下面几个功能, 1,载入歌曲列表(实现a-z字母检索) 2,播放本地音乐 3.智能匹配本地歌词 4.智能载入在线歌词(事实上不算智能.发现歌词迷api提供的歌词 ...

  9. android音乐播放器开发 SweetMusicPlayer 载入歌曲列表

    上一篇写了播放器的总体实现思路,http://blog.csdn.net/huweigoodboy/article/details/39855653,如今来总结下载入歌曲列表. 代码地址:https: ...

随机推荐

  1. CUDA 8混合精度编程

    CUDA 8混合精度编程 Mixed-Precision Programming with CUDA 8 论文地址:https://devblogs.nvidia.com/mixed-precisio ...

  2. python_selenium_PO模式下显示等待、隐式等待封装,结合Excel读取元素可取默认等待时间配置

    basepage中等待的封装 def implicitly_wait(self): self.driver.implicitly_wait(5)def wait(self): time.sleep(5 ...

  3. 用 Flutter 和 Firebase 轻松构建 Web 应用

    作者 / Very Good Ventures Team 我们 (Very Good Ventures 团队) 与 Google 合作,在今年的 Google I/O 大会上推出了 照相亭互动体验 ( ...

  4. java容器学习笔记

    容器 容器的组成 容器有两个接口Map和Collection. collection接口有List类和set类. List类可以分为:Vector.LinkedList.ArrayList.CopyO ...

  5. [Linux]经典面试题 - 系统管理 - 备份策略

    [Linux]经典面试题 - 系统管理 - 备份策略 目录 [Linux]经典面试题 - 系统管理 - 备份策略 一.备份目录 1.1 系统目录 1.2 服务目录 二.备份策略 2.1 完整备份 2. ...

  6. Spring WebFlux 教程:如何构建反应式 Web 应用程序

    Spring WebFlux 教程:如何构建反应式 Web 应用程序 反应式系统提供了我们在高数据流世界中所需的无与伦比的响应能力和可扩展性.然而,反应式系统需要经过专门培训的工具和开发人员来实现这些 ...

  7. 关于MySql数据库误操作数据找回的办法

    先讲个事,前段时间,系统长时间不用的一个功能被开放出来了,想当然的我没有在测试平台上测试,直接操作了正式系统(的确是我不严谨),导致好多数据异常,页面展示错乱了.于是我想到的第一个就是进行备份还原.项 ...

  8. Bert模型实现垃圾邮件分类

    近日,对近些年在NLP领域很火的BERT模型进行了学习,并进行实践.今天在这里做一下笔记. 本篇博客包含下列内容: BERT模型简介 概览 BERT模型结构 BERT项目学习及代码走读 项目基本特性介 ...

  9. 15 自动发布Java项目(Tomcat)

    #!/bin/bash export PAHT=/usr/local/maven/bin:/usr/local/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/ ...

  10. 适合企业的CRM系统选型法则?

    在市场竞争激烈的今天,企业需要找到一款好用的企业CRM系统来帮助维护客户关系,同时也能够帮助企业进行销售管理.营销管理,CRM可以说是当代企业管理的最强工具之一.那么适合企业的CRM客户管理系统要如何 ...