QT+Mplayer简单视频播放器

Mplayer常用命令

loadfile name //加载媒体文件name
volume 100 1 //设置音量,中间位音量的大小0-100
mute 1/0 //静音开关
pause //播放/暂停
get_time_length //获取播放文件的长度,以秒为单位
seek value //定位,valu为秒数
get_percent_pos //获取播放的百分比
get_time_pos //获取当前播放的位置,以秒为单位
get_file_name //获取当前播放的媒体文件名
get_meta_album //获取专辑
get_meta_artist //获取艺术家
get_meta_comment //获取评论
get_meta_genre //获取流派
get_meta_title //获取标题
get_meta_year //获取年份

 

配置文件:config.h

#ifndef CONFIG_H
#define CONFIG_H
#define ISLINUX 0
#endif // CONFIG_H

头文件:video.h

#ifndef VIDEO_H
#define VIDEO_H
#include <QWidget>
#include <QList>
#include <QProcess>
#include "config.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Video; }
QT_END_NAMESPACE
class Video : public QWidget
{
  Q_OBJECT
public:
  Video(QWidget *parent = nullptr);
  ~Video();
private:
  void getVideo(void);
  void appExit(void);
  void stopVideo(void);
  void displayVideo(void);
  void mplayerDataRecieve(void);
private:
  Ui::Video *ui;
  QString videoPath =nullptr;
  QProcess *mplayerApp =nullptr;
  int screenWidth =0;;
  int screenHeight =0;
  bool videoIsRunningFlag = false;
};
#endif // VIDEO_H

源文件:video.cpp

#include "video.h"
#include "ui_video.h"
#include <QDebug>
#include <QScreen>
#include <QFileDialog>
#include <QApplication>
Video::Video(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Video)
{
  ui->setupUi(this);
  ui->video_play->setStyleSheet("background-color:black");
  mplayerApp = new QProcess(this);
#if ISLINUX
  QScreen *screen = QGuiApplication::primaryScreen();
  QSize screenSize = screen->size();
  screenWidth = screenSize.width();
  screenHeight = screenSize.height();
#else
  screenWidth = 480;
  screenHeight = 272;
#endif
  connect(ui->btn_check, &QPushButton::clicked, this, &Video::getVideo);
  connect(ui->btn_exit, &QPushButton::clicked, this, &Video::appExit);
  connect(ui->btn_stop, &QPushButton::clicked, this, &Video::stopVideo);
  connect(ui->btn_start, &QPushButton::clicked, this, &Video::displayVideo);
  connect(mplayerApp, &QProcess::readyReadStandardOutput, this, &Video::mplayerDataRecieve);
}
Video::~Video()
{
  delete ui;
  if(mplayerApp)
  {
    mplayerApp->kill();
    delete mplayerApp;
    mplayerApp =nullptr;
  }
}
void Video::stopVideo(void)
{
  if(videoIsRunningFlag)
  {
    mplayerApp->write("quit\n");
  }
  videoIsRunningFlag = false;
}
void Video::displayVideo(void)
{
  if(videoPath.isEmpty())
  {
    return;
  }
  QString videoWidth = QString("%1").arg(ui->video_play->size().width());
  QString videoHeight = QString("%1").arg(ui->video_play->size().height());
  QStringList args;
  mplayerApp->kill();
  args<<"-slave";
  args<<"-quiet";
  args<<"-wid"<<QString::number(ui->video_play->winId(),10);
  args<<"-zoom";
  args<<"-x";
  args<<videoWidth;
  args<<"-y";
  args<<videoHeight;
  args<<videoPath;
  mplayerApp->start("mplayer",args);
  videoIsRunningFlag = true;
}
void Video::appExit(void)
{
  this->close();
}
void Video::getVideo(void)
{
#if ISLINUX
  QString path = "/mnt/sdcard/video";
#else
  QString path = "./";
#endif
  //调整fileDialog大小以适应窗体
  QFileDialog videoDialog(this,"选择视频文件",path, "*.mp4 *.avi *.mp3");
  videoDialog.resize(screenWidth,screenHeight);
  videoDialog.setAcceptMode(QFileDialog::AcceptOpen);
  videoDialog.exec();
  QStringList files = videoDialog.selectedFiles();
  if(files.size())
  {
    videoPath =files.at(0);
    ui->video_play->setPixmap(QPixmap(":/img/video.png").scaled(ui->video_play->size()));
  }
  else
  {
    ui->video_play->setPixmap(QPixmap(":/img/AGV-TY.png").scaled(ui->video_play->size()));
  }
}
void Video::mplayerDataRecieve(void)
{
  mplayerApp->write("get_time_length\n");
  mplayerApp->write("get_time_pos\n");
  mplayerApp->write("get_percent_pos\n");
  while(mplayerApp->canReadLine())
  {
    QByteArray b=mplayerApp->readLine();
    if(b.startsWith("ANS_TIME_POSITION"))
    {
      b.replace(QByteArray("\n"),QByteArray(""));
      QString s(b);
    }
    else if((b.startsWith("ANS_LENGTH")))
    {
      b.replace(QByteArray("\n"),QByteArray(""));
      QString s(b);
    }
    else if((b.startsWith("ANS_PERCENT_POSITION")))
    {
      b.replace(QByteArray("\n"),QByteArray(""));
      QString s(b);
    }
  }
}

 

posted @ 2024-09-13 08:33  *^VV^*  阅读(28)  评论(0编辑  收藏  举报