zoukankan      html  css  js  c++  java
  • qt中播放视频和音频

    主函数

    #include "studywidget.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        StudyWidget w;
        w.video();
        w.music();
        w.show();
    
        return a.exec();
    }

    函数实现头文件

    #ifndef STUDYWIDGET_H
    #define STUDYWIDGET_H
    
    #include <QWidget>
    #include <QImage>
    #include <QTimer>     // 设置采集数据的间隔时间
    #include <QMediaPlayer>
    #include <QDebug>
    #include <QDir>
    #include <QVideoWidget>
    namespace Ui {
        class StudyWidget;
    }
    
    class StudyWidget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit StudyWidget(QWidget *parent = 0);
        ~StudyWidget();
        void music();
        void video();
    private:
        Ui::StudyWidget *ui;
    };
    
    #endif // STUDYWIDGET_H

    函数实现cpp

    #include "studywidget.h"
    #include "ui_studywidget.h"
    
    
    StudyWidget::StudyWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::StudyWidget)
    {
        ui->setupUi(this);
    }
    
    StudyWidget::~StudyWidget()
    {
        delete ui;
    }
    void StudyWidget::video()
    {
        QMediaPlayer *myPlayer = new QMediaPlayer(this);
        QVideoWidget *video = new QVideoWidget(this);
        video->resize(1104, 622);
        myPlayer->setVideoOutput(video);
        myPlayer->setMedia(QUrl::fromLocalFile("/Users/ZX/Desktop/zx1/1.mp4"));
        myPlayer->play();
    }
    void StudyWidget::music()
    {
        // 音乐播放
        QMediaPlayer *myPlayer = new QMediaPlayer;
        //    QString runPath = QCoreApplication::applicationDirPath();// 获取当前exe所在路径
        //    QString absDir  = runPath + "/1.wav";
        QString absDir = "/Users/ZX/Desktop/zx1/mysound/davichi.mp3";
        //    QString current = QDir::currentPath();
        //    QString absDir  = current.mid(0, current.size() - 6) + "/mysound/davichi.mp3";
        qDebug() << absDir;
        myPlayer->setMedia(QUrl::fromLocalFile(absDir));
        myPlayer->setVolume(80);
        myPlayer->play();
    }





  • 相关阅读:
    串口通信
    PS如何将图片/照片转换为线稿
    merge_image.sh
    remk_dir.sh
    简单监控Tomcat连接池大小的命令以及其他简单命令
    一个挖矿病毒的简单发现历程
    IDEA运行tomcat8.5.35源代码
    mybatis 中 if-test 判断大坑
    浅析前端安全:如何防止XSS攻击
    浅析xss攻击原理、模拟xss攻击及防止xss攻击的方法
  • 原文地址:https://www.cnblogs.com/zx-hit/p/13549343.html
Copyright © 2011-2022 走看看