/*! * @file videoplayer.cpp * Copyright (c) 2018 * @brief des * detailed des * * @date 2018 * @author lee */ #include "videoplayer.h" #include "ui_videoplayer.h" #include "frmmessagebox.h" //#include "mediahome.h" #include "audiosink.h" #include #include #include #include #include #include #include #include "iwidgetbridge.h" VideoPlayer *VideoPlayer::self = NULL; /************************************** Function: Description: ***************************************/ VideoPlayer *VideoPlayer::Instance(QWidget *parent) { if (self == NULL) { QMutex mutex; QMutexLocker locker(&mutex); if (self == NULL) { self = new VideoPlayer(parent); } } return self; } /************************************** Function: Description: ***************************************/ VideoPlayer::VideoPlayer(QWidget *parent) : IWidget(parent), ui(new Ui::VideoPlayer), ap(NULL), m_totalDuration(0), m_curDuration(0), m_bPlayState(STOPPED), m_bVideoListOpen(false), m_curVideoFilePath(""), m_curVideoFileName(""), m_curVideoFileCount(-1), m_bSearchDialogOpen(false), m_pSearchDialog(NULL), m_subtitleNow(1), m_audioNow(1), m_topWidget(Q_NULLPTR) { qDebug()<<"VideoPlayer init"; pStaticVideoPlayer=this; ui->setupUi(this); m_commonFunction = new CommonFunction ; ui->UnFullscreenButton->setVisible(false); //m_pVideoPlayList = VideoPlayList::Instance(this); //m_pVideoPlayList->move(1024,50); //connect(this, SIGNAL(hidePlayList()), m_pVideoPlayList, SLOT(hidePlayList())); //connect(this,SIGNAL(showPlayList()),m_pVideoPlayList,SLOT(showPlayList())); //connect(m_pVideoPlayList,SIGNAL(notifyVideoPlayerToPlay(QList,int)),\ // this,SLOT(on_notifyVideoPlayerToPlay(QList,int))); //connect(m_pVideoPlayList,SIGNAL(notifyVideoPlayerToPlay(QList)),\ // this,SLOT(on_notifyVideoPlayerListChanged(QList))); progressSliderTimer = new QTimer(this); connect(progressSliderTimer,SIGNAL(timeout()),this,SLOT(timerUpdate())); connect(ui->progressSlider,SIGNAL(sliderMoved(int)),this,SLOT(onProgressSliderMoved(int))); connect(ui->progressSlider,SIGNAL(sliderReleased()),this,SLOT(onProgressSliderReleased())); connect(ui->nextVideoButton,SIGNAL(clicked()),this,SLOT(on_nextVideoButton_clicked())); connect(this, SIGNAL(msgSignal(int,void*,void*,int)), this, SLOT(doMsgCmd(int,void*,void*,int))); // setAttribute(Qt::WA_TranslucentBackground, true); // setStyleSheet("background-color: transparent"); ui->playListButton->hide(); ui->nextVideoButton->hide(); ui->preVideoButton->hide(); ui->subtitleSelectButton->hide(); } /************************************** Function: Description: ***************************************/ VideoPlayer::~VideoPlayer() { delete ui; self = NULL; } QString VideoPlayer::id() { return "video"; } void VideoPlayer::initUi() { m_topWidget = topLevelWidget(); if(m_topWidget){ this->setAttribute(Qt::WA_NoSystemBackground, true); this->setAttribute(Qt::WA_OpaquePaintEvent, true); } } void VideoPlayer::setFullScrren(bool bFullScrren) { //视频窗口非QT窗口 窗口显示视频的实际是QT透明和更改显示区域和大小,不能同时用于多个窗口视频同时显示,需要关闭当前在显示另一个. if(bridgt() && m_topWidget &&ap){ bridgt()->setTitleBarVisible(!bFullScrren); //设置是否拥有背景,没有则透明. m_topWidget->setAttribute(Qt::WA_NoSystemBackground, bFullScrren); QPoint pos(0,0); //默认全屏的尺寸. int width = m_topWidget->width(); int height= m_topWidget->height(); if(!bFullScrren){ //VideoPlayer 位置. pos = mapTo(m_topWidget, this->pos()); width = this->width(); height = this->height(); } ap->setViewWindow(pos.x(), pos.y(), width, height); m_topWidget->update(); //必须调用更新窗口.否则无法全屏. } } /************************************** Function: Description: ***************************************/ void VideoPlayer::initAutPlayer() { if(m_curVideoFilePath.isEmpty()) { qDebug()<<"file path is emtpy"; } if(ap != NULL){ delete ap; ap = NULL; } ap = new AUTPlayer(); if(ap == NULL){ qDebug()<<"new AutPlayer error"; return ; } m_bPlayState = STOPPED; int ret = ap->setUrl((char*)m_curVideoFilePath.toLatin1().data()); if(ret != 0){ qDebug()<<"this video set url errror"; return; } ui->videoNameLabel->setText(m_curVideoFileName); ap->setUserCb(autVideoCb_func, this); m_totalDuration = ap->getDuration() / 1000; ui->progressSlider->setRange(0,m_totalDuration * 1000); m_pretime=0; m_curDuration=0; ui->totalTimeLabel->setText(getTimeStr(m_totalDuration,m_totalDuration)); ui->curTimeLabel->setText(getTimeStr(m_curDuration,m_totalDuration)); ret = ap->play(); if(ret != 0) qDebug()<<"play bug"; ui->playButton->setIcon(QIcon(":/images/playerIcons/Pause.png")); ui->playButton->setIconSize(QSize(50,50)); m_bPlayState = PLAYING; progressSliderTimer->start(1000); } /************************************** Function: Description: ***************************************/ void VideoPlayer::startPlay() { bool isExist = m_commonFunction->fileExistOrNot(m_curVideoFilePath); if(!isExist){ qDebug()<<"file exist"; frmMessageBox *msg = new frmMessageBox(this); msg->SetMessage(QString("文件不存在!"),0); msg->exec(); return; } if(!m_commonFunction->video_support_or_not(this,m_curVideoFilePath)){ qDebug()<<"format not support"; return ; } initAutPlayer(); } /************************************** Function: Description: ***************************************/ void VideoPlayer::onProgressSliderMoved(int value) { qDebug()<<"slider is moved"<stop(); if (!ui->progressSlider->isSliderDown()) { ui->progressSlider->setValue(value); } ui->progressSlider->setValue(value); updateDurationInfo(value / 1000); progressSliderTimer->start(1000); } /************************************** Function: Description: ***************************************/ void VideoPlayer::onProgressSliderReleased() { qDebug()<<"slider is released"; if(m_bPlayState == PAUSEED) on_playButton_clicked(); progressSliderTimer->stop(); m_bPlayState = SEEKING; if(ap != NULL){ ap->seekto(ui->progressSlider->value() / 1000); } progressSliderTimer->start(1000); m_bPlayState = PLAYING; } /************************************** Function: Description: ***************************************/ void VideoPlayer::timerUpdate() { if (ap == NULL) return ; m_curDuration = ap->getCurrentPosition(); //qDebug()<<"timerUpdate "<progressSlider->setValue(m_curDuration); updateDurationInfo(m_curDuration / 1000); } /************************************** Function: Description: ***************************************/ void VideoPlayer::updateDurationInfo(qint64 currentInfo) { QString tStr; if (ap == NULL) return ; if(currentInfo >= m_totalDuration) { currentInfo = 0; m_bPlayState = STOPPED; ui->playButton->setIcon(QIcon(":/images/playerIcons/Play.png")); ui->playButton->setIconSize(QSize(50,50)); progressSliderTimer->stop(); } tStr=getTimeStr(currentInfo,m_totalDuration); ui->curTimeLabel->setText(tStr); //qDebug()<<"update the time label" << tStr << " " << tStr.compare("") << " " <close(); if(m_bPlayState == PLAYING){ on_playButton_clicked(); } if(ap != NULL){ ap->stop(); m_bPlayState = STOPPED; progressSliderTimer->stop(); ui->progressSlider->setValue(0); ui->curTimeLabel->setText(tStr); } if(m_bSearchDialogOpen == true){ m_bSearchDialogOpen = false; m_pSearchDialog->hide(); } } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_fullScreenButton_clicked() { ui->UnFullscreenButton->setVisible(true); ui->controlButtonwidget->setVisible(false); ui->progressSlider->setVisible(false); ui->sliderAndTimeLabelWidget->move(0,570); ui->subtitlewidget->move(0,510); ui->videoNameWidget->move(0,5); this->setFullScrren(true); // this->bridgt()->setTitleBarVisible(false); // if(ap && m_topWidget){ // m_topWidget->setAttribute(Qt::WA_NoSystemBackground, true); // ap->setViewWindow(m_topWidget->x(), m_topWidget->y(), m_topWidget->width(), m_topWidget->height()); // m_topWidget->update(); // } } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_UnFullscreenButton_clicked() { ui->UnFullscreenButton->setVisible(false); ui->controlButtonwidget->setVisible(true); ui->progressSlider->setVisible(true); ui->sliderAndTimeLabelWidget->move(0,490); ui->subtitlewidget->move(0,430); ui->videoNameWidget->move(0,70); this->setFullScrren(false); // this->bridgt()->setTitleBarVisible(true); // if(ap && m_topWidget){ // m_topWidget->setAttribute(Qt::WA_NoSystemBackground, false); // QPoint point = this->mapTo(m_topWidget, this->pos()); // ap->setViewWindow(point.x(), point.y(), this->width(), this->height()); // m_topWidget->update(); // } } /************************************** Function: Description: ***************************************/ void VideoPlayer::stopPlay() { if(ap != NULL){ ap->stop(); } } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_preVideoButton_clicked() { if(m_curVideoFileCount -1 >= 0){ m_curVideoFileCount--; m_curVideoFilePath = m_playList.at(m_curVideoFileCount).absoluteFilePath(); m_curVideoFileName = m_playList.at(m_curVideoFileCount).completeBaseName(); progressSliderTimer->stop(); stopPlay(); startPlay(); }else{ return ; } } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_nextVideoButton_clicked() { qDebug()<<"on_nextVideoButton_clicked "; if(m_curVideoFileCount + 1 < m_playList.count()){ m_curVideoFileCount++; m_curVideoFilePath = m_playList.at(m_curVideoFileCount).absoluteFilePath(); m_curVideoFileName = m_playList.at(m_curVideoFileCount).completeBaseName(); progressSliderTimer->stop(); stopPlay(); startPlay(); }else{ return ; } } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_playButton_clicked() { int ret=0; if(ap != NULL){ ret = ap->pause(); if(ret != 0) qDebug()<<"pause bug"; if (m_bPlayState == PAUSEED){ m_bPlayState = PLAYING; ui->playButton->setIcon(QIcon(":/images/playerIcons/Pause.png")); } else if(m_bPlayState == PLAYING){ m_bPlayState = PAUSEED; ui->playButton->setIcon(QIcon(":/images/playerIcons/Play.png")); } else if(m_bPlayState == STOPPED){ initAutPlayer(); } } } /************************************** Function: Description: ***************************************/ /*/ void VideoPlayer::on_playListButton_clicked() { qDebug()<<"on_playListButton_clicked "<move(1024,50); } if(m_bVideoListOpen == false){ emit showPlayList(); m_bVideoListOpen = true; } else { emit hidePlayList(); m_bVideoListOpen = false; } } */ /************************************** Function: Description: ***************************************/ void VideoPlayer::on_searchButton_clicked() { if(m_pSearchDialog == NULL){ m_pSearchDialog = new searchFileDialog(1,this); connect(m_pSearchDialog,SIGNAL(notifyVideoPlayerAddFile(QString)),\ this,SLOT(on_notifyVideoPlayerAddFile(QString))); connect(m_pSearchDialog,SIGNAL(notifyVideoCancelSearch()),\ this,SLOT(on_notifyVideoCancelSearch())); } if(m_bSearchDialogOpen == false){ m_bSearchDialogOpen = true; m_pSearchDialog->showD(); } else { m_bSearchDialogOpen = false; m_pSearchDialog->hide(); } } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_notifyVideoPlayerToPlay(QList playList,int row) { //hide the playlist emit hidePlayList(); m_bVideoListOpen = 0; m_playList = playList; if(m_playList.count() < row){ qDebug()<<"playlist is wrong"; return; } m_curVideoFileName = m_playList.at(row).completeBaseName(); m_curVideoFilePath = m_playList.at(row).absoluteFilePath(); m_curVideoFileCount = row; stopPlay(); startPlay(); } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_notifyVideoPlayerListChanged(QList playList) { m_playList = playList; } /************************************** Function: Description: ***************************************/ void VideoPlayer::getNumOfAudioAndSubtitle(int& subtitleNum,int& audioNum) { if(ap != NULL){ MediaInfo* mediaInfo = ap->getMediaInfo(); subtitleNum = mediaInfo->nSubtitleStreamNum; audioNum = mediaInfo->nAudioStreamNum; }else{ frmMessageBox* msg = new frmMessageBox(); msg->SetMessage("播放器没有在运行!!",0); msg->show(); } } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_subtitleSelectButton_clicked() { QFileDialog *fileDialog = new QFileDialog(this); fileDialog->setStyleSheet("font: bold; font-size:20px; color: rgb(0,0,0); background-color: rgb(170, 255, 255)"); fileDialog->setFileMode(QFileDialog::Directory); fileDialog->setDirectory("/mnt/"); if(fileDialog->exec() == QDialog::Accepted) { str_SelectPath = fileDialog->getExistingDirectory(); qDebug()<<"the path is "<< str_SelectPath; }else{ } } /************************************** Function: Description: ***************************************/ void VideoPlayer::setSubtitleNum(QAction* pAction) { QString file = pAction->text(); pAction->setCheckable(true); pAction->setChecked(true); qDebug()<<"triggered the "<switchSubtitle(num); } /************************************** Function: Description: ***************************************/ void VideoPlayer::setAudioNum(QAction* pAction) { QString file = pAction->text(); pAction->setCheckable(true); pAction->setChecked(true); qDebug()<<"triggered the "<switchAudio(num); } /************************************** Function: Description: ***************************************/ void VideoPlayer::doMsgCmd(int msgType, void* user,void* data,int len) { switch(msgType) { case 128: break; case SUBCTRL_SUBTITLE_AVAILABLE: break; case SUBCTRL_SUBTITLE_EXPIRED: break; case AWPLAYER_MEDIA_PREPARED: break; case AWPLAYER_MEDIA_PLAYBACK_COMPLETE: on_nextVideoButton_clicked(); break; case AWPLAYER_MEDIA_SEEK_COMPLETE: break; case AWPLAYER_MEDIA_STARTED: break; case AWPLAYER_MEDIA_LOG_RECORDER: break; case AWPLAYER_MEDIA_BUFFERING_UPDATE: break; case AWPLAYER_MEDIA_ERROR: break; case AWPLAYER_MEDIA_INFO: break; case AWPLAYER_MEDIA_SET_VIDEO_SIZE: break; default: break; } } /************************************** Function: Description: ***************************************/ int VideoPlayer::autVideoCb_func(int msgType, void* user,void* data,int len) { emit pStaticVideoPlayer->msgSignal(msgType,user,data,len); } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_notifyVideoPlayerAddFile(QString strpath) { m_curVideoFilePath = strpath; QFileInfo fileInfo(strpath); m_curVideoFileName = fileInfo.fileName(); if(m_bSearchDialogOpen == true){ m_bSearchDialogOpen = false; m_pSearchDialog->hide(); } qDebug()<<"on_notifyVideoPlayerAddFile "<< m_curVideoFilePath << " "<< m_curVideoFileName; AudioSink sink; sink.setOutType(AudioSink::WM8960_OUT); stopPlay(); startPlay(); } /************************************** Function: Description: ***************************************/ void VideoPlayer::on_notifyVideoCancelSearch() { qDebug()<<"on_notifyVideoCancelSearch "; if(m_bSearchDialogOpen == true){ m_bSearchDialogOpen = false; m_pSearchDialog->hide(); } }