// Copyright: 2021, Laseranimation Sollinger, Berlin, all rights reserved #include #include #include class VideoServerWindow : public QMainWindow { public: using QMainWindow::QMainWindow; void paintEvent(QPaintEvent * /*e*/) { QPainter painter(this); int menuHeight = 0; int windowWidth = size().width(); int windowHeight = size().height() - menuHeight; if (windowWidth > 0 && windowHeight > 0) { painter.fillRect(0, menuHeight, windowWidth, windowHeight, QColor(0, 0, 0)); QString text = tr("No Video"); //text = tr("No Media"); QFont myFont = painter.font(); QFontMetrics fm(myFont); // Wir nehmen die doppelte Breite damit es links und rechts etwas Abstand gibt QRect textDimensions = fm.boundingRect(text); int textWidth = 2 * textDimensions.width(); int textHeight = textDimensions.height(); if (textWidth < 1) textWidth = 1; if (textHeight < 1) textHeight = 1; qreal aspectRatioWindow = (qreal) windowWidth / (qreal) windowHeight; qreal aspectratioText = (qreal) textWidth / (qreal) textHeight; if (aspectratioText > aspectRatioWindow) { myFont.setPixelSize((int)((windowHeight * aspectRatioWindow ) / aspectratioText)); } else { myFont.setPixelSize(windowHeight); } painter.setFont(myFont); painter.setPen(QColor(255, 255, 255)); painter.drawText(0, menuHeight, windowWidth, windowHeight, Qt::AlignCenter, text); } } }; QMediaPlayerController::QMediaPlayerController() { mVideoWindow = new VideoServerWindow(); mVideoWidget = new QVideoWidget(mVideoWindow); mVideoWindow->setCentralWidget(mVideoWidget); mAudioOnlyWidget = new QLabel("Audio Only", mVideoWindow); // This MUST happen before any of the DSP infrastructure // is brought up. QtMediaInterface::setMediaPlayerControllerInstance(this); mMediaPlayer.setAudioOutput(&mAudioOutput); mMediaPlayer.setVideoOutput(mVideoWidget); mAudioOutput.setVolume(100); QObject::connect( &mMediaPlayer, &QMediaPlayer::playbackStateChanged, [this](QMediaPlayer::PlaybackState state) { if(state == QMediaPlayer::PlayingState) { if(mMediaPlayer.hasVideo()) { mVideoWidget->show(); const auto metaData = mMediaPlayer.metaData(); const auto resolution = metaData.value(QMediaMetaData::Resolution); if(resolution.canConvert()) { const auto resolutionSize = resolution.toSize(); qDebug() << "resolutionSize" << resolutionSize; mVideoWindow->resize(resolutionSize); } } else { mVideoWidget->hide(); mMediaPlayer.setVideoOutput(nullptr); mVideoWindow->setCentralWidget(mAudioOnlyWidget); } mVideoWindow->show(); } }); } QMediaPlayer* QMediaPlayerController::player() { return &mMediaPlayer; } void QMediaPlayerController::prepareVideoPlayer() { qDebug() << "QMediaPlayerController::prepareVideoPlayer, needs new video widget:"; }