Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. #include "musicplayer.h"
  2. #include "ui_musicplayer.h"
  3.  
  4. MusicPlayer::MusicPlayer(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MusicPlayer)
  7. {
  8.  
  9. ui->setupUi(this);
  10.  
  11. Player = new QMediaPlayer(this);
  12. PlayList = new QMediaPlaylist;
  13.  
  14. connect(Player,SIGNAL(positionChanged(qint64)),this,SLOT(PositionChanged(qint64)));
  15. connect(ui->LengthSlider,SIGNAL(valueChanged(int)),this,SLOT(SetPosition(int)));
  16. connect(Player,SIGNAL(durationChanged(qint64)),this,SLOT(DurationChanged(qint64)));
  17. connect(Player,SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),this,SLOT(StatusChanged(QMediaPlayer::MediaStatus)));
  18.  
  19. //przyciski
  20. connect(ui->button_start,SIGNAL(released()),this,SLOT(Play()));
  21. connect(ui->button_stop,SIGNAL(released()),this,SLOT(Stop()));
  22.  
  23. connect(ui->button_next,SIGNAL(released()),this,SLOT(Next()));
  24. connect(ui->button_prev,SIGNAL(released()),this,SLOT(Prev()));
  25.  
  26. connect(ui->button_add,SIGNAL(released()),this,SLOT(Add()));
  27. connect(ui->button_remove,SIGNAL(released()),this,SLOT(Remove()));
  28.  
  29. connect(ui->playlist,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(PlaySelected(QModelIndex)));
  30.  
  31. Player->setPlaylist(PlayList);
  32. Player->bufferStatusChanged(4096);
  33.  
  34. QAudioEncoderSettings audioSettings;
  35. audioSettings.setCodec("audio/mpeg");
  36. audioSettings.setChannelCount(4);
  37. }
  38.  
  39. MusicPlayer::~MusicPlayer()
  40. {
  41. delete ui;
  42. }
  43.  
  44. void MusicPlayer::PositionChanged(qint64 time)
  45. {
  46. ui->LengthSlider->setValue(time);
  47. }
  48.  
  49. void MusicPlayer::DurationChanged(qint64 duration)
  50. {
  51. ui->LengthSlider->setMaximum(duration);
  52. ui->LengthSlider->setValue(0);
  53. }
  54.  
  55. void MusicPlayer::StatusChanged(QMediaPlayer::MediaStatus status)
  56. {
  57. switch(status)
  58. {
  59. case QMediaPlayer::NoMedia:
  60. QMessageBox::information(this,"No media availabe","Playlist is empty");
  61. Stop();
  62. break;
  63. case QMediaPlayer::EndOfMedia:
  64. Stop();
  65. break;
  66. case QMediaPlayer::InvalidMedia:
  67. QMessageBox::warning(this,"Invalid media","Format used in burrent media is not supported");
  68. break;
  69.  
  70. default:
  71. break;
  72. }
  73. }
  74.  
  75. void MusicPlayer::SetPosition(int position)
  76. {
  77. Player->setPosition(position);
  78. }
  79.  
  80. void MusicPlayer::Play()
  81. {
  82. if(Player->state()!=QMediaPlayer::PlayingState)
  83. {
  84. Player->play();
  85.  
  86. ui->LengthSlider->setMaximum(Player->duration());
  87. }
  88. else
  89. {
  90. Player->pause();
  91. }
  92. }
  93.  
  94. void MusicPlayer::Stop()
  95. {
  96. Player->stop();
  97. }
  98.  
  99. void MusicPlayer::Next()
  100. {
  101. int tmp = PlayList->currentIndex()+1;
  102.  
  103. if(tmp>=ui->playlist->count())
  104. PlayList->setCurrentIndex(0);
  105. else
  106. PlayList->next();
  107. }
  108.  
  109. void MusicPlayer::Prev()
  110. {
  111. int tmp = PlayList->previousIndex();
  112.  
  113. if(tmp < 0)
  114. PlayList->setCurrentIndex(ui->playlist->count()-1);
  115. else
  116. PlayList->previous();
  117. }
  118.  
  119. void MusicPlayer::Add()
  120. {
  121. QFileDialog dialog(this);
  122.  
  123. dialog.setNameFilter(tr("Music (*.mp3)"));
  124. dialog.setFileMode(QFileDialog::ExistingFile);
  125.  
  126. QStringList paths;
  127. if(dialog.exec())
  128. paths=dialog.selectedFiles();
  129.  
  130. for(int i=0;i<paths.size();i++)
  131. {
  132. ui->playlist->addItem(paths[i]);
  133.  
  134. PlayList->addMedia(QUrl::fromLocalFile(paths[i]));
  135. }
  136. }
  137.  
  138. void MusicPlayer::Remove()
  139. {
  140. ui->playlist->clear();
  141.  
  142. Stop();
  143. PlayList->clear();
  144. }
  145.  
  146. void MusicPlayer::PlaySelected(QModelIndex index)
  147. {
  148. PlayList->setCurrentIndex(index.row());
  149.  
  150. if(Player->state() != QMediaPlayer::PlayingState)
  151. Play();
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement