媒体播放器无法用于多个视频(QT)

时间:2018-06-28 06:18:51

标签: qt qt5

我当前拥有的代码不适用于多个媒体播放器。它只能用于一个视频,而不能用于多个视频。当我运行代码时,它运行并且我只能听到声音,而不能听到视频表面。我认为我每次都在写类成员“播放器”。我不知道如何使“玩家”成为QVector。请向我展示一下方式。在下面查看我的代码。

这是MainWinsow.h

namespace Ui {
class MainWindow;
}
QT_BEGIN_NAMESPACE
class QAbstractButton;
class QSlider;
class QGraphicsVideoItem;
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    bool isPlayerAvailable() const;
    QSize sizeHint() const override;

public slots:

    void play();

private slots:
    void mediaStateChanged(QMediaPlayer::State state);
    void positionChanged(qint64 position);
    void durationChanged(qint64 duration);
    void setPosition(int position);


private:
    Ui::MainWindow *ui;
    QMediaPlayer *player = nullptr;
    QMediaPlaylist *playlist = nullptr;
    QGraphicsVideoItem *videoItem = nullptr;
    QAbstractButton *m_playButton = nullptr;
    QSlider *m_positionSlider = nullptr;
};

这是MainWindow.cpp

 ui->setupUi(this);

ui->listWidget->setFlow(QListView::LeftToRight);
ui->listWidget->setMinimumSize(860,380);
ui->listWidget->setGridSize(QSize(420, 380));
ui->listWidget->setResizeMode(QListView::Adjust);
ui->listWidget->setViewMode(QListView::ListMode);
ui->listWidget->setWrapping(true);

QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",

                                                   QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);

directory.setNameFilters({"*.mp4", "*.avi"});

for(const QFileInfo & finfo: directory.entryInfoList()){

    player = new QMediaPlayer;
    player->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
    videoItem = new QGraphicsVideoItem;
    QGraphicsScene *scene = new QGraphicsScene(this);
    QGraphicsView *graphicsView = new QGraphicsView(scene);
    scene->addItem(videoItem);
    player->setVideoOutput(videoItem);

    m_playButton = new QPushButton;
    m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
     connect(m_playButton, &QAbstractButton::clicked, this, &MainWindow::play);

    m_positionSlider = new QSlider(Qt::Horizontal);
    m_positionSlider->setRange(0, 0);
    connect(m_positionSlider, &QAbstractSlider::sliderMoved,this, &MainWindow::setPosition);


    QBoxLayout *controlLayout = new QHBoxLayout;
    controlLayout->setMargin(0);
    controlLayout->addWidget(m_playButton);
    controlLayout->addWidget(m_positionSlider);

    QBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(graphicsView);
    layout->addLayout(controlLayout);

    connect(player, &QMediaPlayer::stateChanged, this, &MainWindow::mediaStateChanged);
    connect(player, &QMediaPlayer::positionChanged, this, &MainWindow::positionChanged);
    connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::durationChanged);

    auto    item = new QListWidgetItem("", ui->listWidget);
    auto    widget = new QWidget;
    auto    label = new QLabel(finfo.fileName());
    auto    vb = new QVBoxLayout;

    vb->addWidget(label,1);
    vb->addWidget(graphicsView, 9);
    vb->setMargin(0);
    vb->addWidget(m_playButton);
    vb->addWidget(m_positionSlider);
    widget->setLayout(vb);
    widget->setMinimumSize(400, 360);
    ui->listWidget->setItemWidget(item,widget);


}

0 个答案:

没有答案