Ejemplo n.º 1
0
MainWindow::MainWindow(QWidget *parent, const QString& theHostName)
    : QMainWindow(parent), hostName(theHostName) {

    setObjectName(QStringLiteral("MainWindow"));
    resize(400, 300);

    QMediaPlaylist* playlist = new QMediaPlaylist;
    playlist->addMedia(QUrl("http://" + hostName + "/~andreas/out.mpg"));
//    playlist->addMedia(QUrl("http://" + hostName + ":1234/dvd.mp4"));

    player = new QMediaPlayer(this); // , QMediaPlayer::StreamPlayback);
    player->setPlaylist(playlist);

    QWidget* centralWidget = new QWidget(this);
    QBoxLayout* mainLayout = new QVBoxLayout();
    centralWidget->setLayout(mainLayout);

    QWidget* buttonArea = new QWidget(this);
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    buttonArea->setLayout(buttonLayout);

    QVideoWidget* videoWidget = new QVideoWidget(centralWidget);
    mainLayout->addWidget(videoWidget, 1);
    mainLayout->addWidget(buttonArea);

    connectButton = new QPushButton("Connect", buttonArea);
    startPlayButton = new QPushButton("Play", buttonArea);
    startPlayButton->setEnabled(false);
    stopPlayButton = new QPushButton("Stop", buttonArea);
    stopPlayButton->setEnabled(false);
    streamButton = new QPushButton("Stream", buttonArea);
    streamButton->setEnabled(false);
    buttonLayout->addWidget(connectButton);
    buttonLayout->addWidget(startPlayButton);
    buttonLayout->addWidget(stopPlayButton);
    buttonLayout->addWidget(streamButton);

    tcpSocket = new QTcpSocket(this);

    connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
            this,   SLOT(statusChanged(QMediaPlayer::MediaStatus)));
    connect(connectButton, SIGNAL(clicked()),
            this,   SLOT(doConnect()));
    connect(startPlayButton, SIGNAL(clicked()),
            this,   SLOT(doStartPlay()));
    connect(stopPlayButton, SIGNAL(clicked()),
            this,   SLOT(doStopPlay()));
    connect(streamButton, SIGNAL(clicked()),
            this,   SLOT(doStream()));
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            this,      SLOT(socketError(QAbstractSocket::SocketError)));
    connect(tcpSocket, SIGNAL(connected()),
            this,      SLOT(socketConnected()));
    connect(tcpSocket, SIGNAL(disconnected()),
            this,      SLOT(socketDisconnected()));
    connect(tcpSocket, SIGNAL(readyRead()),
            this,      SLOT(readData()));
    player->setVideoOutput(videoWidget);

    setCentralWidget(centralWidget);
    player->play();
}