Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "musicplayer.h"
- #include "ui_musicplayer.h"
- MusicPlayer::MusicPlayer(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MusicPlayer)
- {
- ui->setupUi(this);
- Player = new QMediaPlayer(this);
- PlayList = new QMediaPlaylist;
- connect(Player,SIGNAL(positionChanged(qint64)),this,SLOT(PositionChanged(qint64)));
- connect(ui->LengthSlider,SIGNAL(valueChanged(int)),this,SLOT(SetPosition(int)));
- connect(Player,SIGNAL(durationChanged(qint64)),this,SLOT(DurationChanged(qint64)));
- connect(Player,SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),this,SLOT(StatusChanged(QMediaPlayer::MediaStatus)));
- //przyciski
- connect(ui->button_start,SIGNAL(released()),this,SLOT(Play()));
- connect(ui->button_stop,SIGNAL(released()),this,SLOT(Stop()));
- connect(ui->button_next,SIGNAL(released()),this,SLOT(Next()));
- connect(ui->button_prev,SIGNAL(released()),this,SLOT(Prev()));
- connect(ui->button_add,SIGNAL(released()),this,SLOT(Add()));
- connect(ui->button_remove,SIGNAL(released()),this,SLOT(Remove()));
- connect(ui->playlist,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(PlaySelected(QModelIndex)));
- Player->setPlaylist(PlayList);
- Player->bufferStatusChanged(4096);
- QAudioEncoderSettings audioSettings;
- audioSettings.setCodec("audio/mpeg");
- audioSettings.setChannelCount(4);
- }
- MusicPlayer::~MusicPlayer()
- {
- delete ui;
- }
- void MusicPlayer::PositionChanged(qint64 time)
- {
- ui->LengthSlider->setValue(time);
- }
- void MusicPlayer::DurationChanged(qint64 duration)
- {
- ui->LengthSlider->setMaximum(duration);
- ui->LengthSlider->setValue(0);
- }
- void MusicPlayer::StatusChanged(QMediaPlayer::MediaStatus status)
- {
- switch(status)
- {
- case QMediaPlayer::NoMedia:
- QMessageBox::information(this,"No media availabe","Playlist is empty");
- Stop();
- break;
- case QMediaPlayer::EndOfMedia:
- Stop();
- break;
- case QMediaPlayer::InvalidMedia:
- QMessageBox::warning(this,"Invalid media","Format used in burrent media is not supported");
- break;
- default:
- break;
- }
- }
- void MusicPlayer::SetPosition(int position)
- {
- Player->setPosition(position);
- }
- void MusicPlayer::Play()
- {
- if(Player->state()!=QMediaPlayer::PlayingState)
- {
- Player->play();
- ui->LengthSlider->setMaximum(Player->duration());
- }
- else
- {
- Player->pause();
- }
- }
- void MusicPlayer::Stop()
- {
- Player->stop();
- }
- void MusicPlayer::Next()
- {
- int tmp = PlayList->currentIndex()+1;
- if(tmp>=ui->playlist->count())
- PlayList->setCurrentIndex(0);
- else
- PlayList->next();
- }
- void MusicPlayer::Prev()
- {
- int tmp = PlayList->previousIndex();
- if(tmp < 0)
- PlayList->setCurrentIndex(ui->playlist->count()-1);
- else
- PlayList->previous();
- }
- void MusicPlayer::Add()
- {
- QFileDialog dialog(this);
- dialog.setNameFilter(tr("Music (*.mp3)"));
- dialog.setFileMode(QFileDialog::ExistingFile);
- QStringList paths;
- if(dialog.exec())
- paths=dialog.selectedFiles();
- for(int i=0;i<paths.size();i++)
- {
- ui->playlist->addItem(paths[i]);
- PlayList->addMedia(QUrl::fromLocalFile(paths[i]));
- }
- }
- void MusicPlayer::Remove()
- {
- ui->playlist->clear();
- Stop();
- PlayList->clear();
- }
- void MusicPlayer::PlaySelected(QModelIndex index)
- {
- PlayList->setCurrentIndex(index.row());
- if(Player->state() != QMediaPlayer::PlayingState)
- Play();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement