Qt视频播放器

普通的方式是
Qt中实现视频播放器的基本步骤可以概括为以下几个方面:

使用 QMediaPlayer 类:这是Qt多媒体框架中的一个核心类,用于播放音频和视频文件。它支持多种音频和视频格式,并且能够在多种操作系统上运行。使用时需要在项目的 .pro 文件中添加 QT += multimedia 。

使用 QVideoWidget 类:作为视频显示组件,它可以将 QMediaPlayer 解析的视频流显示出来。你可以将 QVideoWidget 添加到应用程序的用户界面中,并设置为 QMediaPlayer 的视频输出设备 。

设置视频和音频输出:通过 setVideoOutput() 方法将 QMediaPlayer 与 QVideoWidget 关联,从而在小部件中显示视频。如果需要单独控制音频,可以使用 QAudioOutput 类 。

实现播放控制:通过 QMediaPlayer 的方法如 play()、pause()、stop() 控制视频播放。同时,可以利用其提供的信号和槽机制来响应播放事件,例如连接 stateChanged() 信号到自定义槽以更新播放状态 。

进度条和时间显示:使用 QSlider 控件来实现进度条,通过连接 QMediaPlayer 的 positionChanged() 信号来更新滑块位置。同时,可以更新视频的当前时间和总时间显示 。

自定义用户界面:根据需要设计用户界面,添加播放、暂停、停止等按钮,以及音量控制、倍速播放等功能 。

处理文件和流媒体:支持本地视频文件播放,也可以扩展以支持网络流媒体播放 。

安装必要的解码器:由于 QMediaPlayer 依赖于操作系统提供的解码器,可能需要安装额外的解码器以支持更多格式的视频文件,如K-Lite解码器包 。

错误处理:注意处理编译和运行时的错误,例如链接错误或缺少解码器导致的问题 。

通过上述步骤,你可以构建一个基本的视频播放器。如需高级功能,如视频预览、播放列表管理等,可能需要更深入地定制开发或使用额外的第三方库。
请添加图片描述

为了高效的处理播放视频,需要用到ffmpeg

MAKEFILE_GENERATOR
INCLUDEPATH +=  $$PWD/plugin/
INCLUDEPATH +=  $$PWD/plugin/ffmpeg-win32/include/
INCLUDEPATH +=  $$PWD/plugin/pthreads-win32/include/
INCLUDEPATH +=  $$PWD/plugin/ffplayer/include/
LIBS += -lwinmm -lgdi32 -luser32 -ld3d9
LIBS += -L$$PWD/plugin/ffmpeg-win32/bin/
LIBS += -lavcodec -lavformat -lavutil -lswresample -lswscale
LIBS += -L$$PWD/plugin/pthreads-win32/lib/ -lpthreadVC2
debug:LIBS += -L$$PWD/plugin/ffplayer/lib/Win32_MSVC2013.Debug/ -lffplayer
release:LIBS += -L$$PWD/plugin/ffplayer/lib/Win32_MSVC2013.Release/ -lffplayer

#include "ctrlwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include "SunFun.h"
#include "mainwidget.h"
extern MainWidget *g_mainWidget;

CtrlWidget::CtrlWidget(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    fast_backward = new PushButton(this);//快退
    progress_slider = new ClickableSlider(this);//进度条
    progress_slider->setObjectName(QString::fromLocal8Bit("progress_slider"));
    progress_slider->setOrientation(Qt::Horizontal);
    progress_slider->setRange(0,100);
    progress_slider->setValue(0);
    fast_forward = new PushButton(this);//快进
    time_lable = new QLabel(this); //时间
    open_button = new PushButton(this);//打开
    screenshot_button = new PushButton(this);//截图
    save_button = new PushButton(this);//另存为
    stop_button = new PushButton(this);//停止
    previous_button = new PushButton(this);//前一个
    previous_button->setObjectName("Backward");
    play_button = new PushButton(this);//播放
    pause_button = new PushButton(this);//暂停
    next_button = new PushButton(this);//下一个
    next_button->setObjectName("Forward");
    vol_button = new PushButton(this);//音量
    vol_slider = new ClickableSlider(this);//音量滑块
    vol_slider->setObjectName(QString::fromLocal8Bit("vol_slider"));
    vol_slider->setOrientation(Qt::Horizontal);
    vol_slider->setRange(0,100);
    vol_slider->setValue(100);
    full_button = new PushButton(this); //全屏
    full_restore_button = new PushButton(this); //退出全屏
    sizebox = new PushButton(this);
    sizebox->setEnabled(false);

    fast_backward->setPicName(QString(":/ctrl/fast_backward"));
    QFile file(":/qss/QSlider");
    file.open(QFile::ReadOnly);
    QByteArray byte_array = file.readAll();
    file.close();
    progress_slider->setStyleSheet(byte_array);
    progress_slider->setFixedHeight(11);
    vol_slider->setStyleSheet(byte_array);

    fast_forward->setPicName(QString(":/ctrl/fast_forward"));
    time_lable->setStyleSheet("color:white;");
    time_lable->setText(QString("00:00:00/00:00:00"));
    open_button->setPicName(QString(":/ctrl/open"));
    screenshot_button->setPicName(QString(":/ctrl/screenshot"));
    save_button->setPicName(QString(":/ctrl/save"));
    stop_button->setPicName(QString(":/ctrl/stop"));
    previous_button->setPicName(QString(":/ctrl/previous"));
    play_button->setPicName(QString(":/ctrl/play"));
    pause_button->setPicName(QString(":/ctrl/pause"));
    next_button->setPicName(QString(":/ctrl/next"));
    vol_button->setPicName(QString(":/ctrl/vol"));
    full_button->setPicName(QString(":/ctrl/full"));
    full_restore_button->setPicName(QString(":/ctrl/full_restore"));
    sizebox->setPicName(QString(":/ctrl/sizebox"));

    fast_backward->setToolTip(QString::fromLocal8Bit("快退10秒"));
    fast_forward->setToolTip(QString::fromLocal8Bit("快进10秒"));
    open_button->setToolTip(QString::fromLocal8Bit("打开"));

    screenshot_button->setToolTip(QString::fromLocal8Bit("截图"));
    save_button->setToolTip(QString::fromLocal8Bit("另存为"));
    stop_button->setToolTip(QString::fromLocal8Bit("停止"));
    previous_button->setToolTip(QString::fromLocal8Bit("左键:后退,右键:上一个"));
    play_button->setToolTip(QString::fromLocal8Bit("播放"));
    pause_button->setToolTip(QString::fromLocal8Bit("暂停"));
    next_button->setToolTip(QString::fromLocal8Bit("左键:前进,右键:下一个"));
    vol_button->setToolTip(QString::fromLocal8Bit("静音"));
    full_button->setToolTip(QString::fromLocal8Bit("全屏"));
    full_restore_button->setToolTip(QString::fromLocal8Bit("退出全屏"));

    QHBoxLayout *layout1 = new QHBoxLayout();
    layout1->setContentsMargins(0,2,0,0);
    layout1->setSpacing(0);
    layout1->addWidget(fast_backward,0,Qt::AlignTop);
    layout1->addWidget(progress_slider,0,Qt::AlignTop);
    layout1->addWidget(fast_forward,0,Qt::AlignTop);

    QHBoxLayout *layout2left = new QHBoxLayout();
    layout2left->setContentsMargins(10,0,0,8);
    layout2left->setSpacing(5);
    layout2left->addWidget(time_lable, 0, Qt::AlignVCenter);
    layout2left->addWidget(open_button, 0, Qt::AlignVCenter);
    layout2left->addWidget(screenshot_button, 0, Qt::AlignVCenter);
    //layout2left->addWidget(save_button, 0, Qt::AlignVCenter);


    layout2left->addStretch();
    layout2left->addWidget(stop_button, 0,Qt::AlignVCenter);

    QHBoxLayout *layout2mid = new QHBoxLayout();
    layout2mid->setContentsMargins(0,0,0,4);
    layout2mid->setSpacing(5);
    layout2mid->addWidget(previous_button, 0,Qt::AlignVCenter);
    layout2mid->addWidget(play_button, 0, Qt::AlignCenter);
    pause_button->hide();
    layout2mid->addWidget(pause_button, 0, Qt::AlignCenter);
    layout2mid->addWidget(next_button, 0,Qt::AlignVCenter);

    QHBoxLayout *layout2right = new QHBoxLayout();
    layout2right->setContentsMargins(0,0,2,2);
    layout2right->setSpacing(5);
    layout2right->addSpacing(5);
    layout2right->addWidget(vol_button, 0,Qt::AlignVCenter);
    layout2right->addWidget(vol_slider, 0,Qt::AlignLeft);
    layout2right->addStretch();
    layout2right->addWidget(full_button, 0, Qt::AlignVCenter);
    full_restore_button->hide();
    layout2right->addWidget(full_restore_button, 0, Qt::AlignVCenter);
    layout2right->addWidget(sizebox, 0, Qt::AlignBottom | Qt::AlignRight);

    QHBoxLayout *layout2 = new QHBoxLayout();
    layout2->setContentsMargins(0,0,0,0);
    layout2->addLayout(layout2left);
    layout2->addLayout(layout2mid);
    layout2->addLayout(layout2right);

    QVBoxLayout *main_layout = new QVBoxLayout();
    main_layout->setContentsMargins(0,0,0,0);
    main_layout->addLayout(layout1);
    main_layout->addLayout(layout2);
    setLayout(main_layout);
    this->setFixedHeight(80);

    //信号
    connect(fast_backward,SIGNAL(clicked()),this,SIGNAL(sigBackward()));
    connect(fast_forward,SIGNAL(clicked()),this,SIGNAL(sigForward()));
    connect(previous_button,SIGNAL(clicked()),this,SIGNAL(sigBackward()));
    connect(next_button,SIGNAL(clicked()),this,SIGNAL(sigForward()));
    connect(open_button,SIGNAL(clicked()),this,SIGNAL(sigOpen()));
    connect(screenshot_button,SIGNAL(clicked()),this,SIGNAL(sigScreenshot()));
    connect(save_button,SIGNAL(clicked()),this,SIGNAL(sigSave()));


    connect(stop_button,SIGNAL(clicked()),this,SIGNAL(sigStop()));
    connect(play_button,SIGNAL(clicked()),this,SIGNAL(sigPlay()));
    connect(pause_button,SIGNAL(clicked()),this,SIGNAL(sigPause()));
    connect(vol_button,SIGNAL(clicked()),this,SIGNAL(sigVol()));
    connect(vol_slider,SIGNAL(sliderMoved(int)),this,SIGNAL(sigVolSeek(int)));
    connect(progress_slider,SIGNAL(sliderMoved(int)),this,SIGNAL(sigSeek(int)));
    connect(full_button, SIGNAL(clicked()), this, SIGNAL(sigFullScreen()));
    connect(full_restore_button, SIGNAL(clicked()), this, SIGNAL(sigRestoreFullScreen()));

    open_button->setDefault(true);

    setAutoFillBackground(true);
    QPalette palette;
    palette.setColor(QPalette::Background, QColor(56,58,72));
    setPalette(palette);

    //可获取鼠标跟踪效果
    setMouseTracking(true);
}


bool CtrlWidget::event(QEvent *event)
{
    if (event->type() == MyCustomEventTypeA)
    {
        // processing...
        emit sigSeek(progress_slider->value());
        return true;
    }
    else if (event->type() == MyCustomEventTypeB)
    {
        // processing...
        emit sigVolSeek(vol_slider->value());
        return true;
    }

    return QWidget::event(event);
}

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值