基于qt和mplayer的影音播放器

转自:http://www.cnblogs.com/ziqiuqiandao/archive/2012/11/24/2785473.html


这是一个基于qt和mplayer使用的影音播放器,代码不多,就3,4百行。

使用到的控件有QWidget、QLabel、QFrame、QPushButton、QSlider、QListWidget、QProcess和QString。

通过这几种控件的使用,可以基本了解到qt的编程风格和使用方式。

这个程序参考了网上流行的qt控制mplayer播放的多篇文章,在此感谢一下。

运行效果图如下:

qmplayer.pro:

######################################################################
# Automatically generated by qmake (2.01a) ?? ?? 18 03:06:00 2010
######################################################################

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += qmplayer.h
SOURCES += main.cpp qmplayer.cpp

main.cpp:
#include "qmplayer.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QMPlayer *qmplayer = new QMPlayer;
    qmplayer->showFullScreen();
    return app.exec();
}
qmplayer.h:
   
#ifndef _H_QMPLAYER_
#define _H_QMPLAYER_

#include <QtGui>

class QMPlayer:public QWidget
{
    Q_OBJECT
public:
    void play(QString fileName);
      QMPlayer(QWidget *parent = 0); 

private slots:
    void next();
    void prev();
    void pause();
    void stop();
    void setSpeed();
    void dataRecieve();
    void addItem();
    void delItem();
    void current(int);   
    void changeVolume(int);
    void playListItem(QListWidgetItem *);
    
private:
       QLabel *playLabel;
    QLabel *timeLabel;
    QLabel *percentLabel;
    QLabel *voiceLabel;
    QLabel *volumeLabel;
    
    QFrame *ctlFrame;
    QPushButton *playBtn;
    QPushButton *stopBtn;
    QPushButton *prevBtn;
    QPushButton *nextBtn;
    QPushButton *speedBtn;
    
    QSlider *currentlBar;
    QSlider *volumeSlider;

    QListWidget *playList;
    QPushButton *addBtn;
    QPushButton *delBtn;
    
    QProcess *p;
    QString currentStr;
    QString currentPercent;
    QString totalTime;
    QString fileName;
};

#endif /* _H_QMPLAYER_ */
qmplayer.cpp:
#include "qmplayer.h"

QMPlayer::QMPlayer(QWidget *parent) :QWidget(parent)
{
    resize(320, 240);
    setWindowTitle("mplayer");

    playLabel = new QLabel(this);
    playLabel->setGeometry(QRect(0, 0, 240, 180));
    playLabel->setPixmap(QPixmap("images/mplayer.png"));

    ctlFrame = new QFrame(this);
    ctlFrame->setGeometry(QRect(15, 183, 300, 55));
    ctlFrame->setFrameShape(QFrame::StyledPanel);
    ctlFrame->setFrameShadow(QFrame::Raised);

    currentlBar = new QSlider(ctlFrame);
    currentlBar->setGeometry(QRect(10, 0, 180, 18));
    currentlBar->setOrientation(Qt::Horizontal);
    connect(currentlBar, SIGNAL(sliderMoved(int)), this, SLOT(current(int)));

    percentLabel = new QLabel(ctlFrame);
    percentLabel->setGeometry(QRect(195, 2, 40, 18));

    timeLabel = new QLabel(ctlFrame);
    timeLabel->setGeometry(QRect(235, 2, 60, 18));

    stopBtn = new QPushButton("stop",ctlFrame);
    stopBtn->setGeometry(QRect(10, 18, 40, 18));
    stopBtn->setEnabled(false);
    connect(stopBtn, SIGNAL(clicked()), this, SLOT(stop()));

    prevBtn = new QPushButton("prev",ctlFrame);
    prevBtn->setGeometry(QRect(60, 18, 40, 18));
    connect(prevBtn, SIGNAL(clicked()), this, SLOT(prev()));
    
    playBtn = new QPushButton("play",ctlFrame);
    playBtn->setGeometry(QRect(110, 18, 55, 18));
    playBtn->setEnabled(false);
    connect(playBtn, SIGNAL(clicked()), this, SLOT(pause()));

    nextBtn = new QPushButton("next", ctlFrame);
    nextBtn->setGeometry(QRect(175, 18, 40, 18));
    connect(nextBtn, SIGNAL(clicked()), this, SLOT(next()));

    speedBtn = new QPushButton("speed", ctlFrame);
    speedBtn->setGeometry(QRect(225, 18, 55, 18));
    connect(speedBtn, SIGNAL(clicked()), this, SLOT(setSpeed()));

    voiceLabel = new QLabel("voice:", ctlFrame);
    voiceLabel->setGeometry(QRect(10, 36, 50, 18));

    volumeSlider = new QSlider(ctlFrame);
    volumeSlider->setGeometry(QRect(70, 36, 100, 18));
    volumeSlider->setOrientation(Qt::Horizontal);
    volumeSlider->setRange(0, 100);
    volumeSlider->setValue(100);
    volumeSlider->setEnabled(false);
    connect(volumeSlider, SIGNAL(sliderMoved(int)), this, SLOT(changeVolume(int)));

    volumeLabel = new QLabel(ctlFrame);
    volumeLabel->setGeometry(QRect(175, 36, 60, 18));
    volumeLabel->setText(QString::number(100));

    playList = new QListWidget(this);
    playList->setGeometry(QRect(243, 8, 73, 150));
    connect(playList, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(playListItem(QListWidgetItem *))); 

    addBtn = new QPushButton(QIcon("images/add.png"), "", this);
    addBtn->setGeometry(QRect(248, 160, 30, 20));
    addBtn->setStyleSheet("border-style:none");
    connect(addBtn, SIGNAL(clicked()), this, SLOT(addItem())); 

    delBtn = new QPushButton(QIcon("images/remove.png"), "", this);
    delBtn->setGeometry(QRect(283, 160, 30, 20));
    delBtn->setStyleSheet("border-style:none");
    connect(delBtn, SIGNAL(clicked()), this, SLOT(delItem())); 

}

/*************************************************

Function:next

Description:play the next movie/song

*************************************************/
void QMPlayer::next()
{
    if (playList->count() != 0) {
        if(playList->currentRow() == playList->count() - 1)
            playList->setCurrentRow(0);
        else
            playList->setCurrentRow(playList->currentRow() + 1);
        
        play(playList->currentItem()->text());
        playBtn->setText("pause");
    }
}

/*************************************************

Function:prev

Description:play the previous movie/song

*************************************************/
void QMPlayer::prev()
{
    if (playList->count() != 0) {
        if(playList->currentRow() == 0)
            playList->setCurrentRow(playList->count() - 1);
        else
            playList->setCurrentRow(playList->currentRow() - 1);
        
        play(playList->currentItem()->text());
        playBtn->setText("pause");
    }
}

/*************************************************

Function:addItem

Description:add a item in the playlist

*************************************************/
void QMPlayer::addItem()
{
    QStringList fileNames = QFileDialog::getOpenFileNames(this, "choose movie/song", "/", "Movie/Song (*.mp4 *.mp3)");
    if(fileNames.count() != 0)
        playList->addItems(fileNames);
}

/*************************************************

Function:delItem

Description:delete a item in the playlist

*************************************************/
void QMPlayer::delItem()
{
    if(playList->currentRow() == -1)
        QMessageBox::warning(this, "note", "not select or playList is null", QMessageBox::Yes);
    else
        playList->takeItem(playList->currentRow());
}

/*************************************************

Function:playListItem

Description:play a item in the playlist

*************************************************/
void QMPlayer::playListItem(QListWidgetItem *item)
{
    play(item->text());
    playBtn->setText("pause");
}

/*************************************************

Function:play

Description:call mplayer to play movie/song

*************************************************/

void QMPlayer::play(QString fileName)
{
    if (p != NULL)
        p->kill();

    p = new QProcess(this);
    connect(p, SIGNAL(readyReadStandardOutput()), this, SLOT(dataRecieve())); 

    QStringList args;
    args << "-slave";
    args << "-quiet";
    args << "-zoom";
    args << "-x";
    args << "240";
    args << "-y";
    args << "180";
    args << fileName;
    p->start("mplayer", args);
    
    playBtn->setEnabled(true);
    stopBtn->setEnabled(true);
    volumeSlider->setEnabled(true);

    timeLabel->show();
    percentLabel->show();
}

/*************************************************

Function:pause

Description:pause a playing movie/song

*************************************************/
void QMPlayer::pause()
{
    p->write("pause\n");
    if (playBtn->text() == "play") {
        connect(p, SIGNAL(readyReadStandardOutput()), this, SLOT(dataRecieve())); 
        dataRecieve();
        playBtn->setText("pause");
    }
    else {
        disconnect(p, SIGNAL(readyReadStandardOutput()), this, SLOT(dataRecieve())); 
        playBtn->setText("play");
    }

}

/*************************************************

Function:stop

Description:stop a playing movie/song

*************************************************/
void QMPlayer::stop()
{
    p->kill();
    p = NULL;
    timeLabel->hide();
    percentLabel->hide();
    playLabel->update();
    
    playBtn->setEnabled(false);
    stopBtn->setEnabled(false);
    volumeSlider->setEnabled(false);
}

/*************************************************

Function:changeVolume

Description:change the volume

*************************************************/
void QMPlayer::changeVolume(int v)
{
    volumeLabel->setText(QString::number(v));
    p->write(QString("volume " + QString::number(v) + " 2\n").toUtf8());
}

/*************************************************

Function:setSpeed

Description:set the playing speed

*************************************************/
void QMPlayer::setSpeed()
{
    double speed=QInputDialog::getDouble(this, "set speed", "compare with nomal speed");
    if(speed > 0)
        p->write(QString("speed_set " + QString::number(speed) + " 2\n").toUtf8());
}

/*************************************************

Function:dataRecieve

Description:recieve the data from mplayer

*************************************************/
void QMPlayer::dataRecieve()
{
    p->write("get_time_length\n");
    p->write("get_time_pos\n");
    p->write("get_percent_pos\n");

    while (p->canReadLine()) {
        QByteArray b = p->readLine();
        b.replace(QByteArray("\n"), QByteArray(""));
        QString s(b);
        
        if (b.startsWith("ANS_TIME_POSITION")) {
            currentStr = s.mid(18);
            timeLabel->setText(currentStr + "s");
            currentlBar->setValue(s.mid(18).toFloat());
        }
        else if (b.startsWith("ANS_LENGTH")) {
            totalTime = s.mid(11);
            currentlBar->setRange(0, s.mid(11).toFloat());
        }
        else if(b.startsWith("ANS_PERCENT_POSITION")) {
            currentPercent = s.mid(21);
            percentLabel->setText(currentPercent + "%");
        }
    }

}

/*************************************************

Function:current

Description:get the current process from mplayer

*************************************************/

void QMPlayer::current(int value)
{
    p->write(QString("seek " + QString::number(value) + " 2\n").toUtf8());
}

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Qt中实现一个MPlayer音乐播放器,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了MPlayer。你可以在命令行中运行`mplayer`来检查它是否已经安装并可用。 2. 创建一个新的Qt项目,使用Qt Creator或者命令行工具。 3. 在Qt项目中,使用`QProcess`类来调用MPlayer命令行工具。`QProcess`类允许你在后台运行外部程序并与其通信。 4. 在你的Qt代码中,创建一个`QProcess`对象,并使用`start()`函数来启动MPlayer进程。 ```cpp QProcess* mplayerProcess = new QProcess(this); mplayerProcess->start("mplayer", QStringList() << "your_music_file.mp3"); ``` 这将启动MPlayer进程并播放指定的音乐文件。 5. 如果你需要控制MPlayer的播放,你可以使用`write()`函数向MPlayer发送命令。例如,你可以发送"pause"命令来暂停音乐的播放。 ```cpp mplayerProcess->write("pause\n"); ``` 这将向MPlayer发送"pause"命令。 6. 你还可以使用`readAllStandardOutput()`函数来读取MPlayer的输出。这对于获取当前播放时间、音量等信息非常有用。 ```cpp QByteArray output = mplayerProcess->readAllStandardOutput(); // 解析output中的信息 ``` 7. 最后,记得在你的Qt应用程序退出时终止MPlayer进程,以确保资源的正确释放。 ```cpp mplayerProcess->terminate(); mplayerProcess->waitForFinished(); ``` 这是一个简单的示例来演示如何在Qt中实现一个基本的MPlayer音乐播放器。你可以根据自己的需求进行扩展和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值