Qt 简易播放器

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");

}

 

 

效果图:

 

 

 

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-10/45064.htm

发布了19 篇原创文章 · 获赞 22 · 访问量 19万+
展开阅读全文

QT+gstreamer,视频不在窗口显示问题

08-08

目前采用ARM芯片为飞思卡尔的imx6,该芯片具备VPU,具备硬件加速的能力。在ARM板上安装UBUNTU14系统,桌面为XFCE桌面,安装gstreamer1.0(使用synaptic一键安装)播放器可以流畅播放高清视频。使用QT5.1.2调用gstreamer1.0,视频无法在指定窗口播放,而是全屏播放 代码摘录如下,请指出不能在正确位置播放是否为调用方法问题? Widget::Widget(QWidget *parent) : QWidget(parent) { /* Create the elements */ data->playbin2 = gst_element_factory_make ("playbin", "playbin2"); g_object_set(data->playbin2, "uri", uri.toUtf8().data(), NULL); /* Create the GUI */ createUi(data); /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */ bus = gst_element_get_bus (data->playbin2); } void Widget::createUi (CustomData *data) { setWindowTitle(tr("????????")); setWindowIcon(QIcon(":/new/image/Image/icon_fronware.png")); resize(600,500); setStyleSheet("background-color: white;"); qDebug()<<"CURRENT WIDGET"<<this->winId(); mainLayout = new QVBoxLayout(this); mainLayout->setContentsMargins(5,5,5,5); mainLayout->setSpacing(5); displayWnd = new VideoWidget; qDebug()<<"1"<<displayWnd->width()<<displayWnd->height(); backgroundWnd = new QLabel; backgroundWnd->setStyleSheet("background-color: black;"); qDebug()<<"2backgroundWnd"<<backgroundWnd->width()<<backgroundWnd->height(); renderWnd = new QStackedWidget; //A = new QGstWidget; renderWnd->addWidget(displayWnd); renderWnd->addWidget(backgroundWnd); renderWnd->setCurrentIndex(0); //qDebug()<<"2"<<displayWnd->width()<<displayWnd->height(); qDebug()<<"2renderWnd"<<renderWnd->width()<<renderWnd->height(); qDebug()<<"3renderWnd"<<this->width()<<this->height(); slider = new QSlider(Qt::Horizontal); slider->setFixedHeight(15); timeLabel = new QLabel; timeLabel->setFixedHeight(15); timeLabel->setStyleSheet("background-color: #ffffff;color:black; font-family:\"STXihei\";font-size: 10px;"); timeLabel->setText("00:00:00/00:00:00"); timeLayout = new QHBoxLayout; timeLayout->setContentsMargins(0,0,0,0); timeLayout->setSpacing(5); timeLayout->addWidget(slider,5); timeLayout->addWidget(timeLabel,1); infoLabel = new QLabel; infoLabel->setFixedHeight(25); infoLabel->setStyleSheet("background-color: white;color:black; font-family:\"STXihei\";font-size: 15px;"); openBtn = new QPushButton; openBtn->setFixedSize(75,25); openBtn->setText("????"); openBtn->setStyleSheet(" QPushButton{border: 1px solid #C0C0C0; \ background-color: white;\ border-style: solid;\ border-radius:0px;\ color:black;\ font-family:\"STXihei\";\ font-size: 15px;\ padding:0 0px;\ margin:0 0px;\ }\ QPushButton:hover{border: 1px solid #C0C0C0; \ background-color: #ececec;\ border-style: solid;\ border-radius:0px;\ color:black;\ font-family:\"STXihei\";\ font-size: 15px;\ padding:0 0px;\ margin:0 0px;\ }\ "); startBtn = new QPushButton; startBtn->setFixedSize(45,25); startBtn->setText("??"); startBtn->setStyleSheet(openBtn->styleSheet()); stopBtn = new QPushButton; stopBtn->setFixedSize(45,25); stopBtn->setText("??"); stopBtn->setStyleSheet(openBtn->styleSheet()); plauseBtn = new QPushButton; plauseBtn->setFixedSize(45,25); plauseBtn->setText("??"); plauseBtn->setStyleSheet(openBtn->styleSheet()); plauseBtn->setVisible(false); muteButton = new QToolButton; muteButton->setFixedSize(45,25); muteButton->setIcon(style()->standardIcon(QStyle::SP_MediaVolume)); volumeSlider = new QSlider(Qt::Horizontal); volumeSlider->setFixedSize(60,15); volumeSlider->setRange(0,100); buttonLayout = new QHBoxLayout; buttonLayout->setContentsMargins(0,0,0,0); buttonLayout->setSpacing(15); buttonLayout->addStretch(); buttonLayout->addWidget(openBtn); buttonLayout->addWidget(startBtn); buttonLayout->addWidget(plauseBtn); buttonLayout->addWidget(stopBtn); buttonLayout->addWidget(muteButton); buttonLayout->addWidget(volumeSlider); buttonLayout->addStretch(); mainLayout->addWidget(renderWnd,5); mainLayout->addWidget(infoLabel,1); mainLayout->addLayout(timeLayout); mainLayout->addLayout(buttonLayout); qDebug()<<"3"<<displayWnd->width()<<displayWnd->height(); realize_cb(displayWnd,data); connect(openBtn,SIGNAL(clicked()),this,SLOT(slotOpenButtonClicked())); connect(startBtn,SIGNAL(clicked()),this,SLOT(slotPlayButtonClicked())); connect(plauseBtn,SIGNAL(clicked()),this,SLOT(slotPaluseButtonClicked())); connect(stopBtn,SIGNAL(clicked()),this,SLOT(slotStopButtonClicked())); connect(displayWnd,SIGNAL(fullScreenSignal(bool)),this,SLOT(slotFullScreen(bool))); connect(muteButton,SIGNAL(clicked()),this,SLOT(slotmuteButtonClicked())); connect(volumeSlider,SIGNAL(sliderMoved(int)),this,SLOT(slotVolumeChange(int))); slider->setRange(0,100); volumeSlider->setValue(50); data->slider = slider; connect(slider,SIGNAL(sliderMoved(int)),this,SLOT(seek(int))); data->streams_list = infoLabel; qDebug()<<"4"<<displayWnd->width()<<displayWnd->height(); } void Widget::realize_cb(QWidget *widget, CustomData *data) { guintptr window_handle; window_handle = (guintptr)(widget->winId()); gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(data->playbin2),window_handle); } void Widget::playButtonClicked(QPushButton *button, CustomData *data) { Q_UNUSED(button); GstStateChangeReturn ret; ret = gst_element_set_state (data->playbin2, GST_STATE_PLAYING); } 问答

Qt creator用QMultimedia做视频播放器没图像

07-15

如题,我从网上参考了相关教程自己写了一个(原码在最后),是在Deepin环境下,编译是没错的,结果运行时弹出的窗口是全黑的,视频播放不出来,但奇怪的是有声音,即使把窗口关了也还有声音。如果我换到windows环境下,把相关的库和文件都改成windows的,也是没有报错,但直接连黑窗口都弹不出来了。我用的Qt5.9.1,创建工程时用的是QWidget基类(用其他的没写出来)。在百度上找了很长时间,说是没有gstreamer0.10-ffmpeg解码器,我试过了,这个解码器根本就下载不了。截图如下: ![图片说明](https://img-ask.csdn.net/upload/201707/15/1500129864_271640.png) 关于安装这个解码器的问题,百度说直接从官网上找安装包手动下载,我把好多安装包下载完了,结果都提示不能安装:![图片说明](https://img-ask.csdn.net/upload/201707/15/1500130132_477133.png) 又有说是源的问题,我从网上找了好多源,然后挨个换,都不能安装。还有说系统要升级到最新,可是我也升级了啊,也没什么用。不想放弃,希望得到指点,如果能得到任何帮助,我将不胜感激。 原码如下: ![图片说明](https://img-ask.csdn.net/upload/201707/15/1500130526_572704.png) ![图片说明](https://img-ask.csdn.net/upload/201707/15/1500130595_767762.png) ![图片说明](https://img-ask.csdn.net/upload/201707/15/1500130636_716614.png) ![图片说明](https://img-ask.csdn.net/upload/201707/15/1500130685_741237.png) 那个player.ui我只是画了一些按钮,没有去实现,因为在播放这块儿卡住了。 这是.pro文件: #------------------------------------------------- # # Project created by QtCreator 2017-07-15T09:24:24 # #------------------------------------------------- QT += core gui\ multimedia\ multimediawidgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = test5 TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ player.cpp HEADERS += \ player.h FORMS += \ player.ui ```这是头文件player.h: #ifndef PLAYER_H #define PLAYER_H #include <QWidget> #include <QMediaPlayer> #include <QVideoWidget> namespace Ui { class Player; } class Player : public QWidget { Q_OBJECT public: explicit Player(QWidget *parent = 0); ~Player(); private: Ui::Player *ui; QVideoWidget *videow; QMediaPlayer *player; }; #endif // PLAYER_H ```这是main.cpp: #include "player.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Player w; w.show(); return a.exec(); } ```这是player.cpp: #include "player.h" #include "ui_player.h" Player::Player(QWidget *parent) : QWidget(parent), ui(new Ui::Player) { ui->setupUi(this); videow = new QVideoWidget(); player = new QMediaPlayer(); player->setVideoOutput(videow); player->setMedia(QUrl::fromLocalFile(QStringLiteral("/home/joe/Videos/U6-1.mpg"))); videow->show(); player->play(); } Player::~Player() { delete ui; } ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览