好像不能上传附件。。。 幸好主要代码不是很多, 发个代码 以后自己复用!!!
这个Mplayer因为具体需要,功能不多。
功能: 1.切换显示图片,自动循环显示图片、播放音频、播放视频
2.点击窗口 直接退出
3.小键盘右键 播放下一文件
问题: 1.需要自己修改一些个性化设置,例如 开关的宏定义,关于一些mplayer目录,资源文件目录,图片位置等 还有后面的扫描目录需要自己添加后缀来过滤自己需要播放的文件。
2. 解码方面 都是mplayer自带的lib 还没有测试什么文件不能播, 不过确定了的SWF格式的不能播。。。
MyPlayer.h
#ifndef MYPLAYER_H #define MYPLAYER_H #include <qwidget.h> class QProcess; class QStringList; class QTimer; class CMyPlayer : public QWidget { Q_OBJECT public: CMyPlayer(QWidget *parent = 0,const char *name = 0); ~CMyPlayer(); bool eventFilter(QObject *obj, QEvent *k); public slots: void readProcess(); //读取mplayer信息 void playNextFile(); //播放currentFile号文件 protected: void SetupUi(); void InitList(); void PlayVideo(QString); void PlayAudio(QString); void PlayPicture(QString); void QuitWindow(); private: int fileType; //文件类型(暂时无用) int currentFile; //当前文件号 QProcess *playProcess; //文件播放进程 QStringList fileList; //资源文件列表 QTimer *playTimer; //图片切换显示定时器 }; #endif //end MYPLAYER_H
MyPlayer.cpp
#include "MyPlayer.h" #include <qdir.h> #include <qprocess.h> #include <qfileinfo.h> #include <qstringlist.h> #include <qdir.h> #include <qlayout.h> #include <qpushbutton.h> #include <qpixmap.h> #include <qtimer.h> #define SOURCE_DIR_PATH "/home/yang/" //资源文件目录 #define MPLAYER_PATH "/usr/local/mplayer/bin/mplayer" //mplayer目录 #define BACKGROUND_PIC "/home/yang/MyPlayer/admin_body_bg.png" //音乐播放时显示的背景图片 CMyPlayer::CMyPlayer(QWidget *parent,const char *name): QWidget(parent,name, Qt::WStyle_Customize | Qt::WStyle_NoBorder) { currentFile = 0; SetupUi(); installEventFilter(this); //为本窗口安装事件过滤器 playNextFile(); } CMyPlayer::~CMyPlayer() { } //初始化界面元素 void CMyPlayer::SetupUi() { setFixedSize(600,600); //设置窗口大小 playProcess = new QProcess(this); connect(playProcess,SIGNAL(readyReadStdout()),this,SLOT(readProcess())); connect(playProcess,SIGNAL(readyReadStderr()),this,SLOT(readProcess())); connect(playProcess,SIGNAL(processExited()),this,SLOT(playNextFile())); playTimer = new QTimer(this); connect(playTimer,SIGNAL(timeout()),this,SLOT(playNextFile())); InitList(); } //初始化播放列表 void CMyPlayer::InitList() { QString dirPath(SOURCE_DIR_PATH); QDir fileDir(dirPath); if(!fileDir.isReadable()) { qDebug("Error: Dir can't read/n"); return; } fileDir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks); fileDir.setSorting(QDir::Size | QDir::Reversed); fileDir.setNameFilter("*.avi;*mp3;*jpg;*png"); fileList = fileDir.entryList(); currentFile = 0; } //播放列表下一个文件 void CMyPlayer::playNextFile() { qDebug("begin playNextFile..."); QStringList::Iterator it = fileList.begin(); int tempId = 0; while(tempId++ < currentFile) it++; if((currentFile = tempId) == fileList.size()) //currentFile += 1 currentFile = 0; QFileInfo * fi = new QFileInfo(*it); qDebug(QString("play the file : %1 ").arg(*it)); QString ext = fi->extension(FALSE); if(ext.compare("avi") == 0) { if(playTimer->isActive()) playTimer->stop(); PlayVideo(fi->fileName()); //如果用absFilePath会是本程序的目录加上文件名,所以只能传文件名,后面使用时再加上路径 } else if(ext.compare("mp3") == 0) { if(playTimer->isActive()) playTimer->stop(); PlayAudio(fi->fileName()); } else if(ext.compare("jpg") == 0 || ext.compare("png") == 0) { if(!playTimer->isActive()) playTimer->start(3000); PlayPicture(fi->fileName()); } qDebug("end autoPlay..."); } //读取mplayer返回的信息 void CMyPlayer::readProcess() { while(playProcess->canReadLineStdout() || playProcess->canReadLineStderr()) { QString buffer = playProcess->readStdout(); QString buffer1 = playProcess->readStderr(); if(!buffer.isEmpty()) qDebug(buffer); if(!buffer1.isEmpty()) qDebug(buffer1); } } //启动mplayer播放视频 void CMyPlayer::PlayVideo(QString fileName) { qDebug(QString("play Video : %1 ").arg(fileName)); QString realFileName = QString(SOURCE_DIR_PATH) + fileName; playProcess->clearArguments(); playProcess->addArgument(MPLAYER_PATH); playProcess->addArgument( "-quiet"); playProcess->addArgument( "-slave"); playProcess->addArgument("-wid"); playProcess->addArgument(QString("%1").arg(QString::number(this->winId()))); playProcess->addArgument(realFileName); playProcess->start(); if(playProcess->isRunning()) { qDebug("Video play begin..."); } } //启动mplayer播放音频 void CMyPlayer::PlayAudio(QString fileName) { qDebug(QString("play Audio : %1 ").arg(fileName)); QString realFileName = QString(SOURCE_DIR_PATH) + fileName; this->setPaletteBackgroundPixmap(QPixmap(BACKGROUND_PIC)); playProcess->clearArguments(); playProcess->addArgument(MPLAYER_PATH); playProcess->addArgument( "-quiet"); playProcess->addArgument( "-slave"); playProcess->addArgument(realFileName); playProcess->start(); if(playProcess->isRunning()) { qDebug("Audio play begin..."); } } //显示图片 void CMyPlayer::PlayPicture(QString fileName) { qDebug(QString("play Picture : %1 ").arg(fileName)); QString realFileName = QString(SOURCE_DIR_PATH) + fileName; this->setPaletteBackgroundPixmap(QPixmap(realFileName)); } //事件过滤处理 bool CMyPlayer::eventFilter(QObject *obj, QEvent *k) { if(k->type() == QEvent::MouseButtonPress) { int key = ((QMouseEvent *)k)->button(); if(key == Qt::LeftButton ) { qDebug("Left Mouse Button Press, quit..."); QuitWindow(); return true; } } else if(k->type() == QEvent::KeyPress) { int key = ((QKeyEvent *)k)->key(); if(key == Qt::Key_Right) { qDebug("Right Key Button Press, next File..."); if(playProcess->isRunning()) { playProcess->writeToStdin("quit/n"); qDebug("quit Process..."); } else { playNextFile(); qDebug("play net file.."); } return true; } } return QWidget::eventFilter(obj,k); } //退出 void CMyPlayer::QuitWindow() { qDebug("Quit Window"); playProcess->kill(); this->close(); }