下载

1下载券

加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 QT_播放器

QT_播放器.doc

QT_播放器

晓晨
2011-10-28 0人阅读 举报 0 0 暂无简介

简介:本文档为《QT_播放器doc》,可适用于IT/计算机领域

基于QT前端的mplayer播放器项目文档VPhper:dogwinQq:weblsfamilygmailcom基于QT前端的mplayer播放器项目文档V一、项目概述Mplayer。它支持大量的多媒体文件格式像常见的音频文件如mpwavmid常见的视频文件如avivcddvdrm等等各种视频编解码方式也是应有尽有。我们项目的目标是在liunx下用QT做一个MPlayer皮肤能够实现播放器的常见功能如:播放、停止、快进、快退、上下一曲等。并把程序移植到ARM平台上。二、功能体验本节主要目的是让大家在学习具体实现过程前先体验下播放器的功能。、PC端功能体验将“项目代码工程代码”目录下的mplayertargz拷贝linux系统中(主机环境配置好以后参照第四节的实现过程)。#tarxvfzmplayertargz#mplayer、目标平台功能体验l将“项目代码工程镜像”文件夹中的zImage、rootfscramfs烧写到目标板(或者采用nfs方式)。分区情况为:Boot:size=xKernel:–size=xRootfs:size=x启动参数为:setenvroot=finit=linuxrcrootfstype=cramfsconsole=ttySAC,display=samsetenvbootcmdsetenvbootcmdnandreadgol启动系统后运行:#Qtopiash#cdmymplayer#mymplayerqws三、实现原理先来考虑考虑如何为mplayer编写前端界面的问题。有两种思路一种是把mplayer解剖直接修改他里面的代码这样我们做得界面就能够和mplayer一体了(当然也能够通过linkmplayer用到的任何的库和o文档把他无缝的集成在程式里面)第二种方法就是mplayer所谓的slave模式。mplayer'homelinuxmp'quietslave现在来探讨一下slave模式:所谓的slave模式就是mplayer在运行过程中能够接收用户的输入命令行具体支持哪些命令行能够通过mplayerinputcmdlist这条命令来得到在Mplayer源码的slavetxt中也有对这些命令有详细的讲解。Slave模式下工作的Mplayer可以和系统的标准输入、输出进行信息交互。我们可以用linuxC编程来完成对slave模式工作的Mplayer进行控制和信息获取。如:mkfifo(“tmpfifo”,)可以使用popen()来打开MplayerFILE*mpmp=popen(“mplyerhomelinuxmpquiet–slave–inputfile=tmpfifo,”r”)可以通过管道tmpfifo给mplayer发送命令通过mp获取mplay的返回数据如:system(“echo”mute”>tmpfifo”)写命令fgets(buf,,mp)读取mplay返回数据而Qt给我们提供了更方便的实现方法。通过定义一个QProcess对象调用已编译好的Mplayer。QProcess*process=newQProcess()process>setProcessChannelMode(QProcess::MergedChannels)Process>start(“mplayer–acmadxxxxx”)在命令中添加slave和quiet就可以通过命令设置Mplayer实现相应的功能。在mplayer源码中的slavetxt中对这些命令有详细的讲解。Process>start(“mplayer–slave–quiet–acmadxxxxx”)、暂停功能通过如下代码可以设置Mplayer暂停。process>write(“pause”)执行这段代码的时候如果是播放状态就会暂停暂停状态时就会继续播放。、获取播放文件的总时间和当前播放进度执行下面代码时Mplayer将时间在标准输出显示。process>write("gettimepos")process>write("gettimelength")通过如下代码即可读出我们需要的信息:connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(backmessageslots()))process有可读取的信息时发出信号在槽函数backmessageslots()中读取信息。voidMPlayer::backmessageslots(){while(process>canReadLine()){QStringmessage(process>readLine())message即为读取的信息我们可以根据需要取我们要的信息如文件总时间为:ANSLENGTH=当前时间为:ANSTIMEPOSITION=}}、快进功能seek<value>typeSeektosomeplaceinthemovieisarelativeseekof<value>seconds(default)isaseekto<value>inthemovieisaseektoanabsolutepositionof<value>seconds下面代码即可实现快进功能:process>write(“seek**”)、音量调节volume<value>absIncreasedecreasevolumeorsetitto<value>ifabsisnonzero下面代码即可实现快进功能:Process>write(“volume”)音量减小Process>write(“volume”)音量增加、静音功能mutevalueTogglesoundoutputmutingorsetittovaluewhenvalue>=(==on,==off)下面代码即可实现快进功能:process>write("mute")开启静音process>write("mute")关闭静音、定位视频窗口通过上面的代码基本功能实现了可是播放视频的时候发现又弹出一个窗口。并没有出现在我们的窗口里。如下代码即可时间窗口的定位。QStringcommon="mplayerslavequietacmadzoommovie"filename"wid"QString::number(widget>winId())process>start(common)红色部分实现窗口的定位。Widget是一个QWidget对象。通过winId可以获得一个数字wid既将视频输出定位到widget窗体部件中。注意:wid参数只在X、directX和OpenGL中适用。四、PC环境下的实现过程、PC环境搭建主机环境:RedHatEnterpriseLinux交叉编译工具:gccglibc主机编译工具:gcc()主机端安装mplayerl将“项目代码mplay源码”目录下的MPlayerrctarbz、libmadbtargz(个mp解码库)拷贝到linux系统中如:homelinuxmplayer目录下l安装libmadbtargz#tarxvfzlibmadbtargz#cdlibmadb#configure#make#mkdirliblib#mkdirlibinclude#cpmadhlibinclude#cplibslibmadaliblibl安装mplayer#tarxvfjMPlayerrctarbz#cdMPlayerrc#configurewithextraincdir=libincludewithextralibdir=liblib#make#makeinstall此时可以试着播放一下mp、avi等文件了#mplayeracmadmp#mplayeracmadavi()安装、移植qtopia注:需要先按照节将tslib按照好l将“项目代码qtopia源码”目录下的qtopiaopensourcesrctargz拷贝到linux系统中如:homelinuxQtopia目录下#tarzxvfqtopiaopensourcesrctargz#mvqtopiaopensourcesource#mkdirtarget创建在source同级目录下创建目录target修改源码包#cdsource#cdsrclibrariesqtopiabase#cpcustomlinuxcassiopeiaghcustomlinuxarmgh#cpcustomlinuxcassiopeiagcppcustomlinuxarmgcpp修改时区信息#visrclibrariesqtopiaqtimezonecpp将行的usrsharezoneinfo改为Qtipiazoneinfo,保存退出。#visrcsettingssystemtimesettimecpp将行的usrsharezoneinfo改为Qtipiazoneinfo,保存退出。l裁减Qtopiacore的库(下列操作后在屏幕上会出现一个光标否则没有光标。根据需求配置)#viqtopiacoreqconfigqpeh首先注释掉关于鼠标光标的宏定义让程序运行时触摸屏中央有光标出现:QtopiaCore*#ifndefQTNOQWSCURSOR#defineQTNOQWSCURSOR#endif**#ifndefQTNOQWSMOUSE#defineQTNOQWSMOUSE#endif#ifndefQTNOQWSMOUSEAUTO#defineQTNOQWSMOUSEAUTO#endif*其它宏定义根据需要进行注释。保存后将qconfigqpeh拷贝到global目录。#cpqtopiacoreqconfigqpehqtopiacoreqtsrccorelibglobalqconfigqpeh(必须进行的操作)注释掉其他文件里的QTNOQWSCURSOR的定义#viqtopiacoreqtsrccorelibglobalqfeaturesh注释掉如下内容:*#if!defined(QTNOQWSCURSOR)(defined(QTNOCURSOR))#defineQTNOQWSCURSOR#endif*保存退出。#viqtopiacoreqtsrccorelibglobalqglobalh注释掉以下内容:#defineQTNOQWSCURSOR#vimqtopiacoreqttoolsqvfbqvfbshmemcpp注释掉asmpageh#include<asmpageh>#vimqtopiacoreqttoolsqvfbqvfbmmapcpp注释掉asmpageh#include<asmpageh>并修改如下内容unsignedchar*datauintdataoffsetvalue=sizeof(QVFbHeader)if(dataoffsetvaluePAGESIZE)dataoffsetvalue=PAGESIZE(dataoffsetvaluePAGESIZE)为:unsignedchar*datauintdataoffsetvalue=sizeof(QVFbHeader)constintpagesize=getpagesize()if(dataoffsetvaluepagesize)dataoffsetvalue=pagesize(dataoffsetvaluepagesize)#vimsrclibrariesqtopiabaseqmemoryfileunixcpp修改f=::open(tmpFiletoLatin(),OCREAT|OWRONLY)为:f=::open(tmpFiletoLatin(),OCREAT|OWRONLY)l修改交叉工具#vimqtopiacoreqtmkspecsqwslinuxarmgqmakeconf将文件中的armlinux***全部修改为armsoftfloatlinuxgnu**这样做的前提是我的交叉工具链是armsoftfloatlinuxgnu如果你的是armlinux就不用改了。l生成Makefile#cdtarget为了不破坏源码选择在此目录下配置、编译源码#sourceconfigurereleaseimageQtopiaprefixQtopiaxplatformlinuxarmgarcharmnoqvfbdisplaysizexnomodemextraqtopiacoreconfig"releasexplatformqwslinuxarmgembeddedarmqconfigqpedepths,,,qtsqlsqlitenomouselinuxtpqtmousetslibIhomelinuxtslibincludeLhomelinuxtsliblib">configureERRtxt注意:这里Qtopia是最后Qtopia的安装路径安装到主机的某个路径下最终这个路径和目标板上的路径必须一致。主要配置选项说明如下:xplatformlinuxarmgarcharm目标平台为armlinux,体系结构为arm。noqvfb目标平台已支持framebuffer,因而不使用虚拟帧缓冲。extraqtopiacoreconfig为Qtopiacore配置选项。xplatformqwslinuxarmgembeddedarm目标平台编译配置文件使用qtopiacoreqtmkspecsqwslinuxarmg目录下的配置文件嵌入式平台为arm。qconfigqpe使用配置文件qconfigqpeh若使用qconfiglargeh配置文件则使用qconfiglarge选项。qtsqlsqlite数据库支持Sqlite。qtkbdusb键盘支持usb协议。nomouselinuxtpqtmousetslibIhomelinuxtslibincludeLhomelinuxtsliblib触摸屏协议不支持linuxtp支持tslib并在后面添加上刚才编译的tslib的头文件和库。>qtopiaconfigureERRtxt最后将配置过程中的错误输出到qtopiaconfigureERRtxt文件中。l编译#make#makeinstalll将安装和的目录考到nfsroot目录下#cpQtopiarootfsa()熟悉主机开发环境l提供给PC端的开发工具上面的qtopia编译安装完成后会在咱们前面创建的target目录下生成很多开发工具。先看一下供主机端使用的工具rootlocalhostbin#pwdhomelinuxQtopiatargetqtopiacorehostbinrootlocalhostbin#lsassistantlinguistlupdateqmakerccuicdesignerlreleasemocqvfbtemplatesuic如果系统以前有其它qt开发工具把环境变量修改一下保证它们不要和我们这几个工具冲突。下面可以试一下你的designer了。#designer、在PC端实现基于qt前端的mplayer播放器创建工程目录homelinuxmplayer()搭建ui界面利用前面安装的designer搭建ui界面并将其保存至homelinuxmplayermplayerui#designer圆角矩形标注:加了一个widget留作mplayer的播放区()编写程序在homelinuxmymplayer下创建mplayercpp、mplayerh、maincpp、imageqrcMaincpp*****************************maincpp*****************************#include<QApplication>#include"mplayerh"intmain(intargc,char**argv){QApplicationapp(argc,argv)MPlayerplayer实例最终的MPlayer类playershow()显示界面returnappexec()运行程序}mplayerh#ifndefMPLAYERH#defineMPLAYERH#include<QIcon>#include<QProcess>#include<QTimer>#include<QStringList>#include<QDir>#include<QTime>#include<QString>#include"uimplayerh"classMPlayer:publicQDialog,privateUiDialog{QOBJECTpublic:MPlayer(QWidget*parent=)public:QTimeinttotime(int)publicslots:voidplaypauseslots()暂停voidstopslots()停止voidpreviousslots()上一曲voidnextslots()下一曲voidseekslots(int)voidgettimeslots()得到播放时间voidsetvolumeslots(int)设置音量voidsetsoundslots()静音voidplayerRewardslots()快退voidplayerForwardslots()快进voidbackmessageslots()更新显示信息private:QProcess*processQStringListfilesQDirdirectoryintfilecountQStringfilenameboolisPlayboolisSoundboolisStopQTimer*timerintfilelengthintcurrtime}#endifmplayercpp*******************************mplayercpp**********************************#include"mplayerh"#include<QDebug>#include<unistdh>MPlayer::MPlayer(QWidget*parent):QDialog(parent){setupUi(this)初始化界面isPlay=trueisSound=trueisStop=false************************为按键添加图标**************************playQIconiconplayiconplayaddPixmap(QPixmap(QString::from("imagespauseenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconplay)stopQIconiconstopiconstopaddPixmap(QPixmap(QString::from("imagesstopenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconstop)rewardQIconiconrewardiconrewardaddPixmap(QPixmap(QString::from("imagesrewardenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconreward)forwardQIconiconforwardiconforwardaddPixmap(QPixmap(QString::from("imagesforwardenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconforward)soundQIconiconsoundiconsoundaddPixmap(QPixmap(QString::from("imagessoundenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconsound)QIconiconpreviousiconpreviousaddPixmap(QPixmap(QString::from("imagespreviousdisabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconprevious)QIconiconnexticonnextaddPixmap(QPixmap(QString::from("imagesnextenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconnext)************************设置按钮无边框**********************************pushButton>setFlat(true)pushButton>setFlat(true)pushButton>setFlat(true)pushButton>setFlat(true)pushButton>setFlat(true)pushButton>setFlat(true)pushButton>setFlat(true)*************************获得播放列表***************************directorysetPath("movie")files=directoryentryList(QDir::AllEntries,QDir::Time)filename=files文件和为””””所以从文件开始播放filecount=label>setText(files)*************************初始化进度条及QProcess类**************horizontalSlider>setPageStep()process=newQProcess(this)process>setProcessChannelMode(QProcess::MergedChannels)*************************初始化信号、槽*************************connect(pushButton,SIGNAL(clicked()),this,SLOT(playpauseslots()))connect(pushButton,SIGNAL(clicked()),this,SLOT(stopslots()))connect(pushButton,SIGNAL(clicked()),this,SLOT(playerRewardslots()))connect(pushButton,SIGNAL(clicked()),this,SLOT(playerForwardslots()))connect(pushButton,SIGNAL(clicked()),this,SLOT(previousslots()))connect(pushButton,SIGNAL(clicked()),this,SLOT(nextslots()))connect(horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(seekslots(int)))connect(spinBox,SIGNAL(valueChanged(int)),this,SLOT(setvolumeslots(int)))connect(pushButton,SIGNAL(clicked()),this,SLOT(setsoundslots()))connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(backmessageslots()))当process可以读到Mplayer的返回信息时产生readyReadStandardOutput()信号process>start("mplayerslavequietacmadavi")addwidQWidget>winId()QStringcommon="mplayerslavequietacmadzoommovie"filename"wid"QString::number(widget>winId())这里的widget是ui中MPlayer的显示区process>start(common)开始运行程序spinBox>setValue()timer=newQTimer(this)connect(timer,SIGNAL(timeout()),this,SLOT(gettimeslots()))定时获取MPlayer的时间信息timer>start()启动定时器秒timeout次}voidMPlayer::playpauseslots(){if(!isPlay){if(isStop){filename=filesfilecountQStringcommon="mplayerslavequietacmadzoommovie"filename"wid"QString::number(widget>winId())process>start(common)QIconiconstopiconstopaddPixmap(QPixmap(QString::from("imagesstopenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconstop)isStop=false}else{process>write("pause")}QIconiconplayiconplayaddPixmap(QPixmap(QString::from("imagespauseenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconplay)isPlay=true}else{QIconiconpauseiconpauseaddPixmap(QPixmap(QString::from("imagesplayenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconpause)isPlay=falseprocess>write("pause")}}voidMPlayer::stopslots(){if(!isStop){process>write("quit")QIconiconpauseiconpauseaddPixmap(QPixmap(QString::from("imagesplayenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconpause)isPlay=falseQIconiconstopiconstopaddPixmap(QPixmap(QString::from("imagesstopdisabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconstop)isStop=truelabel>setText("::")label>setText("::")}}voidMPlayer::previousslots(){if(filecount>){if(filecount==(filessize())){QIconiconnexticonnextaddPixmap(QPixmap(QString::from("imagesnextenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconnext)}process>write("quit")process=newQProcess(this)connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(backmessageslots()))filecountif(!isStop){filename=filesfilecountQStringcommon="mplayerslavequietacmadzoommovie"filename"wid"QString::number(widget>winId())process>start(common)}if(filecount==){QIconiconpreviousiconpreviousaddPixmap(QPixmap(QString::from("imagespreviousdisabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconprevious)}label>setText(filesfilecount)}}voidMPlayer::nextslots(){if(filecount<(filessize())){if(filecount==){QIconiconpreviousiconpreviousaddPixmap(QPixmap(QString::from("imagespreviousenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconprevious)}process>write("quit")process=newQProcess(this)connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(backmessageslots()))filecountif(!isStop){filename=filesfilecountQStringcommon="mplayerslavequietacmadzoommovie"filename"wid"QString::number(widget>winId())process>start(common)}if(filecount==(filessize())){QIconiconnexticonnextaddPixmap(QPixmap(QString::from("imagesnextdisabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconnext)}}label>setText(filesfilecount)}voidMPlayer::seekslots(intseeknum){qDebug()<<seeknumif(processprocess>state()==QProcess::Running){process>write(QString("seek"QString::number(qMin(seeknum,))"")toAscii())}}voidMPlayer::gettimeslots(){if(isPlay){process>write("gettimepos")process>write("gettimelength")}}voidMPlayer::setvolumeslots(intvolume){qDebug()<<volumeprocess>write(QString("volume"QString::number(volume)"")toAscii())process>write(QString("volume")toAscii())}voidMPlayer::setsoundslots(){if(isSound){process>write("mute")QIconiconsoundiconsoundaddPixmap(QPixmap(QString::from("imagesnosoundenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconsound)isSound=false}else{process>write("mute")QIconiconsoundiconsoundaddPixmap(QPixmap(QString::from("imagessoundenabledpng")),QIcon::Normal,QIcon::Off)pushButton>setIcon(iconsound)isSound=true}}voidMPlayer::playerRewardslots(){boolokintm=moviePositiontoInt(ok)if(processprocess>state()==QProcess::Running!isPlay){QStringcmd="seek"QString::number(qMax(m,))""process>write(cmdtoAscii())qDebug()<<"Reward"}}voidMPlayer::playerForwardslots(){groupBox>setVisible(false)boolokintm=moviePositiontoInt(ok)*if(processprocess>state()==QProcess::Running!isPlay){QStringcmd="seek"QString::number(qMin(m,))""process>write(cmdtoAscii())qDebug()<<"Forward"}*}voidMPlayer::backmessageslots(){while(process>canReadLine()){QStringmessage(process>readLine())QStringListmessagelist=messagesplit("=")if(messagelist=="ANSTIMEPOSITION"){currtime=messagelisttoDouble()toInt()QTimetime=inttotime(currtime)label>setText(timetoString("hh:mm:ss"))horizontalSlider>setValue(*currtimefilelength)}elseif(messagelist=="ANSLENGTH"){filelength=messagelisttoDouble()toInt()QTimetime=inttotime(filelength)label>setText(timetoString("hh:mm:ss"))}}}QTimeMPlayer::inttotime(intsecond){intsec=,min=,hour=QTimetimeif(second<){sec=secondmin=hour=}if(second>=second<){sec=secondmin=secondhour=}if(second>=){sec=secondmin=(second)hour=second}timesetHMS(hour,min,sec)returntime}imageqrc<RCC><qresourceprefix="images"><file>imagesplayerplaypng<file><file>imagesplayerstoppng<file><file>imagesplayerpausepng<file><file>imagesplayenabledpng<file><file>imagespauseenabledpng<file><file>imagesrewardenabledpng<file><file>imagesforwardenabledpng<file><file>imagesstopenabledpng<file><file>imagessoundenabledpng<file><file>imagesnosoundenabledpng<file><file>imagespreviousenabledpng<file><file>imagespreviousdisabledpng<file><file>imagesnextenabledpng<file><file>imagesnextdisabledpng<file><qresource><RCC>()编译工程l拷贝qmake到当前工程目录下#cphomelinuxQtopiatargetqtopiacorehostbinqmakel生成项目文件、#qmake–projectl生成Makefile#qmakel编译#make成功后可以生成mplayer可执行程序l建立movie和images#mkdirmovie#mkdirimagesrootlocalhostmplayer#lsmoviempaviaviaviavirootlocalhostmplayer#lsimagesforwardenabledpngnosoundenabledpngplayerplaypngrewardenabledpngimagespauseenabledpngplayerstoppngsoundenabledpngnextdisabledpngplayenabledpngpreviousdisabledpngstopdisabledpngnextenabledpngplayerpausepngpreviousenabledpngstopenabledpngl运行程序rootlocalhostmplayer#mplayer五、移植到ARM平台过程、内核要求要求内核支持framebuffer驱动、OSS音频驱动、支持input事件的触摸屏驱动。、目标板上部署qt环境将前面交叉编译好的Qtopia目录拷贝到nfsroot目录下#cpQtopiarootfs–a注:rootfs为目标平台的nfs根文件系统位置、文件系统中移植tslib(下面的步骤是在ubantu环境下编译的其它的系统基本相同)()拷贝“项目代码tslib源码”目录下的tslibtargz到linux系统()#tarzxvftslibtargz#cdtslib#autogensh这一步需要安装一些工具如:在ubantu系统下可以执行:sudoaptgetinstallautomake()执行autogensh脚本所生成的Makefile文件请打入以下命令:echo"accvfuncmallocnon=yes">$ARCHlinuxcacheconfigurehost=armsoftfloatlinuxgnuprefix=homelinuxtslibcachefile=$ARCHlinuxcachehost是指你的交叉编译器的前最例如:你的交叉编译器是armlinuxgcc,则host=armlinux如果是armsoftfloatlinuxgnugcc则host=armsoftfloatlinuxgnuprefix是你执行makeinstall的时候编译后生成的可执行文件和库文

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/20

QT_播放器

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利