qt调用mplayer

来源:互联网 发布:淘宝网店快递怎么合作 编辑:程序博客网 时间:2024/06/05 10:27
#ifndef MOVIEPLAY_H#define MOVIEPLAY_H#include <QPushButton>#include <QWidget>#include <QSlider>#include <QListWidget>#include <QListWidgetItem>#include <QStringList>#include <QString>#include <QDir>#include <QProcess>#include <QTimer>#include <QCloseEvent>#include <QLabel>class MoviePlay:public QWidget{Q_OBJECTpublic:QPushButton *play;QPushButton *stop;QPushButton *forward;QPushButton *rewind;QPushButton *come_back;QLabel *v_label;QLabel *list_label;QProcess *mplayer;QStringList args;QSlider *volumeBar;QTimer *timer;QSlider *timeBar;QListWidget *listWidget;QDir *dir;QStringList *fileList;QString *file;/******************************************************************/QLabel *length;QLabel *curr;QPushButton *ok;/******************************************************************/int number;int status;int currentTime;int timeLength;int volume;MoviePlay(QWidget *parent=0);~MoviePlay();const QString deleteSuffix(const QString &str);signals:void end();public slots:void _play();void _stop();void _rewind();void _forward();void selectFile(QListWidgetItem * item);void playFinished(int,QProcess::ExitStatus);void parseOutput();void getCurrentTime();void setVolume(int);/******************************************************************/void _ok();/******************************************************************/void end_slot();protected:void closeEvent(QCloseEvent *e);};#endif

以上是头文件里面都文件里面类的定义


下面是具体实现


#include "musicplay.h"#include <QString>#include <iostream>const QString DIR = "/home/music";//const QString DIR = "/root/all_show/music";const QString ICON = "/home/app.png";//const QString ICON = "/root/all_show/music/app.png";const QString PATH = "/home/mplayer";//const QString PATH = "/usr/local/mplayer/bin/mplayer";const int MAX_VOLUME = 25;const int WIDTH = 320;const int HEIGHT = 240;const int BUTTON_WIDTH = 40;const int BUTTON_HEIGHT = 40;MusicPlay::MusicPlay(QWidget *parent):QWidget(parent){status = 0;row = 2;mode = 1;volume = 25;nextEnable = false;this->resize(WIDTH,HEIGHT);path = new QString(PATH);args<<"-slave"<<"-quiet"<<"-ac"<<"mad";mplayer = new QProcess(this);connect(mplayer,SIGNAL(readyReadStandardOutput()),this,SLOT(parseOutput()));timer = new QTimer(this);connect(timer,SIGNAL(timeout()),this,SLOT(getCurrentTime()));modeComboBox = new QComboBox(this);modeComboBox->clear();modeComboBox->insertItem(0,QString::fromUtf8("单曲播放"));modeComboBox->insertItem(1,QString::fromUtf8("单曲循环"));modeComboBox->insertItem(2,QString::fromUtf8("随机播放"));modeComboBox->insertItem(3,QString::fromUtf8("全部重复"));modeComboBox->setGeometry(QRect(260,1,80,20));connect(modeComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(changeMode(int)));comeBack = new QPushButton(QString::fromUtf8("返回"),this);    comeBack->setGeometry(QRect(290, 225, 30, 15));connect(comeBack,SIGNAL(clicked()),this,SLOT(end_slot()));playList = new QPushButton(QString::fromUtf8("列表"),this);playList->setGeometry(0,225,30,15);connect(playList,SIGNAL(clicked()),this,SLOT(showPlayList()));play = new QPushButton(QString::fromUtf8("播放"),this);play->setGeometry(QRect(95,150,30,20));connect(play,SIGNAL(clicked()),this,SLOT(_play()));forward = new QPushButton(QString::fromUtf8("快进"),this);forward->setGeometry(QRect(190,138,30,30));connect(forward,SIGNAL(clicked()),this,SLOT(_forward()));stop = new QPushButton(QString::fromUtf8("停止"),this);stop->setGeometry(QRect(147,195,30,20));connect(stop,SIGNAL(clicked()),this,SLOT(_stop()));rewind = new QPushButton(QString::fromUtf8("快退"),this); rewind->setGeometry(QRect(25,104,30,30));connect(rewind,SIGNAL(clicked()),this,SLOT(_rewind()));nextOne = new QPushButton(QString::fromUtf8("下一曲"),this);nextOne->setGeometry(QRect(228,155,BUTTON_WIDTH,BUTTON_HEIGHT));connect(nextOne,SIGNAL(clicked()),this,SLOT(next()));previousOne = new QPushButton(QString::fromUtf8("上一曲"),this);previousOne->setGeometry(QRect(10,155,BUTTON_WIDTH,BUTTON_HEIGHT));connect(previousOne,SIGNAL(clicked()),this,SLOT(previous()));timeBar = new QSlider(this);timeBar->setGeometry(QRect(0, 10, 250, 20));timeBar->setOrientation(Qt::Horizontal);v_label=new QLabel(QString::fromUtf8("音量"),this);v_label->setGeometry(290,130,25,15);volumeBar = new QSlider(this);volumeBar->setGeometry(QRect(295,150,20,65));volumeBar->setOrientation(Qt::Vertical);volumeBar->setRange(0,100);volumeBar->setValue(50);connect(volumeBar,SIGNAL(valueChanged(int)),this,SLOT(setVolume(int)));listWidget = new QListWidget(this);listWidget->hide();listWidget->setGeometry(QRect(0,0,240,320));/*set location and size*/listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);/*unable horizontalscroll*/listWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);/*unable edit*/dir = new QDir(DIR);fileList = new QStringList(dir->entryList(QDir::Files));number = fileList->size();for(int i = 0 ;i<fileList->size() ;i++ )listWidget->addItem(new QListWidgetItem(QIcon(ICON),deleteSuffix(fileList->at(i))));file = QString(fileList->at(0));connect(listWidget,SIGNAL(itemDoubleClicked(QListWidgetItem *)),this,SLOT(getItem(QListWidgetItem *)));connect(this,SIGNAL(sendFileID(int)),this,SLOT(setFileName(int)));connect(mplayer,SIGNAL(finished(int, QProcess::ExitStatus)),this,SLOT(next1(int,QProcess::ExitStatus)));        name = new QLabel(this);        name->setObjectName(QString::fromUtf8("name"));        name->setGeometry(QRect(0, 30, 320, 30));name->setText(file);name->setAlignment(Qt::AlignCenter);QFont f(QString::fromUtf8("DejaVu Sans Mono"),9);f.setBold(false);length = new QLabel(this);length->setGeometry(QRect(220,30,40,10));length->setAlignment(Qt::AlignLeft|Qt::AlignTrailing|Qt::AlignVCenter);length->setFont(f);curr = new QLabel(this);curr->setGeometry(QRect(185,30,40,10));curr->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);curr->setFont(f);/******************************************************************/play->setStyleSheet("border-style:none;border-radius:30px;color:blue;");    forward->setStyleSheet("border-style:none;border-radius:30px;color:blue;");stop->setStyleSheet("border-style:none;border-radius:30px;color:blue;");rewind->setStyleSheet("border-style:none;border-radius:30px;color:blue;");playList->setStyleSheet("border-style:none;color:white;");nextOne->setStyleSheet("border-style:solid;border-width:2px;color:white;");previousOne->setStyleSheet("border-style:solid;border-width:2px;color:white;");comeBack->setStyleSheet("border-style:none;color:white;");name->setStyleSheet("color:red");modeComboBox->setStyleSheet("border-style:none");}MusicPlay::~MusicPlay(){}void MusicPlay::closeEvent(QCloseEvent *e){_stop();mplayer->waitForFinished();e->accept();}void MusicPlay::_play(){if(status==0){timer->start(100);status = 1;mplayer->start(*path,QStringList(args)<<(QString(DIR).append("/").append(file)),QIODevice::ReadWrite);name->setText(QString(file));mplayer->waitForStarted();mplayer->write("get_time_length\n");play->setText(QString::fromUtf8("暂停"));setVolume(volumeBar->value());return ;}if(status==1)//to pause{status = 2;mplayer->write("pause\n");play->setText(QString::fromUtf8("播放"));return ;}if(status==2)//to continue{status = 1;mplayer->write("pause\n");if(volume!=volumeBar->value())setVolume(volumeBar->value());play->setText(QString::fromUtf8("暂停"));return ;}}void MusicPlay::_stop(){if(status==0)return;status = 0;mplayer->write("quit\n");play->setText(QString::fromUtf8("播放"));timeBar->setValue(0);timer->stop();}void MusicPlay::_rewind(){if(status==1)mplayer->write("seek -5\n");}void MusicPlay::_forward(){if(status==1)mplayer->write("seek +5\n");}void MusicPlay::next1(int exitCode, QProcess::ExitStatus exitStatus){if(nextEnable){switch(mode){case 1:break;case 2:_play();break;case 3:setFileName((rand()%number));_play();break;case 4:setFileName(++row);_play();break;default:break;}nextEnable = false;}}void MusicPlay::next(){switch(mode){case 3:setFileName(rand()%number);break;default:setFileName(++row);break;}if(status==1){mplayer->write("quit\n");mplayer->waitForFinished();mplayer->start(*path,QStringList(args)<<(QString(DIR).append("/").append(file)),QIODevice::ReadWrite);name->setText(QString(file));mplayer->waitForStarted();/***************************************************************/setVolume(volumeBar->value());/***************************************************************/mplayer->write("get_time_length\n");}}void MusicPlay::previous(){switch(mode){case 3:setFileName(rand()%number);break;default:setFileName(--row);break;}if(status==1){mplayer->write("quit\n");mplayer->waitForFinished();mplayer->start(*path,QStringList(args)<<(QString(DIR).append("/").append(file)),QIODevice::ReadWrite);name->setText(QString(file));mplayer->waitForStarted();/***************************************************************/setVolume(volumeBar->value());/***************************************************************/mplayer->write("get_time_length\n");}}void MusicPlay::changeMode(int index){mode = index+1;}void MusicPlay::setVolume(int v){if(status==1){QString temp("volume ");temp.append(QString::number(v)).append(" 1\n");mplayer->write(temp.toLocal8Bit().data());volume = v;}}void MusicPlay::getItem(QListWidgetItem *item1){row = listWidget->row(item1);emit sendFileID(row);showPlayList();}void MusicPlay::showPlayList(){if(!listWidget->isVisible()){listWidget->show();play->hide();forward->hide();stop->hide();rewind->hide();nextOne->hide();previousOne->hide();name->hide();timeBar->hide();volumeBar->hide();modeComboBox->hide();v_label->hide();}else{listWidget->hide();play->show();forward->show();stop->show();rewind->show();nextOne->show();previousOne->show();name->show();timeBar->show();volumeBar->show();modeComboBox->show();v_label->show();}}void MusicPlay::setFileName(int row1){if(row1<0)row = number-1;elserow = row1%number;file = QString(fileList->at(row));}void MusicPlay::parseOutput(){while(mplayer->canReadLine()){QByteArray *buffer = new QByteArray(mplayer->readLine());/**************************************************************/int temp;/**************************************************************/if(buffer->startsWith("ANS_LENGTH")){buffer->remove(0, 11); buffer->replace(QByteArray("'"), QByteArray(""));buffer->replace(QByteArray(" "), QByteArray(""));buffer->replace(QByteArray("\n"), QByteArray(""));buffer->replace(QByteArray("\r"), QByteArray(""));timeLength = static_cast<int>(buffer->toFloat()*10);timeBar->setRange(0,timeLength);/***********************************************************/temp = timeLength/10;QString str("/");str.append(QString::number(temp/60));str.append(":").append(QString::number(temp%60));length->setText(str);/***********************************************************/}else{if(buffer->startsWith("ANS_TIME_POSITION")){buffer->remove(0, 18); buffer->replace(QByteArray("'"), QByteArray(""));buffer->replace(QByteArray(" "), QByteArray(""));buffer->replace(QByteArray("\n"), QByteArray(""));buffer->replace(QByteArray("\r"), QByteArray(""));currentTime = static_cast<int>(buffer->toFloat()*10);timeBar->setValue(currentTime);/*******************************************************/temp = currentTime/10;QString str(QString::number(temp/60));str.append(":").append(QString::number(temp%60));curr->setText(str);/*******************************************************/if((currentTime+5)>timeLength){nextEnable = true;_stop();}}}}}const QString MusicPlay::deleteSuffix(const QString &str){QString temp(str);int lastDot = temp.lastIndexOf(".");int length = temp.length();temp.remove(lastDot,length);return temp;}void MusicPlay::getCurrentTime(){if(status==1)mplayer->write("get_time_pos\n");}///////////////////////////////////////////void MusicPlay::end_slot(){mplayer->close();emit end();}


原创粉丝点击