新手,想问一下怎么去找缺少的外部库,看了很多帖子,我估计是这个方面的问题,但是不会解决。求大佬看一哈
.pro文件如下
#-------------------------------------------------
#
# Project created by QtCreator 2019-05-21T15:19:35
#
#-------------------------------------------------
QT += core gui
QT += multimedia multimediawidgets //添加的
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testVideo
TEMPLATE = app
OBJECTS_DIR = tmp //添加的,因为在自定义类的.h文件中加入了Q_OBJECT宏
MOC_DIR = tmp //添加的
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
CONFIG += c++11
SOURCES += \
main.cpp \
widget.cpp \
playerwidget.cpp \
mylistwidgetitem.cpp
HEADERS += \
widget.h \
playerwidget.h \
mylistwidgetitem.h
FORMS +=
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Playerwidget.h
#ifndef PLAYERWIDGET_H
#define PLAYERWIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int Pvolume = 10;
class PlayerWidget: public QWidget
{
Q_OBJECT
public:
PlayerWidget();
public slots:
void doubleClicked_Item(QModelIndex index);//双击listwidget中的Item 实现播放,并且playing状态为true
void timesld_clicked();//按下时间进度条任意一个点的瞬间
void timesld_move();//按下进度条并移动到任意一个点,计时器被调节到相应的时间点,调节过程中计时器停止计时
void timesld_release();//释放进度条瞬间,开始计时
void timesld_renew();//播放过程中,每隔0.5秒刷新进度条的位置 //ontimerout()
// void getIndex();
//**************************************************
//播放暂停键的槽函数
void playandstop();
void mediaStateChanged(QMediaPlayer::State);
//下一首、上一首的槽函数
void next();
void last();
//静音的按钮槽函数,调节音量的槽函数
void toSlient();
void volumesld_changed();
private:
int timesld_max = 1000; //时间进度条最大值
QHBoxLayout *operateHlayout;
QPushButton *PlayAndStopbtn;
QPushButton *Nextbtn;
QPushButton *Lastbtn;
QPushButton *Volumebtn;
QSlider *volumesld;
QSlider *timesld;
QVideoWidget *videoWidget;
QMediaPlayer *mediaPlayer;
QVBoxLayout *mainVlayout = new QVBoxLayout;
QPushButton *open_file_btn;
QListWidget *listWidget;
QListWidgetItem *listWidgetItem;
QVBoxLayout * menuVlayout;
QHBoxLayout *finalHlayout;
QMediaPlaylist *playlist;
QTimer *timer;
};
#endif // PLAYERWIDGET_H
Playerwidget.cpp
#include "playerwidget.h"
#include "myslider.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
PlayerWidget ::PlayerWidget()
{
setWindowFlags(Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint);
QHBoxLayout *operateHlayout;
operateHlayout = new QHBoxLayout;
QPushButton *PlayAndStopbtn;
PlayAndStopbtn = new QPushButton(this);
PlayAndStopbtn->setText("暂停");
QPushButton *Nextbtn;
Nextbtn = new QPushButton(this);
Nextbtn->setText("上一首");
QPushButton *Lastbtn;
Lastbtn = new QPushButton(this);
Lastbtn->setText("下一首");
QPushButton *Volumebtn;
Volumebtn = new QPushButton(this);
Volumebtn->setText("音量");
QSlider *volumesld;
volumesld = new QSlider(this);
volumesld->setOrientation(Qt::Horizontal);
operateHlayout->addStretch(10);//设置弹簧
operateHlayout->addWidget(Nextbtn);
operateHlayout->addWidget(PlayAndStopbtn);
operateHlayout->addWidget(Lastbtn);
operateHlayout->addStretch(30);
// operateHlayout->setContentsMargins(80,10,10,10); ???
operateHlayout->addWidget(Volumebtn);
operateHlayout->addWidget(volumesld);
operateHlayout->setSpacing(20);//设置控件之间的间距
operateHlayout->setMargin(10);//设置外间距
operateHlayout->addStretch(10);
QSlider *timesld;
timesld = new QSlider(this);
timesld->setOrientation(Qt::Horizontal);
QVideoWidget *videoWidget;
videoWidget = new QVideoWidget;
QMediaPlayer *mediaPlayer;
mediaPlayer = new QMediaPlayer(this);
mediaPlayer->setVideoOutput(videoWidget);
videoWidget->show();
QVBoxLayout *mainVlayout = new QVBoxLayout;
mainVlayout -> addWidget(videoWidget);
mainVlayout -> addWidget(timesld);
mainVlayout -> addLayout(operateHlayout);
QPushButton *open_file_btn;
open_file_btn = new QPushButton;
open_file_btn->setText("添加文件");
QListWidget *listWidget;
listWidget = new QListWidget(this);
QListWidgetItem *listWidgetItem;
listWidgetItem = new QListWidgetItem;
QVBoxLayout * menuVlayout;
menuVlayout = new QVBoxLayout;
menuVlayout->addWidget(open_file_btn);
menuVlayout->addWidget(listWidget);
QHBoxLayout *finalHlayout;
finalHlayout = new QHBoxLayout;
finalHlayout->addLayout(mainVlayout);
finalHlayout->addLayout(menuVlayout);
this->setLayout(finalHlayout);
QMediaPlaylist *playlist;
playlist = new QMediaPlaylist(this);
// connect(playlist,SIGNAL(currentIndexChanged(int)),this,SLOT(mediaChange(int)));
mediaPlayer->setPlaylist(playlist);
// 测试
// mediaPlayer->setMedia(QUrl::fromLocalFile("D:/音乐/ce.mp4"));
// mediaPlayer->play();
//menu部分的信号与槽(打开文件按钮和点击listWidgetItem实现播放)
connect(open_file_btn,&QPushButton::clicked,this,[=](){
QFileInfo fileInfo;
QString filePath= QFileDialog::getOpenFileName(this,"chooseFile","D:\\","(*.mp3 *.mp4 *.wma *.3gp *.wav *.avi *.flv *.rmvb *.mkv *.ts *.wmv)");
fileInfo = QFileInfo(filePath);
listWidget->addItem(fileInfo.fileName());
// listWidgetItem->setIndex();
playlist->addMedia(QUrl::fromLocalFile(filePath));
mediaPlayer->setMedia(QUrl::fromLocalFile(filePath));
mediaPlayer->play();
timesld->setRange(0,timesld_max);
timer = new QTimer();
timer->setInterval(500); //如果想看起来流畅些,可以把时间间隔调小,如100ms
timer->start();
//将timer连接至timesld_renew()槽函数
connect(timer, &QTimer::timeout, this, &PlayerWidget::timesld_renew);
});
connect(listWidget,SIGNAL(doubleClicked(QModleIndex)),this,SLOT(doubleClicked_Item(QModleIndex)));
//自定义一个槽函数,实现双击listWidgetItem,播放视频
//operat部分的信号与槽(音量:一键静音按钮和音量滑动条)
connect(Volumebtn,&QPushButton::clicked,this,&PlayerWidget::toSlient);
connect(volumesld,SIGNAL(MySlider::MySliderClicked()),this,SLOT(PlayerWidget::volumesld_changed()));
connect(volumesld,&QSlider::sliderMoved,this,&PlayerWidget::volumesld_changed);
//operat部分的信号与槽(上一曲,下一曲)
connect(Nextbtn,&QPushButton::clicked,this,&PlayerWidget::next);
connect(Lastbtn,&QPushButton::clicked,this,&PlayerWidget::last);
//operat部分的信号与槽(播放暂停)
connect(PlayAndStopbtn,&QPushButton::clicked,this,&PlayerWidget::playandstop);
connect(mediaPlayer,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(mediaStateChanged(QMediaPlayer::State)));
//timeslider部分的信号与槽
connect(timesld,SIGNAL(MySlider::MySliderClicked()),this,SLOT(PlayerWidget::timesld_clicked()));
connect(timesld,&QSlider::sliderMoved,this,&PlayerWidget::timesld_move);
connect(timesld,&QSlider::sliderReleased,this,&PlayerWidget::timesld_release);
}
//menu部分槽函数:双击listwidget中的Item 实现播放,并且playing状态为true
void PlayerWidget::doubleClicked_Item(QModelIndex index){
int n=index.row();
playlist->setCurrentIndex(n);
mediaPlayer->play();
}
//播放暂停键的槽函数
void PlayerWidget::playandstop(){
switch (mediaPlayer->state()) {
case QMediaPlayer::PlayingState:
mediaPlayer->pause();
break;
default:
mediaPlayer->play();
break;
}
}
void PlayerWidget::mediaStateChanged(QMediaPlayer::State state){
switch (state) {
case QMediaPlayer::PlayingState:
PlayAndStopbtn->setText("暂停");
break;
default:
PlayAndStopbtn->setText("播放");
break;
}
}
void PlayerWidget::next()
{
int currentIndex = playlist->currentIndex();
if((currentIndex + 1) == playlist->mediaCount()){
currentIndex = 0;
playlist->setCurrentIndex(0);
mediaPlayer ->play();
}
else{
playlist->setCurrentIndex(++currentIndex);
mediaPlayer ->play();
}
}
void PlayerWidget::last(){
int currentIndex = playlist->currentIndex();
if(currentIndex == 0){
currentIndex = playlist->mediaCount() - 1;
playlist->setCurrentIndex(currentIndex);
mediaPlayer ->play();
}
else {
playlist->setCurrentIndex(--currentIndex);
mediaPlayer ->play();
}
}
void PlayerWidget::toSlient(){
if(mediaPlayer->isMuted()){
Pvolume = mediaPlayer->volume();
mediaPlayer->setMuted(true);
}
else{
mediaPlayer ->setVolume(Pvolume);
}
}
void PlayerWidget::volumesld_changed(){
mediaPlayer->setVolume(volumesld->value());
}
//按下时间进度条任意一个点的瞬间
void PlayerWidget::timesld_clicked(){
mediaPlayer->setPosition(timesld->value()*mediaPlayer->duration()/timesld_max);//进度条现在的值比上进度条最大值乘播放已持续时间
}
//按下进度条并移动到任意一个点,计时器被调节到相应的时间点,调节过程中计时器停止计时
void PlayerWidget::timesld_move(){
timer->stop();
mediaPlayer->setPosition(timesld->value()*mediaPlayer->duration()/timesld_max);
}
//释放进度条瞬间,开始计时
void PlayerWidget::timesld_release(){
timer->start();
}
//播放过程中,每隔0.5秒刷新进度条的位置 //ontimerout()
void PlayerWidget::timesld_renew(){
timesld->setValue(mediaPlayer->position()*timesld_max/mediaPlayer->duration());
}
MySlider.h
#ifndef MYSLIDER_H
#define MYSLIDER_H
#include
#include
#include
class MySlider : public QSlider
{
Q_OBJECT
public:
MySlider();
void mousePressEvent(QMouseEvent *ev);//重写QSlider的mousePressEvent事件
public: signals:
void MySliderClicked();//自定义的鼠标单击信号,用于捕获并处理
};
#endif // MYSLIDER_H
MySlider.cpp
#include "myslider.h"
MySlider::MySlider()
{
}
//重写鼠标事件
void MySlider::mousePressEvent(QMouseEvent *ev){
QSlider::mousePressEvent(ev);
double position = ev->pos().x()/(double)width();
setValue(position * (maximum() - minimum()) + minimum());
emit MySliderClicked();
}
main.cpp
#include "widget.h"
#include "playerwidget.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PlayerWidget w;
w.show();
return a.exec();
}