QT调用mplayer

来源:互联网 发布:莱阳螳螂拳淘宝 编辑:程序博客网 时间:2024/05/25 23:58

转载地址:http://blog.sina.com.cn/s/blog_6273cce40100izdl.html

 

在上一篇博客中已经编译安装好了Mplayer,其只是一个简单的后台播放程序,没有窗口按钮,没有皮肤。但可以通过Qt编写一个前端程序,调用后台的Mplayer,实现一个多媒体播放器。

  可以先通过designer来设计一个简单的播放界面,然后再编写头文件和实现文件。

  通过定义QProcess对象来调用已经编译好的Mplayer。

   mplayerProcess= new QProcess();

//QProcess:该类用来启动一个外部程序,并且与之交互通信。

  mplayerProcess->setProcessChannelMode(QProcess::MergedChannels);

//voidQProcess::setProcessChannelMode():Sets the channel mode ofthe QProcess standard output and standard error channels to themode specified. This mode will be used the next time start() iscalled. 将QProcess类的标准输出和标准错误信道模型,设置成指定的模型。

  通过如下代码可以读出信息:

   connect(mplayerProcess,SIGNAL(readyReadStandardOutput()), this, SLOT(catchOutput())));

当mplayerProcess有可读取的信息时,readyReadStandardOutput()函数发出信号,然后在槽函数catchOutput()中读取该信息。

   voidcatchOutput()

 {

  while(mplayerProcess->canReadLine());

   {

    QByteArraybuffer(mplayerProcess->readLine());//QByteArray:提供一个字节数组,buffer即为读取的信息。我们可以根据需要获取需要的信息。

    log->append(QString(buffer));//log是之前定义的一个QTextEdit类,用来显示只读信息。这里就是将buffer获取的信息,让log函数显示出来。

    ......

   }

 }

  另外有很多命令可以实现设置Mplayer的功能。比如上图我们调用了一个QSlider类,滑块类来显示播放时间的进度。

   poller = newQTimer();

  connect(poller, SIGNAL(timeout()), this,SLOT(pollerCurrentTime()));

   voidpollerCurrentTime()

{

    mplayerProcess->write("get_time_pos/n");//可以将时间在标准输出显示

}

  还有一些其他功能,比如快进:process->write("seek ** 1/n");

   音量调节:

  Process->write("volume -1/n"); //音量减小

 Process->write("volume +1/n");//音量增加

  静音功能:

  process->write("mute 0/n"); //开启静音

 process->write("mute 1/n"); //关闭静音

  等等,这里只是简单的琢磨了一下Qt程序调用Mplayer的方法,要设计一个mplayer的前端程序,后期编程还是有一定难度的。

 

原创粉丝点击