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(); }