Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to play youtube video on the ui



  • I would like to play the video from youtube but I couldn't make it. Can anyone please help !
    Here are my code:

    QT       += core gui multimedia multimediawidgets // this is added in the .pro
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QMediaPlayer>
    #include <QVideoWidget>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QMediaPlayer *player = new QMediaPlayer;
            
        QMediaContent *mc=new QMediaContent(QUrl("https://www.youtube.com/watch?v=ksBjyegtBKE"));
        
        player->setMedia(*mc);
        
        
        player->setVolume(50);
        
        QVideoWidget *videoWidget = new QVideoWidget;
        
        videoWidget->resize(700,700);
        
        videoWidget->show();
        
        player->setVideoOutput(videoWidget);
        
        player->play();
    
        
        
        qDebug()<<player->availableMetaData()<<player->currentMedia().canonicalUrl();
        
        qDebug()<<player->errorString();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    These are the code in mainwindow.cpp

    and I got an error

    () QUrl("https://www.youtube.com/watch?v=ksBjyegtBKE")
    ""
    DirectShowPlayerService::doRender: Unresolved error code 0x80040218 ()

    How can I fix the problem or any other way to play the video ?

    Thank you for helping !


  • Moderators

    @greencow said in How to play youtube video on the ui:

    new QMediaContent(QUrl("https://www.youtube.com/watch?v=ksBjyegtBKE"));

    the url doesn't provide a video stream, but the youtube HTML page of the video.

    Take a look at this.
    Youtube videos use RMTP streaming protocol.



  • @raven-worx Thank you for helping !!!!


Log in to reply