这段时间忙着找工作,好久没更新了,或许有点BUG,没得太多时间去精调,还请见谅,有意见请评论哦!
整个文件打包了,下载链接:http://download.csdn.net/download/wu9797/10043716
1、mplayer for windows安装和使用方法:http://blog.csdn.net/wu9797/article/details/78253548
2、mplayer for arm(已编译好,直接放在arm开发板即可使用):http://download.csdn.net/download/wu9797/10024789
一、工程结构
二、player.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-10-12T15:08:48
# Author : wsg
# Date :
# Version : 2.0
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = player
TEMPLATE = app
SOURCES += main.cpp\
player.cpp
HEADERS += player.h
FORMS += player.ui
DISTFILES += \
image/quiet -.png \
image/voice0.png \
image/voice1.png \
image/voice2.png \
image/voice3.png
RESOURCES += \
image/image.qrc
三、头文件player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <QMainWindow>
#include <QTimer>
#include <QProcess>
namespace Ui {
class Player;
}
class Player : public QMainWindow
{
Q_OBJECT
public:
explicit Player(QWidget *parent = 0);
~Player();
private slots:
void on_BackBt_clicked();
//定时刷新时间 槽函数
void update_time();
void up_dtime();
void read_data();
void on_addBt_clicked();
void on_delBt_clicked();
void on_movielist_clicked(const QModelIndex &index);
void on_pauseBt_clicked();
void on_forwdBt_2_clicked();
void on_novoiceButton_clicked();
void on_backwdBt_clicked();
void on_FullScreentoolBt_clicked();
void on_QuitBt_clicked();
void on_verticalSlider_valueChanged(int value);
void voiceIcoChanged();
void noviceIcoChanged();
void on_horizontalSlider_sliderReleased();
void on_horizontalSlider_sliderPressed();
void on_verticalSlider_sliderReleased();
void on_verticalSlider_sliderPressed();
private:
Ui::Player *ui;
// 时间与进程
QTimer *mtimer;
QTimer *dtimer;
QProcess *mprocess;
QString mpath;
QString msg;
bool playFlag;
};
#endif // PLAYER_H
四、源文件:main.cpp
#include "player.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Player w;
w.show();
return a.exec();
}
五、源文件:player.cpp
#include "player.h"
#include "ui_player.h"
#include <QDate> // 时间与日期
#include <QTime>
#include <QFileDialog> // 文件对话框
#include <QFileInfo>
#include <QModelIndex>
#include <QStringList>\
#include <QMessageBox>
#include <QDebug>
#define WIN_KEY // windows平台
Player::Player(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Player)
{
ui->setupUi(this);
#ifndef WIN_KEY
// 去掉标题栏边框...
this->setWindowFlags(this->windowFlags()|Qt::FramelessWindowHint);
#endif
// 定时器初始化,关联信号与槽函数,启动(视频时间)
mtimer = new QTimer();
connect(mtimer, SIGNAL(timeout()),this, SLOT(update_time()));
mtimer->start(1000);
// 日期和时间的设置(日期时间栏)
dtimer = new QTimer();
connect(dtimer, SIGNAL(timeout()), this, SLOT(up_dtime()));
dtimer->start(1000);
// 定义Qt中的进程
mprocess = new QProcess(this);
mprocess->setProcessChannelMode(QProcess::MergedChannels);
// 关联进程读取数据信号
connect(mprocess, SIGNAL(readyRead()), this, SLOT(read_data()));
\
connect(ui->verticalSlider, SIGNAL(valueChanged(int)), this, SLOT(voiceIcoChanged()));
connect(ui->novoiceButton, SIGNAL(clicked(bool)), this, SLOT(noviceIcoChanged()));
playFlag = false; // 初始化为暂停状态
}
Player::~Player()
{
delete ui;
}
void Player::voiceIcoChanged()
{
unsigned int value = ui->verticalSlider->value();
if (value == 0)
{
ui->voiceIcoLb->setStyleSheet("border-image: url(:/pic/voice0.png);");
}
else if (0 < value && value <= 33)
{
ui->voiceIcoLb->setStyleSheet("border-image: url(:/pic/voice1.png);");
}
else if (33 < value && value <= 66)
{
ui->voiceIcoLb->setStyleSheet("border-image: url(:/pic/voice2.png);");
}
else
{
ui->voiceIcoLb->setStyleSheet("border-image: url(:/pic/voice3.png);");
}
}
void Player::noviceIcoChanged()
{
if (ui->novoiceButton->isChecked() == true)
{
ui->quietLb->setStyleSheet("border-image: url(:/pic/quiet.png);");
}
else
{
ui->quietLb->setStyleSheet("border-image: url(:/pic/noquiet.png);");
}
}
void Player::update_time()
{
// 定时写获取时间命令
mprocess->write("get_time_length\n"); // 视频总时间
mprocess->write("get_time_pos\n"); // 视频当前时间
}
void Player::up_dtime()
{
// 设置dateLabel和timeLabel里的内容(日期+时间)
ui->dateLabel->setText(QDate::currentDate().toString("yyyy-MM-dd"));
ui->timeLabel->setText(QTime::currentTime().toString("hh:mm:ss"));
}
\
// *************************** 添加视频至列表中
void Player::on_addBt_clicked()
{
if (mprocess->state() == QProcess::Running) // 若视频在运行,则暂停
{
//if (ui->pauseBt->text() == "||")
if (playFlag == true)
{
mprocess->write("pause\n");
ui->pauseBt->setText("►");
mtimer->stop(); // 播放时间暂停
playFlag = false;
//update();
}
}
QString title = tr("打开视频文件");
QString format = tr("voideo (*.mp4 *.rmvb *.mkv *.avi *.3gp *.mov)"); // 格式过滤
QStringList fileName = QFileDialog::getOpenFileNames(this, title,"./", format);
ui->movielist->addItems(fileName);
#ifdef CAN
// 判断是否存在相同文件
int row = 0;
QListWidgetItem *item = ui->movielist->takeItem(row);
qDebug()<<ui->movielist->count();
for (int row = 0; row < ui->movielist->count(); row++)
{
if (fileName == (QStringList)ui->movielist->item(row)->text()){}
else
{
ui->movielist->addItems(fileName);
}
}
#endif
}
// *************************** 删除某一项视频列表
void Player::on_delBt_clicked()
{
// 获取当前行号
int row = ui->movielist->currentRow();
QListWidgetItem *item = ui->movielist->takeItem(row);
delete item;
}
// *************************** 点击播放视频(通过Qt中的进程运行mplayer播放视频)
void Player::on_movielist_clicked(const QModelIndex &index)
{
int row = index.row(); // 当前行
// 获取视频路径
QString mpath = ui->movielist->item(row)->text();
// mplayer -slave -quiet -geometry 50:50 -zoom -x 400 -y 350 /mnt/mv.wmv
// 播放命令格式打包
const QString filePath_lin = "/bin/mplayer";
QString cmd_lin = QString("%1 -slave -quiet -geometry 0:0 -zoom -x %2 -y %3 %4")
.arg(filePath_lin)
.arg(QString::number(ui->playLabel->width())) // 宽和高的长度获取
.arg(QString::number(ui->playLabel->height()))
.arg(mpath);
#ifdef WIN_KEY
// windows下player.exe的路径 ---注意第一次使用mplayer的时候先在cmd命令行窗口运行一次,让系统配置。
// 详细情况请访问:http://blog.csdn.net/wu9797/article/details/78253548
const QString filePath_win = tr("cmd /k C:/MPlayer-for-windows/MPlayer/mplayer"); // 或者mplayer.exe
QString cmd_win = QString("%1 %2 -wid %3 -slave -quiet -zoom")
.arg(filePath_win)
.arg(mpath)
.arg(ui->playLabel->winId());
#endif
/* 启动进程播放 */
// 判断进程是否在运行,
if (mprocess->state() == QProcess::Running)
{
mprocess->kill(); // 播放状态就杀死进程
// 等待
mprocess->waitForFinished();
}
mtimer->stop();
// 开始播放
mprocess->start(cmd_lin);
// 音量初始化
mprocess->write(QString("volume %1 2\n").arg(ui->verticalSlider->value()).toUtf8());
// 判断是否为静音状态
if (ui->novoiceButton->isChecked() == true) // 选中状态
{
mprocess->write("mute 0\n"); // 0开启静音
}
// 定时器重启
mtimer->start(1000);
playFlag = true;
#ifdef WIN_KEY
mprocess->start(cmd_win);
#endif
if (ui->pauseBt->text() == "►")
{
ui->pauseBt->setText("||");
}
}
// *************************** 播放与暂停 pause
void Player::on_pauseBt_clicked()
{
if (playFlag == true) // 暂停
{
mprocess->write("pause\n");
ui->pauseBt->setText("►");
mtimer->stop(); // 播放时间暂停
playFlag = false;
// 禁用静音功能,除去BUG
ui->novoiceButton->setEnabled(false);
}
else
{ //播放
mprocess->write("pause\n");
ui->pauseBt->setText("||");
mtimer->start(1000);
playFlag = true;
// 启用静音功能
ui->novoiceButton->setEnabled(true);
}
}
// *************************** “<” 键播放上一个视频
void Player::on_backwdBt_clicked()
{
// 获取上一个视频路径
int row = ui->movielist->currentRow(); // 当前行号
if (row == 0)
row = ui->movielist->count()-1; // 把row设置为最下面一个
else
row--;
// 当前行设置
ui->movielist->setCurrentRow(row);
// 调用点击播放函数
on_movielist_clicked(ui->movielist->currentIndex());
}
// *************************** “>”键播放下一个视频
void Player::on_forwdBt_2_clicked()
{
// 获取下一个视频路径
int row = ui->movielist->currentRow(); // 当前行
if (row == ui->movielist->count()-1)
row = 0;
else
row++;
ui->movielist->setCurrentRow(row);
// 调用点击播放函数
on_movielist_clicked(ui->movielist->currentIndex());
}
// *************************** 读取视频时间数据
void Player::read_data()
{
while(mprocess->canReadLine())
{
// 读取时间(视频总时间)
msg = mprocess->readLine();
// "ANS_LENGTH=238.20\nANS_TIME_POSITION=53.9\n"
if (msg.indexOf("ANS_LENGTH=") >= 0)
{
msg.remove("ANS_LENGTH=");
msg.remove("\n");
// 把msg秒数转换为hh:mm:ss格式
double all = msg.toDouble();
int iall = (int)all; // 得到总共秒数
// 设置进度条最大值
ui->horizontalSlider->setMaximum(iall);
QTime atime(0, 0, 0);
atime = atime.addSecs(iall);
ui->totalTimeLabel->setText(atime.toString("hh:mm:ss"));
}
// 当前视频播放时间
if (msg.indexOf("ANS_TIME_POSITION=") >= 0)
{
msg.remove("ANS_TIME_POSITION=");
msg.remove("\n");
// 把msg秒数转换为hh:mm:ss格式
double pos = msg.toDouble();
int ipos = (int)pos; // 得到总共秒数
// 更新进度条
ui->horizontalSlider->setValue(ipos);
QTime atime(0, 0, 0);
atime = atime.addSecs(ipos);
ui->curtTimeLabel->setText(atime.toString("hh:mm:ss"));
}
}
}
// *************************** 静音功能开关
void Player::on_novoiceButton_clicked()
{
if (ui->novoiceButton->isChecked() == false) // 未静音状态
{
mprocess->write("mute 0\n");
ui->quietLb->setStyleSheet("border-image: url(:/pic/quiet.png);");
}
if (ui->novoiceButton->isChecked() == true)
{
mprocess->write("mute 1\n");
ui->quietLb->setStyleSheet("border-image: url(:/pic/noquiet.png);");
}
}
// *************************** 全屏
void Player::on_FullScreentoolBt_clicked()
{
#ifdef WIN_KEY
mprocess->write("vo_fullscreen");
#endif
#ifndef WIN_KEY
QTime atime(0, 0, 0);
if (ui->FullScreentoolBt->text() == "<>")
{
if (msg.indexOf("ANS_TIME_POSITION=") >= 0)
{
msg.remove("ANS_TIME_POSITION=");
msg.remove("\n");
// 把msg秒数转换为hh:mm:ss格式
double pos = msg.toDouble();
int ipos = (int)pos; // 得到总共秒数
// 更新进度条
ui->horizontalSlider->setValue(ipos);
atime = atime.addSecs(ipos);
ui->curtTimeLabel->setText(atime.toString("hh:mm:ss"));
}
QString cmd = QString("/bin/mplayer -slave -quiet -geometry %4 -zoom -x %1 -y %2 %3")
.arg(tr("800")) // 宽和高的长度获取
.arg(tr("480"))
.arg(mpath)
.arg(atime.toString("mm:ss"));
mprocess->close();
mprocess->kill();
mprocess->waitForFinished();
mprocess->start(cmd);
ui->FullScreentoolBt->setText("><");
update();
}
else
{
if (msg.indexOf("ANS_TIME_POSITION=") >= 0)
{
msg.remove("ANS_TIME_POSITION=");
msg.remove("\n");
// 把msg秒数转换为hh:mm:ss格式
double pos = msg.toDouble();
int ipos = (int)pos; // 得到总共秒数
// 更新进度条
ui->horizontalSlider->setValue(ipos);
atime = atime.addSecs(ipos);
ui->curtTimeLabel->setText(atime.toString("mm:ss"));
}
QString cmd = QString("/bin/mplayer -slave -quiet -geometry %4 -zoom -x %1 -y %2 %3")
.arg(ui->playLabel->width()) // 宽和高的长度获取
.arg(ui->playLabel->height())
.arg(mpath)
.arg(atime.toString("hh:mm:ss"));
mprocess->close();
mprocess->kill();
mprocess->waitForFinished();
mprocess->start(cmd);
ui->FullScreentoolBt->setText("<>");
update();
}
#endif
}
void Player::on_BackBt_clicked() // 退出
{
this->close();
}
// ************************** 拖动滑块快进
//void Player::on_horizontalSlider_valueChanged(int value)
//{
// mtimer->stop();
// ui->pauseBt->setText(tr("►"));
// const char *cmd = QString("seek %1 2\n").arg(value).toUtf8();
// mprocess->write(cmd);
// mtimer->start(1000);
// ui->pauseBt->setText("||");
//}
// ************************* 结束播放
void Player::on_QuitBt_clicked()
{
mprocess->write("quit");
mprocess->close();
update();
}
// ************************ 音量滑块
void Player::on_verticalSlider_valueChanged(int value)
{
const char *cmd = QString("volume %1 2\n").arg(value).toUtf8();
mprocess->write(cmd);
if (ui->novoiceButton->isChecked() == true)
{
mprocess->write("mute 0\n");
}
}
// ********************** 滑块拖动
void Player::on_horizontalSlider_sliderPressed()
{
mtimer->stop();
ui->pauseBt->setText(tr("►"));
}
void Player::on_horizontalSlider_sliderReleased()
{
int value = ui->horizontalSlider->value();
const char *cmd = QString("seek %1 2\n").arg(value).toUtf8();
mprocess->write(cmd);
mtimer->start(1000);
ui->pauseBt->setText("||");
}
void Player::on_verticalSlider_sliderReleased()
{
if (ui->novoiceButton->isChecked() == true)
{
mprocess->write("mute 0\n");
}
}
void Player::on_verticalSlider_sliderPressed()
{
if (ui->novoiceButton->isChecked() == true)
{
mprocess->write("mute 0\n");
}
}
六、界面
七、运行