#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
,colorDialog(0)
,coverLabel(0)
,playerState(QMediaPlayer::StoppedState)
,playerMuted(false)
{
setWindowTitle("Media Player");
listView_playlist=new QListView(this);
horizontalSlider_pos= new QSlider(Qt::Horizontal, this);
label_duration_pos=new QLabel(this);
pushButton_open=new QPushButton(tr("Open"),this);
toolButton_stop= new QToolButton(this);
toolButton_pre= new QToolButton(this);
toolButton_play= new QToolButton(this);
toolButton_next= new QToolButton(this);
toolButton_mute= new QToolButton(this);
horizontalSlider_vol= new QSlider(Qt::Horizontal, this);
comboBox_rate=new QComboBox(this);
pushButton_fullscreen=new QPushButton(tr("FullScreen"),this);
pushButton_coloroptions=new QPushButton(tr("ColorOptions..."),this);
mediaPlayer = new QMediaPlayer(this);
connect(mediaPlayer, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
this, SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus)));//load/buffering/unknow
connect(mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(stateChanged(QMediaPlayer::State)));//play/pause/stop
connect(mediaPlayer, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));
//likeyong20170927,mediaPlayer is inherit from MainWindow,even mediaPlayer is the recerver, SLOT()(inherit form MainWindow) can also be invoked.
connect(mediaPlayer, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
connect(mediaPlayer, SIGNAL(videoAvailableChanged(bool)), SLOT(videoAvailableChanged(bool)));//currently in play/pause state
connect(mediaPlayer, SIGNAL(metaDataChanged()), SLOT(metaDataChanged()));
connect(mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(displayErrorMessage()));
connect(mediaPlayer, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int)));
mediaPlaylist = new QMediaPlaylist();
connect(mediaPlaylist, SIGNAL(currentIndexChanged(int)), SLOT(playlistPositionChanged(int)));
mediaPlayer->setPlaylist(mediaPlaylist);
connect(listView_playlist,SIGNAL(clicked(QModelIndex)),this,SLOT(jump(QModelIndex)));
horizontalSlider_pos->setRange(0, mediaPlayer->duration() / 1000);
connect(horizontalSlider_pos, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));
connect(pushButton_open, SIGNAL(clicked()), this, SLOT(open()));
toolButton_stop->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
toolButton_stop->setEnabled(false);
connect(toolButton_stop, SIGNAL(clicked()), this, SLOT(stop()));
toolButton_pre->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
connect(toolButton_pre, SIGNAL(clicked()), this, SLOT(previous()));
toolButton_play->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(toolButton_play, SIGNAL(clicked()), this, SLOT(play()));
toolButton_next->setIcon(style()->standardIcon(QStyle::SP_MediaSkipForward));
connect(toolButton_next, SIGNAL(clicked()), this, SLOT(next()));
toolButton_mute->setIcon(style()->standardIcon(QStyle::SP_MediaVolume));
connect(toolButton_mute, SIGNAL(clicked()), this, SLOT(changeMute()));
horizontalSlider_vol->setRange(0, 100);
connect(horizontalSlider_vol, SIGNAL(sliderMoved(int)), this, SLOT(changeVolume(int)));
comboBox_rate->addItem("0.5x", QVariant(0.5));
comboBox_rate->addItem("1.0x", QVariant(1.0));
comboBox_rate->addItem("2.0x", QVariant(2.0));
comboBox_rate->setCurrentIndex(1);
connect(comboBox_rate, SIGNAL(activated(int)), this, SLOT(changeRate(int)));
pushButton_fullscreen->setCheckable(true);
connect(pushButton_coloroptions, SIGNAL(clicked()), this, SLOT(showColorDialog()));
//mediaPlayer->setMedia( QUrl::fromLocalFile("C:/Users/Public/Videos/Sample Videos/Wildlife.wmv") );
//mediaPlayer->setMedia( QUrl::fromLocalFile("C:\\Users\\Public\\Videos\\Sample Videos\\Wildlife.wmv") );
//mediaPlayer->setMedia(QDir("C:/Users/Public/Videos/Sample Videos/Wildlife.wmv"));
videoWidget=new MyVideoWidget(this);
videoWidget->show();
mediaPlayer->setVideoOutput(videoWidget);
//mediaPlayer->play();
QBoxLayout *HLayout = new QHBoxLayout;
HLayout->addWidget(videoWidget, 2);
HLayout->addWidget(listView_playlist);
QBoxLayout *HLayout2 = new QHBoxLayout;
HLayout2->addWidget(horizontalSlider_pos, 2);
HLayout2->addWidget(label_duration_pos);
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(pushButton_open);
controlLayout->addStretch(1);
controlLayout->addWidget(toolButton_stop);
controlLayout->addStretch(1);
controlLayout->addWidget(toolButton_pre);
controlLayout->addStretch(1);
controlLayout->addWidget(toolButton_play);
controlLayout->addStretch(1);
controlLayout->addWidget(toolButton_next);
controlLayout->addStretch(1);
controlLayout->addWidget(toolButton_mute);
controlLayout->addStretch(1);
controlLayout->addWidget(horizontalSlider_vol);
controlLayout->addStretch(1);
controlLayout->addWidget(comboBox_rate);
controlLayout->addStretch(1);
controlLayout->addWidget(pushButton_fullscreen);
controlLayout->addStretch(1);
controlLayout->addWidget(pushButton_coloroptions);
QBoxLayout *TopLayout = new QVBoxLayout;
TopLayout->addLayout(HLayout);
TopLayout->addLayout(HLayout2);
TopLayout->addLayout(controlLayout);
setLayout(TopLayout);
if (!isMediaPlayerAvailable()) {
QMessageBox::warning(this, tr("Service not available"),
tr("The QMediaPlayer object does not have a valid service.\n"\
"Please check the media service plugins are installed."));
disableAllKey();
}
}
MainWindow::~MainWindow()
{
//delete ui;
}
void MainWindow::disableAllKey()
{
listView_playlist->setEnabled(false);
horizontalSlider_pos->setEnabled(false);
label_duration_pos->setEnabled(false);
pushButton_open->setEnabled(false);
toolButton_stop->setEnabled(false);
toolButton_pre->setEnabled(false);
toolButton_play->setEnabled(false);
toolButton_next->setEnabled(false);
toolButton_mute->setEnabled(false);
horizontalSlider_vol->setEnabled(false);
comboBox_rate->setEnabled(false);
pushButton_fullscreen->setEnabled(false);
pushButton_coloroptions->setEnabled(false);
}
bool MainWindow::isMediaPlayerAvailable() const
{
return mediaPlayer->isAvailable();
}
void MainWindow::open()
{
QFileDialog fileDialog(this);
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setWindowTitle(tr("Open Files"));
QStringList supportedMimeTypes = mediaPlayer->supportedMimeTypes();
if (!supportedMimeTypes.isEmpty()) {
supportedMimeTypes.append("audio/x-m3u"); // MP3 playlists
fileDialog.setMimeTypeFilters(supportedMimeTypes);
}
fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::MoviesLocation).value(0, QDir::homePath()));
if (fileDialog.exec() == QDialog::Accepted)
addToPlaylist(fileDialog.selectedUrls());
}
static bool isPlaylist(const QUrl &url) // Check for ".m3u" playlists.
{
if (!url.isLocalFile())
return false;
const QFileInfo fileInfo(url.toLocalFile());
return fileInfo.exists() && !fileInfo.suffix().compare(QLatin1String("m3u"), Qt::CaseInsensitive);
}
void MainWindow::addToPlaylist(const QList<QUrl> urls)
{
//mediaPlaylist->removeMedia(0);//add by likeyong
//mediaPlaylist->clear();//add by likeyong
bool isFindSameUrl = false;
foreach (const QUrl &url, urls) {
if (isPlaylist(url))
mediaPlaylist->load(url);
else{
if(0 == mediaPlaylist->mediaCount()){
mediaPlaylist->addMedia(url);
}else{
for(int i = 0; i < mediaPlaylist->mediaCount(); i++){
QUrl location = mediaPlaylist->media(i).canonicalUrl();
if(QFileInfo(url.path()).fileName() == QFileInfo(location
没有合适的资源?快使用搜索试试~ 我知道了~
QT编写的多媒体播放器源码--mediaplayer
共28个文件
cpp:8个
o:5个
h:5个
3星 · 超过75%的资源 需积分: 50 152 下载量 154 浏览量
2017-10-12
14:52:13
上传
评论 10
收藏 1.41MB ZIP 举报
温馨提示
用QT5开发的媒体播放器,界面美观,可以播放rmvb、mp4等视频格式文件,也可以播放mp3、wav等音频格式文件,代码具备一定参考价值!
资源推荐
资源详情
资源评论
收起资源包目录
media_ui.zip (28个子文件)
media_ui
build-mediaplayer-Desktop_Qt_5_6_2_MinGW_32bit-Debug
Makefile 22KB
release
Makefile.Debug 61KB
debug
moc_myvideowidget.o 255KB
myvideowidget.o 484KB
moc_myvideowidget.cpp 3KB
mainwindow.o 635KB
main.o 505KB
moc_mainwindow.cpp 9KB
mediaplayer.exe 2.2MB
moc_mainwindow.o 508KB
ui_mainwindow.h 6KB
Makefile.Release 61KB
mediaplayer
myvideowidget.cpp 3KB
mainwindow.cpp 18KB
myvideowidget.h 2KB
mainwindow.h 3KB
mediaplayer.pro.user 23KB
main.cpp 183B
mediaplayer.pro 525B
mainwindow.ui 5KB
mediaplayer.bak
myvideowidget.cpp 3KB
mainwindow.cpp 11KB
myvideowidget.h 2KB
mainwindow.h 1KB
mediaplayer.pro.user 23KB
main.cpp 183B
mediaplayer.pro 525B
mainwindow.ui 5KB
共 28 条
- 1
资源评论
- weixin_434372772018-12-11还不错,可以用
- 骡码人2018-03-2855555~~~很好,能学到很多
- qtc++开发者2019-03-14这不就是qt的官方例子吗
- 亚光速2019-01-17还不错的呀
hekafei1982
- 粉丝: 1
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功