void Audio::playBGM(const QString &filename) { if (!BGMPlayer) BGMPlayer = new QMediaPlayer; QMediaPlaylist *BGMList = new QMediaPlaylist(BGMPlayer); BGMList->addMedia(QUrl(filename)); QDir BGMDir("audio/system/bgm"); QStringList mp3Filters("*.mp3"); QFileInfoList mp3List = BGMDir.entryInfoList(mp3Filters, QDir::Files); foreach (QFileInfo file, mp3List) { BGMList->addMedia(QUrl(file.filePath())); }
Timer::Timer(QString filename, int period, QKeySequence keySequence, UINT modifiers, UINT virtualKey, unsigned int hotkeyId) : filename(filename) , period(period) , keySequence(keySequence) , modifiers(modifiers) , virtualKey(virtualKey) , hotkeyId(hotkeyId) , broken(false) { // Register the hotkey (add the MOD_NOREPEAT flag) if (!RegisterHotKey(nullptr, hotkeyId, modifiers | getNoRepeatFlag(), virtualKey)) { throw SMException(tr("failed to register the hotkey.")); } // Create the player, the playlist and the media container this->mediaContent = new QMediaContent(filename); this->player = new QMediaPlayer(this); QMediaPlaylist* playlist = new QMediaPlaylist(this); // Give the media to the playlist, and the playlist to the player playlist->addMedia(*mediaContent); playlist->setPlaybackMode(QMediaPlaylist::CurrentItemOnce); player->setMedia(playlist); // Create the timer this->timer = new QTimer(this); this->timer->setInterval(period * 1000); // Connections connect(this->player, &QMediaPlayer::mediaStatusChanged, this, &Timer::mediaStatusChanged); connect(this->player, static_cast<void (QMediaPlayer::*)(QMediaPlayer::Error)>(&QMediaPlayer::error), this, &Timer::error); connect(this->timer, &QTimer::timeout, this, &Timer::playSound); connect(MainWindow::instance()->getNativeEventFilter(), &NativeEventFilter::hotkeyReceived, this, &Timer::togglePlayStop); }
SoundClass::SoundClass(QString filePath) { /* mediaObject=new Phonon::MediaObject(0); audioOutput=new Phonon::AudioOutput(Phonon::MusicCategory,0); Phonon::createPath(mediaObject,audioOutput); mediaObject->setCurrentSource(filePath); mediaObject-> // mySound=new QSound(filePath,qApp); //mySound->setLoops(100); play=false;*/ player = new QMediaPlayer; //connect(player,SIGNAL()) QUrl url = QUrl::fromLocalFile(filePath);//path + QLatin1String("/qml/lightapp/moto.wav")); QMediaPlaylist * playlist = new QMediaPlaylist(); playlist->addMedia(url); playlist->setPlaybackMode( QMediaPlaylist::Loop ); player->setPlaylist( playlist ); playlist->setCurrentIndex(0); player->setVolume(60); // player->play(); }
VideoPlayer::VideoPlayer(QWidget *parent) : QWidget(parent) , mediaPlayer(0, QMediaPlayer::VideoSurface) , playButton(0) , positionSlider(0) , errorLabel(0) { QVideoWidget *videoWidget = new QVideoWidget; QAbstractButton *openButton = new QPushButton(tr("Open...")); connect(openButton, SIGNAL(clicked()), this, SLOT(openFile())); playButton = new QPushButton; playButton->setEnabled(false); playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); connect(playButton, SIGNAL(clicked()), this, SLOT(play())); positionSlider = new QSlider(Qt::Horizontal); positionSlider->setRange(0, 0); connect(positionSlider, SIGNAL(sliderMoved(int)), this, SLOT(setPosition(int))); errorLabel = new QLabel; errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); QBoxLayout *controlLayout = new QHBoxLayout; controlLayout->setMargin(0); controlLayout->addWidget(openButton); controlLayout->addWidget(playButton); controlLayout->addWidget(positionSlider); QBoxLayout *layout = new QVBoxLayout; layout->addWidget(videoWidget); layout->addLayout(controlLayout); layout->addWidget(errorLabel); setLayout(layout); mediaPlayer.setVideoOutput(videoWidget); connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(mediaStateChanged(QMediaPlayer::State))); connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64))); connect(&mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError())); QFileInfo info("C:\\Users\\MichaU\\Desktop\\ResonanceP1.mp4"); QMediaPlaylist *lista = new QMediaPlaylist; lista->addMedia(QUrl::fromLocalFile(info.absoluteFilePath())); mediaPlayer.setPlaylist(lista); mediaPlayer.play(); connect(lista,SIGNAL(currentIndexChanged(int)),this,SLOT(ChangePlaylist())); }
QGraphicsItem *AudioElement::render(const bool interactive) { if(interactive || !getValue(QStringLiteral("visible")).toBool()) return 0; player = new QMediaPlayer; player->setVolume(getValue(QStringLiteral("volume")).toInt()); connect(player, &QMediaPlayer::stateChanged, this, &AudioElement::stateChanged); QMediaPlaylist *playlist = new QMediaPlaylist(player); playlist->addMedia(QUrl::fromLocalFile(getValue(QStringLiteral("src")).toString())); if(getValue(QStringLiteral("loop")).toBool()) playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop); player->setPlaylist(playlist); return 0; }
void MainWindow::loadPreview() { QMediaPlaylist* playlist = new QMediaPlaylist; playlist->addMedia(QUrl::fromLocalFile(ui->filePath->text())); playlist->setCurrentIndex(1); player->play(); player->isSeekable(); player->setMuted(true); player->setPlaylist(playlist); player->setVideoOutput(videoWidget); player->pause(); videoWidget->show(); videoWidget->hide(); videoWidget->resize(ui->graphicsView->width(), ui->graphicsView->height()); videoWidget->setGeometry(0, 0, ui->graphicsView->width(), ui->graphicsView->height()); videoWidget->show(); }
void MainDialog::init() { ui->label->setAlignment(Qt::AlignCenter); ui->label->setText("登陆名:"+name); this->setFixedSize(this->size()); QPalette palette; QPixmap pixmap(":/images/background.jpg"); pixmap = pixmap.scaled(this->width(),this->height()); palette.setBrush(QPalette::Background, QBrush(pixmap)); this->setPalette(palette); player = new QMediaPlayer; QMediaPlaylist * playList = new QMediaPlaylist; playList->addMedia(QMediaContent(QUrl("qrc:/sounds/BGM.mp3"))); playList->setPlaybackMode(QMediaPlaylist::Loop); player->setPlaylist(playList); player->setVolume(60); creatTrayIcon(); }
void PlayListDlg::triggerAddAudio() { QString fileName, audioName, formatName; QMediaPlaylist *playList = MyPlayList::getInstance(); QStringList list = QFileDialog::getOpenFileNames( this, "选择需要添加的音乐文件"); if (list.isEmpty()) return ; foreach(QString absFilesPath, list) { fileName = QFileInfo(absFilesPath).fileName(); formatName = fileName.mid(fileName.indexOf(".")); if (formatName != ".mp3") continue ; audioName = fileName.left(fileName.indexOf(".")); m_stdItemModel->setItem(m_mediaCount ++, new QStandardItem(audioName)); playList->addMedia(QUrl::fromLocalFile(absFilesPath)); }
//打开播放临时文件 void MusicList::openTempFile(QString file) { // 在界面上创建“临时列表” QTreeWidgetItem *toplevelItem = new QTreeWidgetItem(QStringList(QString("临时列表"))); this->addTopLevelItem(toplevelItem); QTreeWidgetItem *musicItem = new QTreeWidgetItem(QStringList(QFileInfo(file).fileName())); this->topLevelItem(this->topLevelItemCount()-1)->addChild(musicItem); // 创建临时播放列表 QMediaPlaylist *playlist = new QMediaPlaylist(this); playlistVector.append(playlist); // play playlist->addMedia(QUrl::fromLocalFile(file)); // completerList.append(QFileInfo(query_musicName.value("musicName").toString()).fileName()); this->topLevelItem(0)->setExpanded(false); this->topLevelItem(this->topLevelItemCount()-1)->setExpanded(true); // this->setCurrentItem(createItem); int toplevelIndex = this->topLevelItemCount()-1; int musicIndex = this->topLevelItem(toplevelIndex)->childCount()-1; playMusic(toplevelIndex, musicIndex); }
MainWindow::MainWindow(QWidget *parent, const QString& theHostName) : QMainWindow(parent), hostName(theHostName) { setObjectName(QStringLiteral("MainWindow")); resize(400, 300); QMediaPlaylist* playlist = new QMediaPlaylist; playlist->addMedia(QUrl("http://" + hostName + "/~andreas/out.mpg")); // playlist->addMedia(QUrl("http://" + hostName + ":1234/dvd.mp4")); player = new QMediaPlayer(this); // , QMediaPlayer::StreamPlayback); player->setPlaylist(playlist); QWidget* centralWidget = new QWidget(this); QBoxLayout* mainLayout = new QVBoxLayout(); centralWidget->setLayout(mainLayout); QWidget* buttonArea = new QWidget(this); QHBoxLayout* buttonLayout = new QHBoxLayout(); buttonArea->setLayout(buttonLayout); QVideoWidget* videoWidget = new QVideoWidget(centralWidget); mainLayout->addWidget(videoWidget, 1); mainLayout->addWidget(buttonArea); connectButton = new QPushButton("Connect", buttonArea); startPlayButton = new QPushButton("Play", buttonArea); startPlayButton->setEnabled(false); stopPlayButton = new QPushButton("Stop", buttonArea); stopPlayButton->setEnabled(false); streamButton = new QPushButton("Stream", buttonArea); streamButton->setEnabled(false); buttonLayout->addWidget(connectButton); buttonLayout->addWidget(startPlayButton); buttonLayout->addWidget(stopPlayButton); buttonLayout->addWidget(streamButton); tcpSocket = new QTcpSocket(this); connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(statusChanged(QMediaPlayer::MediaStatus))); connect(connectButton, SIGNAL(clicked()), this, SLOT(doConnect())); connect(startPlayButton, SIGNAL(clicked()), this, SLOT(doStartPlay())); connect(stopPlayButton, SIGNAL(clicked()), this, SLOT(doStopPlay())); connect(streamButton, SIGNAL(clicked()), this, SLOT(doStream())); connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError))); connect(tcpSocket, SIGNAL(connected()), this, SLOT(socketConnected())); connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData())); player->setVideoOutput(videoWidget); setCentralWidget(centralWidget); player->play(); }