QT(5.9.9 )creator做一个播放器遇到的问题与解决

代码如下(需要自取)(已测试过完美运行)

MyMediaPlayer.pro

QT       += core gui
QT       += multimedia
QT       += multimediawidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include  //文件查找对话框
#include  //播放器
#include  //播放队列
#include  //视频显示窗口
#include   //绘图事件
#include 

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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



private slots:
    void on_OpenButton_clicked();

    void on_PlayButton_clicked();

    void on_pauseButton_clicked();

    void on_pushButton_4_clicked();
protected:
    void paintEvent(QPaintEvent*e);

private:
    Ui::MainWindow *ui;
    QMediaPlayer* pPlayer;
    QMediaPlaylist* pPlayerList;
    QVideoWidget* pVideoWidget;
};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置标题
    setWindowTitle("QtCPP");

    //把播放器对象  播放队列对象  显示视频界面的对象做出来
    pPlayer = new QMediaPlayer;
    pPlayerList = new QMediaPlaylist;
    pVideoWidget = new QVideoWidget(ui->label);

    //pVideoWidget->resize(ui->label->size());
    //设置播放器的播放队列
    pPlayer->setPlaylist(pPlayerList);
    //设置播放器的显示窗口
    pPlayer->setVideoOutput(pVideoWidget);

}

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


//OpenButton按钮被点击
void MainWindow::on_OpenButton_clicked()
{
    //qDebug()<<"这个按钮被点击了";
    //打开一个文件查找框
    //  \微软  /GNU
    QStringList fileNames=QFileDialog::getOpenFileNames(this,"let me see see",
                                                        "C:/Users/DYT/Desktop",
                                                        "allfiles(*.*);;"
                                                        "MP3(*.mp3);;"
                                                        "MP4(*.mp4);;"
                                                        "MKV(*.mkv);;"
                                                        "AVI(*.avi)");
    pPlayerList->clear();//先清空播放队列

    foreach(QString const&str,fileNames){//在QStringList对象中循环
        //qDebug() << str;
        //把选中的音视频文件添加到播放队列中
        QUrl url(str);
        pPlayerList->addMedia(url);

    }
}
//播放函数
void MainWindow::paintEvent(QPaintEvent*e){
    pVideoWidget->resize(ui->label->size());
}
void MainWindow::on_PlayButton_clicked()
{
    pPlayer->play();
}

void MainWindow::on_pauseButton_clicked()
{
    pPlayer->pause();
}

void MainWindow::on_pushButton_4_clicked()
{
    pPlayer->stop();
}

mainwindow.ui



 MainWindow
 
  
   
    0
    0
    560
    423
   
  
  
   MainWindow
  
  
   
    
     
      
       
        16777215
        45
       
      
      
       
        
         
          打开
         
        
       
       
        
         
          播放
         
        
       
       
        
         
          暂停
         
        
       
       
        
         
          停止
         
        
       
      
     
    
    
     
      
       Qt::Horizontal
      
     
    
    
     
      
       dyt影音
      
      
       Qt::AlignCenter
      
     
    
   
  
  
   
    
     0
     0
     560
     26
    
   
  
  
 
 
 

遇到的问题:

1.播放视频无法播放,出现下面的代码

解放方案:

缺少一个插件, LAV Filters可以去搜索下载,github有install版本的下载它,一直下一步就能装好。

2.各种报错

解决方案:

检查

QT       += multimedia
QT       += multimediawidgets

这两个代码是否正确添加

前后类名是否一致等。

你可能感兴趣的:(qt5,c++,windows)