Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. play rtsp stream QMultimedia
Qt 6.11 is out! See what's new in the release blog

play rtsp stream QMultimedia

Scheduled Pinned Locked Moved General and Desktop
qmultimediaqmediaplayerqgraphicsvideoistream
1 Posts 1 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SansBlague
    wrote on last edited by SansBlague
    #1

    hi,

    i tried to play a rtsp stream, that is streammed with VLC on localhost

    #include "VideoPlayer2.h"
    
    #include <QtWidgets>
    #include <qvideowidget.h>
    #include <qvideosurfaceformat.h>
    #include <QGraphicsScene>
    #include <QGraphicsVideoItem>
    
    VideoPlayer2::VideoPlayer2(QWidget *parent)
        : QWidget(parent)
        , mediaPlayer(0, QMediaPlayer::StreamPlayback)
        , playButton(0)
        , positionSlider(0)
        , errorLabel(0)
    {
        QGraphicsView * view=new QGraphicsView(this);
        //QVideoWidget *videoWidget = new QVideoWidget;
        QGraphicsVideoItem *videoItem= new QGraphicsVideoItem();
        QGraphicsScene * scene =new QGraphicsScene(this);
        scene->addItem(videoItem);
        view->setScene(scene);
    
        QAbstractButton *openButton = new QPushButton(tr("Open..."));
        connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
    
        playButton = new QPushButton;
        playButton->setEnabled(false);
        playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    
        connect(playButton, SIGNAL(clicked()),
                this, SLOT(play()));
    
        positionSlider = new QSlider(Qt::Horizontal);
        positionSlider->setRange(0, 0);
    
        connect(positionSlider, SIGNAL(sliderMoved(int)),
                this, SLOT(setPosition(int)));
    
        errorLabel = new QLabel;
        errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
    
        QBoxLayout *controlLayout = new QHBoxLayout;
        controlLayout->setMargin(0);
        controlLayout->addWidget(openButton);
        controlLayout->addWidget(playButton);
        controlLayout->addWidget(positionSlider);
    
        QBoxLayout *layout = new QVBoxLayout;
       // layout->addWidget(videoWidget);
    	layout->addWidget(view);
        layout->addLayout(controlLayout);
        layout->addWidget(errorLabel);
    
        setLayout(layout);
    
        mediaPlayer.setVideoOutput(videoItem);
    	//mediaPlayer.setMedia(
        connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),
                this, SLOT(mediaStateChanged(QMediaPlayer::State)));
        connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
        connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
        connect(&mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError()));
        view->show();
    }
    
    VideoPlayer2::~VideoPlayer2()
    {
    }
    
    void VideoPlayer2::openFile()
    {
        errorLabel->setText("");
    
       // QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
    	QString fileName ="rtsp://localhost:8554/toto";
        if (!fileName.isEmpty()) {
            mediaPlayer.setMedia(QUrl(fileName));
            playButton->setEnabled(true);
        }
    }
    
    void VideoPlayer2::play()
    {
        switch(mediaPlayer.state()) {
        case QMediaPlayer::PlayingState:
            mediaPlayer.pause();
            break;
        default:
            mediaPlayer.play();
            break;
        }
    }
    
    void VideoPlayer2::mediaStateChanged(QMediaPlayer::State state)
    {
        switch(state) {
        case QMediaPlayer::PlayingState:
            playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
            break;
        default:
            playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
            break;
        }
    }
    
    void VideoPlayer2::positionChanged(qint64 position)
    {
        positionSlider->setValue(position);
    }
    
    void VideoPlayer2::durationChanged(qint64 duration)
    {
        positionSlider->setRange(0, duration);
    }
    
    void VideoPlayer2::setPosition(int position)
    {
        mediaPlayer.setPosition(position);
    }
    
    void VideoPlayer2::handleError()
    {
        playButton->setEnabled(false);
        errorLabel->setText("Error: " + mediaPlayer.errorString());
    }
    
    

    But it does not work
    here is what my error label says: " error: Failed to load source "
    Can you please tell me why ?
    help help I'm desperate

    by the way, I'm using Qt5.3.2 visualStudio 2010
    Thank you

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved