歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Qt 簡易播放器

頭文件: 

#ifndef MEDIA_H
#define MEDIA_H

#include <QMainWindow>
#include<QImage>
#include<QLabel>
#include<QProcess>
#include<QStringList>
#include<QFileDialog>
#include<QDebug>
#include<QScrollArea>
#include<phonon/VideoPlayer>//影音播放
#include<QApplication>
#include<QUrl>
#include<QWidget>
#include<QPixmap>
#include<QString>
namespace Ui {
    class Media;

}

class Media : public QMainWindow
{
    Q_OBJECT

public:
    explicit Media(QWidget *parent = 0);
    ~Media();

private:
    Ui::Media *ui;
    QLabel *label;
    QProcess *process;
    QWidget widget;
    QPixmap pix;
    QString file_name;
private slots:
    void on_pushButton_5_clicked();
    void on_kuaijin_clicked();
    void on_pushButton_2_clicked();
    void on_jinying_clicked();
    void on_small_clicked();
    void on_pushButton_clicked();
    void on_pushButton_4_clicked();
    void on_down_clicked();
    void on_up_clicked();
    void on_start_clicked();
    void on_open_clicked();

};

#endif // MEDIA_H

 

 

主函數:

#include <QtGui/QApplication>
#include "media.h"
#include<QTextCodec>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));

    Media w;
    w.setDisabled(false);
    w.setFixedSize(w.width(),w.height());
    w.show();
    return a.exec();
}


 cpp函數:

#include "media.h"
#include "ui_media.h"
int i=0,j=0;
QString medialist[100];//存放播放的音頻
QStringList::Iterator it;//迭代器

Media::Media(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Media)
{
    ui->setupUi(this);
/*************************************添加背景圖片*************************************/
    QImage image1;
    image1.load(":/55.png");    //括號內為圖片的相對目錄
    QImage image2 = image1.scaled(320,240);
    QPalette palette;
    palette.setBrush(this->backgroundRole(),QBrush(image2));
    this->setPalette(palette);
     process=new QProcess(this);
     this->setWindowTitle(tr("播放器-----菜鳥營制作中心"));
    this->setWindowIcon(QIcon("../../Media/100.png"));

 


}

Media::~Media()
{
    delete ui;
}
/*************************************打開文件*************************************/
void Media::on_open_clicked()
{
    int k=0;
    QStringList list=QFileDialog::getOpenFileNames(this,"selecct media","/","*.*");
    for(it=list.begin();it<list.end();it++)
    {
            medialist[k]=*it;
            qDebug()<<medialist[k];
            k++;
    }
        j=k-1;
}
/*************************************開始播放*************************************/
void Media::on_start_clicked()
{
     process->close();
     QString path="../../Media/mplayer.exe";
     QStringList list1;
     list1<<"-slave";
     list1<<"-quiet";
     list1<<"-wid";//這兩個不能分開寫
     list1<<QString::number((unsigned int)(ui->widget->winId()));
     list1<<"-vo";
     list1<<"directx:noaccel";
     list1<<medialist[i];
     process->start(path,list1);//將播放器與窗口相結合
     i++;
     if(i>j)
         i=0;

}
/*************************************上一曲*************************************/
void Media::on_up_clicked()
{
    process->close();
    i++;
    if(i>j)
        i=0;
    process->close();
    QString path="../../Media/mplayer.exe";
    QStringList list1;
    list1<<"-slave";
    list1<<"-quiet";
    list1<<"-wid";//這兩個不能分開寫
    list1<<QString::number((unsigned int)(ui->widget->winId()));
    list1<<"-vo";
    list1<<"directx:noaccel";
    list1<<medialist[i];
    process->start(path,list1);
}
/*************************************下一曲*************************************/
void Media::on_down_clicked()
{
    process->close();
    i--;
    if(i<0)
        i=j;
    process->close();
    QString path="../../Media/mplayer.exe";
    QStringList list1;
    list1<<"-slave";
    list1<<"-quiet";
    list1<<"-wid";//這兩個不能分開寫
    list1<<QString::number((unsigned int)(ui->widget->winId()));
    list1<<"-vo";
    list1<<"directx:noaccel";
    list1<<medialist[i];
    process->start(path,list1);

}
/*************************************停止播放*************************************/
void Media::on_pushButton_4_clicked()
{
    process->write("p\n");
}
/*************************************增大音量*************************************/
void Media::on_pushButton_clicked()
{
    process->write("volume +1\n");
}
/*************************************減小音量*************************************/
void Media::on_small_clicked()
{
    process->write("volume -1\n");
}
/*************************************開啟靜音*************************************/
void Media::on_jinying_clicked()
{
    process->write(" mute 1\n");
}
/*************************************關閉靜音*************************************/
void Media::on_pushButton_2_clicked()
{
     process->write(" mute 0\n");
}
/*************************************快進*************************************/
void Media::on_kuaijin_clicked()
{
    process->write("seek +1\n");
}
/*************************************快退*************************************/
void Media::on_pushButton_5_clicked()
{
    process->write("seek -2\n");
}

效果圖:

Copyright © Linux教程網 All Rights Reserved