QT应用程序同时播放多个视频

时间:2016-02-10 06:35:26

标签: c++ multithreading qt video

我想在Qt上创建应用程序,它可以同时播放多个视频。我想申请安全摄像头,所以我必须同时显示所有摄像机的流。我在QMediaPlayer和QVideoWidget的帮助下制作了媒体应用程序,它支持所有类型的视频。

我也从bellow网站获得帮助,但我不想使用Vlc Lib。 Playing multiple video using libvlc and Qt

请指导我实现目的地的路径。

我应该使用Phonon吗?

我尝试了一些代码同时显示两个视频,但遇到了一些问题。

的main.cpp

#include "mainwindow.h"
#include <QApplication>

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"
#include <QGridLayout>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    for(int i=0; i<2; i++){
        for(int j=0; j<2; j++){

            player[i][j] = new QMediaPlayer(this);
            vw[i][j] = new QVideoWidget(this);
            player[i][j]->setVideoOutput(vw[i][j]);
            ui->graphicsView->setViewport(vw[i][j]);
            //ui->graphicsView_2->setViewport(vw[i][j]);

            slider = new QSlider(this);
            bar = new QProgressBar(this);
            slider1 = new QSlider(this);

            slider->setOrientation(Qt::Horizontal);

            ui->statusBar->addPermanentWidget(slider);
            ui->statusBar->addPermanentWidget(bar);
            ui->statusBar->addPermanentWidget(slider1);

            connect(player[i][j],&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
            connect(player[i][j],&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
            connect(slider,&QSlider::sliderMoved,player[i][j],&QMediaPlayer::setPosition);

            slider1->setValue(50);
            connect(slider1,&QSlider::sliderMoved,player[i][j],&QMediaPlayer::setVolume);

            connect(player[i][j],&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
            connect(player[i][j],&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);

        }
    }
}

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

void MainWindow::on_actionOpen_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this,"Open a File","","Video File (*.*)");
    on_actionStop_triggered();

    player[1][1]->setMedia(QUrl::fromLocalFile(filename));

    on_actionPlay_triggered();
}

void MainWindow::on_actionPlay_triggered()
{
    player[1][1]->play();
    ui->statusBar->showMessage("Playing");
}

void MainWindow::on_actionPause_triggered()
{
    player[1][1]->pause();
    ui->statusBar->showMessage("Paused...");
}

void MainWindow::on_actionStop_triggered()
{
    player[1][1]->stop();
    ui->statusBar->showMessage("Stopped");
}

void MainWindow::on_actionMute_triggered()
{
    player[1][1]->setMuted(1);
    ui->statusBar->showMessage("Muted...");
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QFileDialog>
#include <QProgressBar>
#include <QSlider>
#include <QWidget>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_actionOpen_triggered();

    void on_actionPlay_triggered();

    void on_actionPause_triggered();

    void on_actionStop_triggered();

    void on_actionMute_triggered();

private:
    Ui::MainWindow *ui;
    QMediaPlayer* player[2][2];
    QVideoWidget* vw[2][2];
    QProgressBar* bar;
    QSlider* slider;
    QSlider* slider1;

};

#endif // MAINWINDOW_H

mainwindow.ui enter image description here

应用图片: enter image description here

现在我想在Graphics小部件上播放相同的视频。可能吗?如果是,那怎么样? 任何人都可以指导我,我如何在mainwindo.cpp文件中的ui->GraphicsViewui->GraphicsView_2添加线程?

由于 Tejas Virpariya

1 个答案:

答案 0 :(得分:0)

我完成了,我可以同时播放两个视频。

我只更改了我的mainwindow.cpp文件。它不是一个完美的代码,但它是一个解决方案。

mainwindow.cpp

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    player = new QMediaPlayer;
    vw = new QVideoWidget;
    player->setVideoOutput(vw);
    ui->graphicsView->setViewport(vw);
    //ui->graphicsView_2->setViewport(vw1);

    slider = new QSlider(this);
    bar = new QProgressBar(this);
    slider1 = new QSlider(this);

    slider->setOrientation(Qt::Horizontal);

    ui->statusBar->addPermanentWidget(slider);
    ui->statusBar->addPermanentWidget(bar);
    ui->statusBar->addPermanentWidget(slider1);

    connect(player,&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
    connect(player,&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
    connect(slider,&QSlider::sliderMoved,player,&QMediaPlayer::setPosition);

    slider1->setValue(50);
    connect(slider1,&QSlider::sliderMoved,player,&QMediaPlayer::setVolume);

    connect(player,&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
    connect(player,&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);



    player1 = new QMediaPlayer;
    vw1 = new QVideoWidget;
    player1->setVideoOutput(vw1);
    //ui->graphicsView->setViewport(vw);
    ui->graphicsView_2->setViewport(vw1);

    slider2 = new QSlider(this);
    bar1 = new QProgressBar(this);
    slider3 = new QSlider(this);

    slider2->setOrientation(Qt::Horizontal);

    ui->statusBar->addPermanentWidget(slider2);
    ui->statusBar->addPermanentWidget(bar1);
    ui->statusBar->addPermanentWidget(slider3);

    connect(player1,&QMediaPlayer::durationChanged,slider2,&QSlider::setMaximum);
    connect(player1,&QMediaPlayer::positionChanged,slider2,&QSlider::setValue);
    connect(slider2,&QSlider::sliderMoved,player1,&QMediaPlayer::setPosition);

    slider3->setValue(50);
    connect(slider3,&QSlider::sliderMoved,player1,&QMediaPlayer::setVolume);

    connect(player1,&QMediaPlayer::durationChanged,bar1,&QProgressBar::setMaximum);
    connect(player1,&QMediaPlayer::positionChanged,bar1,&QProgressBar::setValue);
}

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

void MainWindow::on_actionOpen_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this,"Open a File","","Video File (*.*)");
    QString filename1 = "C:/Users/Public/Videos/Sample Videos/Wildlife.wmv";
    on_actionStop_triggered();

    player->setMedia(QUrl::fromLocalFile(filename));
    player1->setMedia(QUrl::fromLocalFile(filename1));

    on_actionPlay_triggered();
}

void MainWindow::on_actionPlay_triggered()
{
    player->play();
    player1->play();
    ui->statusBar->showMessage("Playing");
}

void MainWindow::on_actionPause_triggered()
{
    player->pause();
    player1->pause();
    ui->statusBar->showMessage("Paused...");
}

void MainWindow::on_actionStop_triggered()
{
    player->stop();
    player1->stop();
    ui->statusBar->showMessage("Stopped");
}

void MainWindow::on_actionMute_triggered()
{
    if(player->isMuted()){
        player->setMuted(0);
    }else{
        player->setMuted(1);
    }
    if(player1->isMuted()){
        player1->setMuted(0);
    }else{
        player1->setMuted(1);
    }
    ui->statusBar->showMessage("Muted...");
}