Qt#第十四章:QMediaPlayer

QMediaPlayer 为媒体播放器,主要用于播放歌曲、网络收音机等功能;QMediaPlaylist 专门用于媒体播放内容的列表
QT += multimedia
播放视频加上  QT += multimediawidgets。QVideoWidget 类用于媒体对象生成的视频
下面为播放视频的一个demo:
继承QVideoWidget 类 
#ifndef QMYSELFVIDEOWIDGET_H
#define QMYSELFVIDEOWIDGET_H

#include <QWidget>
#include <QObject>
#include <QVideoWidget>
#include <QMediaPlayer>

#include <QKeyEvent>
#include <QMouseEvent>

class QMyselfVideoWidget : public QVideoWidget
{
public:
    QMyselfVideoWidget(QWidget *parent=Q_NULLPTR);

    void SetMediaPlayer(QMediaPlayer *player);

private:
    QMediaPlayer *theplayer;

protected:
    void keyPressEvent(QKeyEvent *event); // 键盘按键事件,当用户按下ESC退出全局播放状态
    void mousePressEvent(QMouseEvent *event); // 鼠标按键事件,当用户控制暂停和继续播放

};

#endif // QMYSELFVIDEOWIDGET_H
#include "qmyselfvideowidget.h"

QMyselfVideoWidget::QMyselfVideoWidget(QWidget *parent):QVideoWidget(parent)
{

}
void QMyselfVideoWidget::SetMediaPlayer(QMediaPlayer *player)
{
    //    设置播放器操作
    theplayer=player;
}

void QMyselfVideoWidget::keyPressEvent(QKeyEvent *event) // 键盘按键事件,当用户按下ESC退出全局播放状态
{
    if((event->key()==Qt::Key_Escape)&& (isFullScreen()))
    {
        setFullScreen(false);
        event->accept();
        QVideoWidget::keyPressEvent(event);
    }
}

void QMyselfVideoWidget::mousePressEvent(QMouseEvent *event) // 鼠标按键事件,当用户控制暂停和继续播放
{
    if(event->button()==Qt::LeftButton)
    {
        if(theplayer->state()==QMediaPlayer::PlayingState)
            theplayer->pause();
        else
            theplayer->play();
    }

    QVideoWidget::mousePressEvent(event);
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qmyselfvideowidget.h"
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>
#include <QFileDialog>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;

    QMediaPlayer *player;
    QString drtTime; // 视频文件时间长度
    QString posTime; // 播放视频当前位置


private slots: // 自定义槽函数
    void onstatebuttonchanged(QMediaPlayer::State state); // 控制按钮状态切换
    void ondrttimechanged(qint64 drt); // 视频文件时间长度、更新变化
    void onpostimechanged(qint64 pos); // 播放视频当前位置时间、更新变化


    void on_pushButton_Open_clicked();
    void on_pushButton_Play_clicked();
    void on_pushButton_Pause_clicked();
    void on_pushButton_Stop_clicked();
    void on_pushButton_Volumn_clicked();
    void on_horizontalSlider_Volumn_valueChanged(int value);
    void on_horizontalSlider_position_valueChanged(int value);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    player=new QMediaPlayer(this);
    player->setNotifyInterval(1000);
    player->setVideoOutput(ui->graphicsView); // 视频显示组件

    ui->graphicsView->SetMediaPlayer(player); // 设置显示组件的关联播放器

    connect(player,SIGNAL(stateChanged(QMediaPlayer::State)),this,
            SLOT(onstatebuttonchanged(QMediaPlayer::State)));

    connect(player,SIGNAL(positionChanged(qint64)),this,
            SLOT(onpostimechanged(qint64)));

    connect(player,SIGNAL(durationChanged(qint64)),this,
            SLOT(ondrttimechanged(qint64)));



}

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

void MainWindow::onstatebuttonchanged(QMediaPlayer::State state) // 控制按钮状态切换
{
    ui->pushButton_Play->setEnabled(!(state==QMediaPlayer::PlayingState));
    ui->pushButton_Pause->setEnabled(state==QMediaPlayer::PlayingState);
    ui->pushButton_Stop->setEnabled(state==QMediaPlayer::PlayingState);
}

void MainWindow::ondrttimechanged(qint64 drt) // 视频文件时间长度、更新变化
{
    ui->horizontalSlider_position->setMaximum(drt);

    int sec=drt/1000; // 秒
    int min=sec/60; // 分
    sec=sec%60; // 余数秒
    drtTime=QString::asprintf("%d:%d",min,sec);
    ui->label_Time->setText(posTime+"|"+drtTime);
}

void MainWindow::onpostimechanged(qint64 pos) // 播放视频当前位置时间、更新变化
{
    if(ui->horizontalSlider_position->isSliderDown())
        return ; // 如果正在手动滑动条,则直接退出

    ui->horizontalSlider_position->setSliderPosition(pos);

    int sec=pos/1000; // 秒
    int min=sec/60; // 分
    sec=sec%60; // 余数秒
    posTime=QString::asprintf("%d:%d",min,sec);
    ui->label_Time->setText(posTime+"|"+drtTime);

}

void MainWindow::on_pushButton_Open_clicked()
{
    QString currentpath=QDir::homePath(); // 获取系统当前目录
    QString dlgtitle="请选择视频文件";
    QString filter="所有文件(*.*);;mp4文件(*.mp4)"; // 文件过滤器
    QString strfile=QFileDialog::getOpenFileName(this,dlgtitle,currentpath,filter);

    if(strfile.isEmpty())
        return ;


    QFileInfo fileinfo(strfile);
    ui->label_Name->setText(fileinfo.fileName());
    player->setMedia(QUrl::fromLocalFile(strfile)); // 设置播放文件
    player->play();

}

void MainWindow::on_pushButton_Play_clicked()
{
    player->play();

}

void MainWindow::on_pushButton_Pause_clicked()
{
    player->pause();

}

void MainWindow::on_pushButton_Stop_clicked()
{
    player->stop();

}

void MainWindow::on_pushButton_Volumn_clicked()
{
    bool bmute=player->isMuted();
    player->setMuted(!bmute);

    if(bmute)
        ui->pushButton_Volumn->setIcon(QIcon(":/new/prefix1/images/volumn.bmp"));
    else
        ui->pushButton_Volumn->setIcon(QIcon(":/new/prefix1/images/mute.bmp"));
}

void MainWindow::on_horizontalSlider_Volumn_valueChanged(int value)
{
    player->setVolume(value);

}

void MainWindow::on_horizontalSlider_position_valueChanged(int value)
{
    player->setPosition(value);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值