QT5实现视频播放器

前言

涵盖了视频播放器的创建和基本功能

视频播放器实现示例

#include <QtWidgets>
#include <QtMultimediaWidgets>
class VideoPlayer : public QWidget {
    Q_OBJECT
public:
    VideoPlayer(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        QVBoxLayout *layout = new QVBoxLayout(this);
        videoWidget = new QVideoWidget(this);
        layout->addWidget(videoWidget);
        mediaPlayer = new QMediaPlayer(this);
        mediaPlayer->setVideoOutput(videoWidget);
        controlLayout = new QHBoxLayout;
        playButton = new QPushButton("Play");
        pauseButton = new QPushButton("Pause");
        controlLayout->addWidget(playButton);
        controlLayout->addWidget(pauseButton);
        layout->addLayout(controlLayout);
        connect(playButton, &QPushButton::clicked, this, &VideoPlayer::play);
        connect(pauseButton, &QPushButton::clicked, this, &VideoPlayer::pause);
        // Replace with your video file path
        videoFilePath = "/path/to/your/video/file.mp4";
        mediaPlayer->setMedia(QUrl::fromLocalFile(videoFilePath));
    }

private slots:
    void play() {
        mediaPlayer->play();
    }

    void pause() {
        mediaPlayer->pause();
    }

private:
    QVideoWidget *videoWidget;
    QMediaPlayer *mediaPlayer;
    QPushButton *playButton;
    QPushButton *pauseButton;
    QHBoxLayout *controlLayout;
    QString videoFilePath;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    VideoPlayer player;
    player.show();

    return app.exec();
}
#include "main.moc"

在上述代码中,我们首先创建一个名为VideoPlayer的自定义QWidget类,其中包含了一个视频播放区域(QVideoWidget)和控制按钮(Play和Pause)。播放器使用QMediaPlayer来管理视频播放,并将视频输出到QVideoWidget。

要测试此示例,您需要替换videoFilePath变量中的路径为您自己的视频文件路径。然后,您可以使用Qt Creator或命令行编译器来构建和运行该应用程序。

实现快进和历史播放记录功能

#include <QtWidgets>
#include <QtMultimediaWidgets>

class VideoPlayer : public QWidget {
    Q_OBJECT

public:
    VideoPlayer(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        QVBoxLayout *layout = new QVBoxLayout(this);
        videoWidget = new QVideoWidget(this);
        layout->addWidget(videoWidget);
        mediaPlayer = new QMediaPlayer(this);
        mediaPlayer->setVideoOutput(videoWidget);
        controlLayout = new QHBoxLayout;
        playButton = new QPushButton("Play");
        pauseButton = new QPushButton("Pause");
        forwardButton = new QPushButton("Forward");
        historyButton = new QPushButton("History");
        controlLayout->addWidget(playButton);
        controlLayout->addWidget(pauseButton);
        controlLayout->addWidget(forwardButton);
        controlLayout->addWidget(historyButton);
        layout->addLayout(controlLayout);
        connect(playButton, &QPushButton::clicked, this, &VideoPlayer::play);
        connect(pauseButton, &QPushButton::clicked, this, &VideoPlayer::pause);
        connect(forwardButton, &QPushButton::clicked, this, &VideoPlayer::forward);
        connect(historyButton, &QPushButton::clicked, this, &VideoPlayer::showHistory);
        // Replace with your video file path
        videoFilePath = "/path/to/your/video/file.mp4";
        mediaPlayer->setMedia(QUrl::fromLocalFile(videoFilePath));
    }

private slots:
    void play() {
        mediaPlayer->play();
    }

    void pause() {
        mediaPlayer->pause();
    }

    void forward() {
        qint64 currentPosition = mediaPlayer->position();
        qint64 newPosition = currentPosition + 10000; // Forward by 10 seconds
        mediaPlayer->setPosition(newPosition);
    }

    void showHistory() {
        QMediaPlaylist *playlist = mediaPlayer->playlist();
        if (!playlist) {
            playlist = new QMediaPlaylist(mediaPlayer);
            mediaPlayer->setPlaylist(playlist);
        }
        // Save the current position to the history
        playlist->addMedia(QUrl::fromLocalFile(videoFilePath));
        // Play the last item in the playlist (latest history entry)
        mediaPlayer->setPlaylist(playlist);
        mediaPlayer->play();
    }

private:
    QVideoWidget *videoWidget;
    QMediaPlayer *mediaPlayer;
    QPushButton *playButton;
    QPushButton *pauseButton;
    QPushButton *forwardButton;
    QPushButton *historyButton;
    QHBoxLayout *controlLayout;
    QString videoFilePath;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    VideoPlayer player;
    player.show();

    return app.exec();
}
#include "main.moc"

历史播放记录

#include <QtWidgets>
#include <QtMultimediaWidgets>

class VideoPlayer : public QWidget {
    Q_OBJECT

public:
    VideoPlayer(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        QVBoxLayout *layout = new QVBoxLayout(this);
        videoWidget = new QVideoWidget(this);
        layout->addWidget(videoWidget);
        mediaPlayer = new QMediaPlayer(this);
        mediaPlayer->setVideoOutput(videoWidget);
        controlLayout = new QHBoxLayout;
        playButton = new QPushButton("Play");
        pauseButton = new QPushButton("Pause");
        forwardButton = new QPushButton("Forward");
        historyButton = new QPushButton("History");
        controlLayout->addWidget(playButton);
        controlLayout->addWidget(pauseButton);
        controlLayout->addWidget(forwardButton);
        controlLayout->addWidget(historyButton);
        layout->addLayout(controlLayout);
        connect(playButton, &QPushButton::clicked, this, &VideoPlayer::play);
        connect(pauseButton, &QPushButton::clicked, this, &VideoPlayer::pause);
        connect(forwardButton, &QPushButton::clicked, this, &VideoPlayer::forward);
        connect(historyButton, &QPushButton::clicked, this, &VideoPlayer::showHistory);
        // Create a media playlist for history
        historyPlaylist = new QMediaPlaylist(mediaPlayer);
        mediaPlayer->setPlaylist(historyPlaylist);
        // Replace with your video file path
        videoFilePath = "/path/to/your/video/file.mp4";
        mediaPlayer->setMedia(QUrl::fromLocalFile(videoFilePath));
    }

private slots:
    void play() {
        mediaPlayer->play();
    }

    void pause() {
        mediaPlayer->pause();
    }

    void forward() {
        qint64 currentPosition = mediaPlayer->position();
        qint64 newPosition = currentPosition + 10000; // Forward by 10 seconds
        mediaPlayer->setPosition(newPosition);
    }

    void showHistory() {
        // Play the last item in the history playlist
        if (historyPlaylist->mediaCount() > 0) {
            historyPlaylist->setCurrentIndex(historyPlaylist->mediaCount() - 1);
            mediaPlayer->play();
        }
    }

private:
    QVideoWidget *videoWidget;
    QMediaPlayer *mediaPlayer;
    QMediaPlaylist *historyPlaylist;
    QPushButton *playButton;
    QPushButton *pauseButton;
    QPushButton *forwardButton;
    QPushButton *historyButton;
    QHBoxLayout *controlLayout;
    QString videoFilePath;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    VideoPlayer player;
    player.show();

    return app.exec();
}

#include "main.moc"

实现降噪处理

#include <QtWidgets>
#include <QtMultimedia>

class NoiseReducer : public QWidget {
    Q_OBJECT

public:
    NoiseReducer(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        QVBoxLayout *layout = new QVBoxLayout(this);

        startButton = new QPushButton("Start Recording");
        stopButton = new QPushButton("Stop Recording");
        toggleNoiseReductionButton = new QPushButton("Toggle Noise Reduction");

        layout->addWidget(startButton);
        layout->addWidget(stopButton);
        layout->addWidget(toggleNoiseReductionButton);

        connect(startButton, &QPushButton::clicked, this, &NoiseReducer::startRecording);
        connect(stopButton, &QPushButton::clicked, this, &NoiseReducer::stopRecording);
        connect(toggleNoiseReductionButton, &QPushButton::clicked, this, &NoiseReducer::toggleNoiseReduction);

        audioInput = nullptr;
        audioOutput = nullptr;
        noiseReductionEnabled = false;
    }

private slots:
    void startRecording() {
        QAudioFormat format;
        format.setSampleRate(44100);
        format.setChannelCount(1);
        format.setSampleSize(16);
        format.setCodec("audio/pcm");
        format.setByteOrder(QAudioFormat::LittleEndian);
        format.setSampleType(QAudioFormat::SignedInt);

        QAudioDeviceInfo inputDeviceInfo = QAudioDeviceInfo::defaultInputDevice();
        if (!inputDeviceInfo.isFormatSupported(format)) {
            qWarning() << "Default format not supported, trying to use nearest";
            format = inputDeviceInfo.nearestFormat(format);
        }

        audioInput = new QAudioInput(inputDeviceInfo, format, this);

        noiseReductionBuffer.clear();
        audioInputIODevice = audioInput->start();
        connect(audioInputIODevice, &QIODevice::readyRead, this, &NoiseReducer::processAudioData);
    }

    void stopRecording() {
        if (audioInput) {
            audioInput->stop();
            delete audioInput;
            audioInput = nullptr;
        }
    }

    void toggleNoiseReduction() {
        noiseReductionEnabled = !noiseReductionEnabled;
    }

    void processAudioData() {
        QByteArray audioData = audioInputIODevice->readAll();

        if (noiseReductionEnabled) {
            // Apply a simple moving average filter to simulate noise reduction
            noiseReductionBuffer.append(audioData);
            if (noiseReductionBuffer.size() > 44100) {  // Adjust buffer size based on your needs
                noiseReductionBuffer.remove(0, noiseReductionBuffer.size() - 44100);
            }

            // Calculate the average value and replace the audio data with the average
            qint16 average = 0;
            for (char value : noiseReductionBuffer) {
                average += static_cast<qint8>(value);
            }
            average /= noiseReductionBuffer.size();

            audioData.fill(static_cast<char>(average), audioData.size());
        }

        // Play the processed audio
        if (!audioOutput) {
            audioOutput = new QAudioOutput(QAudioDeviceInfo::defaultOutputDevice(), audioInput->format(), this);
            audioOutputIODevice = audioOutput->start();
        }
        audioOutputIODevice->write(audioData);
    }

private:
    QPushButton *startButton;
    QPushButton *stopButton;
    QPushButton *toggleNoiseReductionButton;
    QAudioInput *audioInput;
    QIODevice *audioInputIODevice;
    QAudioOutput *audioOutput;
    QIODevice *audioOutputIODevice;
    QByteArray noiseReductionBuffer;
    bool noiseReductionEnabled;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    NoiseReducer noiseReducer;
    noiseReducer.show();

    return app.exec();
}

#include "main.moc"
QT5实现视频播放器

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

滚动到顶部