• Qt媒体播放之QMediaPlayer


    1.音乐播放

    /*Header:
    #include <QMediaPlayer> 
    qmake:
    QT += multimedia
    */
    QMediaPlayer *player = new QMediaPlayer();
    /*从本地读取*/
    player->setMedia(QUrl("F:/pianai.mp3"));
    player->setMedia(QMediaContent(QUrl::fromLocalFile("F:/pianai.mp3")));
    player->setMedia(QUrl("qrc:/static/pianai.mp3"));
    /*网络读取*/
    player->setMedia(QMediaContent(QUrl("http://s320.xiami.net/348/2110080348/2102495981/1795105593_1516272907214.mp3?ccode=xiami_mac_&expire=86400&duration=210&psid=fac47f82a061b1bd1e408f6ad353a7ed&ups_client_netip=129.204.235.253&ups_ts=1608702323&ups_userid=0&utid=&vid=1795105593&fn=1795105593_1516272907214.mp3&vkey=Bed34e421fe1306ac9bf9095e5793b2a9")));
    player->setVolume(50);
    player->play();

    2.视频播放

    /*
    Header:
    #include<QMediaPlaylist>
    #include<QVideoWidget>
    #include<QMediaPlayer>
    qmake:
    QT += multimedia multimediawidgets
    */

     播放多个视频(也可以播放多个音乐)

    QHBoxLayout *mainLayout=new QHBoxLayout(this);
    QMediaPlaylist *playlist = new QMediaPlaylist();
    /*本地读取*/
    playlist->addMedia(QMediaContent(QUrl::fromLocalFile("F:/1.mp4")));
    playlist->addMedia(QUrl("qrc:/static/1.mp4"));
    playlist->addMedia(QUrl("F:/1.mp4"));
    /*网络读取*/
    playlist->addMedia(QMediaContent(QUrl("https://v-cdn.zjol.com.cn/280443.mp4")));
    playlist->addMedia(QMediaContent(QUrl("https://v-cdn.zjol.com.cn/276982.mp4")));
    playlist->setCurrentIndex(1);
    
    QMediaPlayer *player = new QMediaPlayer();
    player->setPlaylist(playlist);
    
    QVideoWidget *videoWidget = new QVideoWidget();
    player->setVideoOutput(videoWidget);
    mainLayout->addWidget(videoWidget);
    player->play();
    

      

      

      播放单个视频

        QHBoxLayout *mainLayout=new QHBoxLayout(this);
        QVideoWidget *videoWidget = new QVideoWidget();
        QMediaPlayer *player = new QMediaPlayer();
         /*从本地读取*/
        player->setMedia(QUrl("qrc:/static/1.mp4"));
        player->setMedia(QUrl("F:/1.mp4"));
        player->setMedia(QMediaContent(QUrl::fromLocalFile("F:/1.mp4")))
        /*网络读取*/
         player->setMedia(QMediaContent(QUrl("https://v-cdn.zjol.com.cn/280443.mp4")));
         player->setVideoOutput(videoWidget);
         mainLayout->addWidget(videoWidget);
         player->play();
    

      

      

      

    播放视频,本地需要安装视频解码器

         比如:

         

    /*
    K-Lite:http://www.codecguide.com
    
    LAVFilters:https://www.free-codecs.com
    
    */
    

      

  • 相关阅读:
    吊打面试官系列:Redis 性能优化的 13 条军规大全
    Laravel 7.6 发布
    Laravel 8 新功能:使用 schema:dump 来加速 Migration 和测试
    php中常用的4种运行方式
    基于 Redis 的订阅与发布
    [ida]使用pycharm编写IDApython
    visual studio 配置使生成的pdb文件中包含所有符号
    D/B位、一致与非一致代码段、向下拓展的实验与总结
    [debug] CE指针扫描扫出来为空
    error LNK2019: 无法解析的外部符号 _main,该符号在函数___tmainCRTStartup 中被引用
  • 原文地址:https://www.cnblogs.com/navysummer/p/14178245.html
Copyright © 2020-2023  润新知