如何在声机媒体播放器中添加矩形?



这是一个简单的基于声子(QT)的媒体播放器。

#include <QApplication>
#include <QWidget>
#include <phonon>
#include <QUrl>
#include <QObject>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QWidget *widget = new QWidget;
    widget->setWindowTitle("Media Player");
    widget->resize(400,400);
    Phonon::MediaObject *media = new Phonon::MediaObject;
    media->setCurrentSource(Phonon::MediaSource("test.mpg"));
    Phonon::VideoWidget *vwidget = new Phonon::VideoWidget(widget);
    Phonon::createPath(media, vwidget);
    vwidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);
    Phonon::AudioOutput *aOutput = new Phonon::AudioOutput(Phonon::VideoCategory);
    Phonon::createPath(media, aOutput);
    QLabel *label = new QLabel("Volume: ");
    Phonon::VolumeSlider *volumeSlider = new Phonon::VolumeSlider;
    volumeSlider->setAudioOutput(aOutput);
    volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
    Phonon::SeekSlider *seekSlider = new Phonon::SeekSlider;
    seekSlider->setMediaObject(media);
    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(label);
    hLayout->addWidget(volumeSlider);
    hLayout->addStretch();
    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addWidget(vwidget);
    vLayout->addWidget(seekSlider);
    vLayout->addLayout(hLayout);
    widget->setLayout(vLayout);
    widget->show();
    media->play();
    return app.exec();
}

现在我想在当前小部件播放视频时添加一些符号。我该如何实现呢?例如,如果我想在当前帧的指定位置添加一个矩形。我怎么能这么做呢?

现在我有另一个尝试这样做:我定义了一个名为MyVideoWidget的类,它是从Phonon::VideoWidget继承的。就像这样:

class MyVideoWidget : public Phonon::VideoWidget

然后我像这样重载函数paintEvent

void
MyVideoWidget::paintEvent (QPaintEvent * event)
{
Phonon::VideoWidget::paintEvent (event);
QPainter painter (this);
QPen pen;
pen.setJoinStyle(Qt::MiterJoin);
pen.setWidth(5);
pen.setColor(QColor::fromRgb(255,255, 255));
painter.setPen(pen);
painter.drawLine (QPoint (20, 20), QPoint (100, 20));
painter.drawLine (QPoint (20, 100), QPoint (100, 100));
}

但它仍然不能工作…有人有什么好主意吗?

在我的选项中,我根本不会触摸声子实现。而不是试图重新实现Phonon::VideoWidget我将创建一个覆盖使用QStackedLayout。下面是修改后的示例

//Qt
#include <QApplication>
#include <QWidget>
#include <QUrl>
#include <QObject>
#include <QStackedLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
//Phonon
#include <Phonon/AudioOutput>
#include <Phonon/MediaObject>
#include <Phonon/MediaSource>
#include <Phonon/VideoWidget>
#include <Phonon/VolumeSlider>
#include <Phonon/SeekSlider>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget *widget = new QWidget;
widget->setWindowTitle("Media Player");
widget->resize(400,400);
Phonon::MediaObject *media = new Phonon::MediaObject;
media->setCurrentSource(Phonon::MediaSource("test.avi"));
Phonon::VideoWidget *vwidget = new Phonon::VideoWidget(widget);
Phonon::createPath(media, vwidget);
vwidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);
Phonon::AudioOutput *aOutput = new Phonon::AudioOutput(Phonon::VideoCategory);
Phonon::createPath(media, aOutput);
QLabel *label = new QLabel("Volume: ");
Phonon::VolumeSlider *volumeSlider = new Phonon::VolumeSlider;
volumeSlider->setAudioOutput(aOutput);
volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
Phonon::SeekSlider *seekSlider = new Phonon::SeekSlider;
seekSlider->setMediaObject(media);
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(label);
hLayout->addWidget(volumeSlider);
hLayout->addStretch();
QStackedLayout *videoStack = new QStackedLayout;
QWidget *myOverlay = new QWidget;
myOverlay->setFixedSize(100, 100);
QVBoxLayout *l_overlayLayout = new QVBoxLayout;
l_overlayLayout->addWidget(label);
myOverlay->setLayout(l_overlayLayout);
videoStack->addWidget(vwidget);
videoStack->addWidget(myOverlay);
videoStack->setStackingMode(QStackedLayout::StackAll);
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addLayout(videoStack);
vLayout->addWidget(seekSlider);
vLayout->addLayout(hLayout);
widget->setLayout(vLayout);
widget->show();
media->play();
return app.exec();

}

相关内容

  • 没有找到相关文章

最新更新