自制一个播放器!

发布于 2021-09-01  502 次阅读


热爱,探索,追逐

写在前面

最近晚上回来老是喜欢捣鼓一些小东西。
然后最近一直也有复习C语言。
觉着干学也没什么成就感,,
然后再网上搜了搜。
就发现这个令我着迷的软件。
这软件不就是以前我一直梦寐以求的那种格式的软件吗哈哈哈哈。

软件介绍

file

它是基于C++开发,且有着图形化开发支持c/c++开发的一款软件。
可以用它开发app,电脑软件等等等等(因为目前就用到这么多)
界面:
最强的莫过于你添加的一些事件(在可视化界面添加的按钮)会自动生成函数,然后需要编写的就是这些函数。

file

构思

网上搜了搜它的项目。
哇,原来都是用它来做嵌入式设备的软件的。
芜湖
以前也有过很多想法。
首先做一个简单的播放器吧

实践!

  • 首先一定要有查找电脑里的文件功能,当然要可以播放音乐和视频啦
  • 其次可以暂停
  • 可以关闭
  • 再想一想其他的。。
想法总是丰富的,但是实践会出现很多问题
比如。音量调节,,,
比如拖动视频窗口大小。。。
视频大小没有跟着变化

成果展示!

还好都一路坚持下来了><!
基本功能80%吧不说了上图
嘿!

代码结构

file

外观

file

音乐/视频播放(可拖动窗口)

file

暂停 停止功能均实现

未实现(音量调节)。。

代码

QT       += core gui multimedia multimedia multimediawidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QDebug>
#include<QFileDialog>
#include<QMediaPlayer>      //播放器
#include<QMediaPlaylist>    //播放列表
#include<QVideoWidget>      //视频显示窗口
#include<QMainWindow>
#include<QPaintEvent>       //绘图事件
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:

    void on_OpenFile_clicked();

    void on_PlayFile_clicked();

    void on_StopFile_clicked();

    void on_Close_file_clicked();

protected:
    void paintEvent(QPaintEvent* e);

private:
    Ui::MainWindow *ui;

    QMediaPlayer* pPlayer;
    QMediaPlaylist* pPlayerList;
    QVideoWidget* pVideoWidget;

};
#endif // MAINWINDOW_H
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //标题
    setWindowTitle("你若盛开,轻风自来");
    pPlayer = new QMediaPlayer;         //电影放映员
    pPlayerList = new QMediaPlaylist;   //电影胶带
    pVideoWidget = new QVideoWidget(ui->label); //电影屏幕

    pVideoWidget->resize(ui->label->size());

    //设置播放器的播放队列
    pPlayer->setPlaylist(pPlayerList);
    //设置播放器的显示窗口
    pPlayer->setVideoOutput(pVideoWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//当‘打开按钮’被点击时 执行这个函数。
void MainWindow::on_OpenFile_clicked()
{
    //qDebug() <<"guoan郭安阿~~~" ;
    QStringList fileNames= QFileDialog::getOpenFileNames(this,"过往的青春是一场回不去的梦","D:/360Downloads/cscc","allfiles(*.*);;""MP3(*.mp3)");
    pPlayerList->clear();   //先清空播放队列
    foreach(QString const & str,fileNames){//在QStringList对象中循环
        //qDebug() << str ;
     //把选中的音频文件添加到播放列表
       QUrl url(str);

       pPlayerList->addMedia(url);
    }
}
//播放函数
void MainWindow::on_PlayFile_clicked()
{
      pPlayer->play();

      pVideoWidget->resize(ui->label->size());
}

void MainWindow::paintEvent(QPaintEvent* e)
{
    pVideoWidget->resize(ui->label->size());
}

void MainWindow::on_StopFile_clicked()
{
    pPlayer->pause();
}

void MainWindow::on_Close_file_clicked()
{
    pPlayer->stop();
}

ui界面设计

file


擦肩而过的概率