• VS+Qt实现视频播放(载入视频,播放视频,停止,进度条)


    在这里插入图片描述
    PlayerSlider中的代码:

    #ifndef PLAYERSLIDER_H
    #define PLAYERSLIDER_H
    
    #include <QSlider>
    #include <QMouseEvent>
    class PlayerSlider : public QSlider
    {
    	Q_OBJECT
    public:
    	PlayerSlider(QWidget * parent = 0);
    	void        setProgress(qint64);
    signals:
    	void        sigProgress(qint64);
    private:
    	bool        m_bPressed;
    protected:
    	void        mousePressEvent(QMouseEvent *);
    	void        mouseMoveEvent(QMouseEvent *);
    	void        mouseReleaseEvent(QMouseEvent *);
    };
    
    #endif // PLAYERSLIDER_H
    

    PlayerSlider.cpp中的代码:

    #include "PlayerSlider.h"
    
    PlayerSlider::PlayerSlider(QWidget * parent) : QSlider(parent)
    {
    	m_bPressed = false;
    }
    
    void PlayerSlider::mousePressEvent(QMouseEvent *e)
    {
    	m_bPressed = true;
    	QSlider::mousePressEvent(e);//必须有这句,否则手动不能移动滑块
    }
    
    void PlayerSlider::mouseMoveEvent(QMouseEvent *e)
    {
    	QSlider::mouseMoveEvent(e);//必须有这句,否则手动不能移动滑块
    }
    
    void PlayerSlider::mouseReleaseEvent(QMouseEvent *e)
    {
    	m_bPressed = false;
    	qint64 i64Pos = value();
    	emit sigProgress(i64Pos);
    
    	QSlider::mouseReleaseEvent(e);//必须有这句,否则手动不能移动滑块
    }
    
    void PlayerSlider::setProgress(qint64 i64Progress)
    {
    	if (!m_bPressed)
    		setValue(i64Progress);
    }
    

    VedioPlayer.h中的代码:

    #pragma once
    
    #include <QtWidgets/QWidget>
    #include "ui_VideoPlayer.h"
    #include <QtMultimedia>
    #include <QVideoWidget>
    
    class VideoPlayer : public QWidget
    {
    	Q_OBJECT
    
    public:
    	VideoPlayer(QWidget *parent = Q_NULLPTR);
    	bool            m_bReLoad;
    	public slots:
    	void            OnSetMediaFile(void);
    	void            OnSlider(qint64);
    	void            OnDurationChanged(qint64);
    	void            OnStateChanged(QMediaPlayer::State);
    
    private:
    	Ui::VideoPlayerClass ui;
    	QVideoWidget    *       m_pPlayerWidget;
    	QMediaPlayer    *       m_pPlayer;
    };
    

    VedioPlayer.cpp中的代码:

    #include "VideoPlayer.h"
    #include <QFileDialog>
    #include "ui_VideoPlayer.h"
    
    VideoPlayer::VideoPlayer(QWidget *parent)
    	: QWidget(parent)
    {
    	ui.setupUi(this);
    
    	m_pPlayer = new QMediaPlayer;
    	m_pPlayerWidget = new QVideoWidget;
    	m_pPlayer->setVideoOutput(m_pPlayerWidget);
    	ui.verticalLayout->addWidget(m_pPlayerWidget);
    
    	m_pPlayerWidget->setAutoFillBackground(true);
    	QPalette qplte;
    	qplte.setColor(QPalette::Window, QColor(0, 0, 0));
    	m_pPlayerWidget->setPalette(qplte);
    
    	//载入
    	connect(ui.BtnLoad, SIGNAL(clicked()), this, SLOT(OnSetMediaFile()));
    	//播放
    	connect(ui.BtnPlay, SIGNAL(clicked()), m_pPlayer, SLOT(play()));
    	//停止
    	connect(ui.BtnStop, SIGNAL(clicked()), m_pPlayer, SLOT(stop()));
    
    	connect(m_pPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnStateChanged(QMediaPlayer::State)));
    
    	ui.BtnStop->setEnabled(false);
    
    	//设置滑块行为
    	m_bReLoad = true;
    	ui.slider->setEnabled(false);
    	//ui.slider->setEnabled(false);
    	connect(m_pPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(OnSlider(qint64)));
    	connect(m_pPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(OnDurationChanged(qint64)));
    	connect(ui.slider, SIGNAL(sigProgress(qint64)), m_pPlayer, SLOT(setPosition(qint64)));
    }
    
    void VideoPlayer::OnSetMediaFile(void)
    {
    	QFileDialog dialog(this);
    	dialog.setFileMode(QFileDialog::AnyFile);
    	QStringList fileNames;
    	if (dialog.exec())
    		fileNames = dialog.selectedFiles();
    
    	if (!fileNames.empty())
    	{
    		m_pPlayer->setMedia(QUrl::fromLocalFile(fileNames[0]));
    		m_bReLoad = true;
    		ui.slider->setValue(0);
    	}
    }
    
    void VideoPlayer::OnSlider(qint64 i64Pos)
    {
    	ui.slider->setProgress(i64Pos);
    	//ui.horizontalSlider->setProgress(i64Pos);
    }
    
    void VideoPlayer::OnDurationChanged(qint64 i64Duration)
    {
    	if (i64Duration > 0 && m_bReLoad)
    	{
    		ui.slider->setRange(0, i64Duration);
    		m_bReLoad = false;
    	}
    }
    
    
    void VideoPlayer::OnStateChanged(QMediaPlayer::State enumState)
    {
    	if (QMediaPlayer::StoppedState == enumState)
    	{
    		ui.BtnPlay->setEnabled(true);
    		ui.BtnStop->setEnabled(false);
    		ui.slider->setEnabled(false);
    	}
    	else if (QMediaPlayer::PlayingState == enumState)
    	{
    		ui.BtnPlay->setEnabled(false);
    		ui.BtnStop->setEnabled(true);
    		ui.slider->setEnabled(true);
    	}
    }
    

    main.cpp中的代码:

    #include "VideoPlayer.h"
    #include <QtWidgets/QApplication>
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    	VideoPlayer w;
    	w.show();
    	return a.exec();
    }
    

    运行结果:
    在这里插入图片描述

  • 相关阅读:
    jvm类加载机制
    线程误区-join,wait(里边还是调用的wait)
    线程间通信wait和notify【All】简介
    指令重排序
    window.opener
    scrollIntoView()
    保持饥饿,保持愚蠢
    执行sql语句,不依靠实体 获取string值
    join函数详解
    html常用代码大全
  • 原文地址:https://www.cnblogs.com/xiaohai123/p/16399783.html
Copyright © 2020-2023  润新知