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 */