qt MP4播放器 笔记1

 控件

 

 pro文件

 修改

QT       += core gui multimediawidgets multimedia

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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 \
    dialog.cpp

HEADERS += \
    dialog.h

FORMS += \
    dialog.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 头文件

#ifndef DIALOG_H
#define DIALOG_H

#pragma execution_character_set("utf-8")

#include <QDialog>

#include <QMediaPlayer>
#include<QvideoWidget>
#include<QGraphicsvideoItem>
#include<QFileDialog>
#include<QGraphicsScene>
#include<QMessageBox>
#include<QMediaPlaylist>

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private:
    Ui::Dialog *ui;
    QMediaPlayer *player;//播放对象
    QGraphicsVideoItem *videoitem;//播放场景
    QString durationtime,psitiontime;//播放时间
private slots:
    void onStateChanged(QMediaPlayer::State state);
    void onDurationChanged(qint64 duration);
    void onPositionChanged(qint64 position);

    void on_Open_clicked();
    void on_Player_clicked();
    void on_Pause_clicked();
    void on_stop_clicked();
    void on_Sound_clicked();
};
#endif // DIALOG_H

 

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    //固定窗口尺寸(又用户设计窗口为准)
     setFixedSize(width(),height());
     //UI样式设计

     //视频播器初始化参数
     player=new QMediaPlayer(this);
     player->setNotifyInterval(2000);

     QGraphicsScene *sence=new QGraphicsScene(this);
     ui->g->setScene(sence);

     videoitem=new QGraphicsVideoItem;
     videoitem->setSize(QSize(ui->g->width(),ui->g->height()));
     videoitem->setFlags(QGraphicsItem::ItemIsMovable|
                         QGraphicsPathItem::ItemIsSelectable|
                         QGraphicsItem::ItemIsFocusable);

     sence->addItem(videoitem);
     player->setVideoOutput(videoitem);

}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::onStateChanged(QMediaPlayer::State state){}
void Dialog::onDurationChanged(qint64 duration){}
void Dialog::onPositionChanged(qint64 position){}
//打开寻找视频文件
void Dialog::on_Open_clicked()
{
    QString strCurrentPath=QDir::homePath();//获取系统当前目录
    QString strDlgTitle="";//打开对话框的标题名称
    QString strFilter="MP4 Files(*.mp4);;All Files(*.*);";//打开视频文件过滤器
    QString strAllFiles=QFileDialog::getOpenFileName(this,strDlgTitle,strCurrentPath,strFilter);
    //
    //QMessageBox::information(this,"提示",strFilter,QMessageBox::Yes|QMessageBox::No);
    if(strAllFiles.isEmpty())
    {
         QMessageBox::information(this,"提示",strFilter,QMessageBox::Yes|QMessageBox::No);
        return;
    }

    QFileInfo FileInfo(strAllFiles);//获取文件信息
    ui->label1->setText(FileInfo.fileName());//修改UI的文件名
    player->setMedia(QUrl::fromLocalFile(strAllFiles));
    player->play();





}

void Dialog::on_Player_clicked()
{

}

void Dialog::on_Pause_clicked()
{

}

void Dialog::on_stop_clicked()
{

}

void Dialog::on_Sound_clicked()
{

}

 main.cpp

#include "dialog.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

 

 

 


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值