如何在我的声子媒体播放器中添加矩形?

发布于 2024-11-26 16:58:10 字数 2307 浏览 0 评论 0原文

现在这是一个基于声子(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));
}

但仍然无法工作...... 有人有什么好主意吗?

Now This is a simple media player which is based on phonon(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();
}

And now I want to add some sign in the current widget when it is playing video. How can I implement this?
For example, if I want to add a rectangle in the specified position over the current frame. How could I do that?

Now I have another try to do this:
I define a class named MyVideoWidget which is inhred from Phonon::VideoWidget. Just like this:

class MyVideoWidget : public Phonon::VideoWidget

And then I overload the function paintEvent like this:

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));
}

But is still cannot work...
Does anyone has some good idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单气质女生网名 2024-12-03 16:58:11

在我的选择中,我根本不会触及声子的实现。我会使用 QStackedLayout 创建一个覆盖层,而不是尝试重新实现 Phonon::VideoWidget。这是修改后的示例

//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();

}

In my option I would not touch Phonon implementation at all. Rather then trying to re-implement the Phonon::VideoWidget I would create an overlay using QStackedLayout. here is your example modified

//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();

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文