Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "radio.h"
  2. #include "yaml-cpp/yaml.h"
  3. #include <QDebug>
  4. #include <QUrlQuery>
  5. Radio::Radio(QObject *parent) : QObject(parent), logo(0, 0)
  6. {
  7. mgr = 0;
  8. logoTries = 0;
  9. currentSong = 0;
  10. songsTries = 0;
  11. streamTries = 0;
  12. songTimer = new QTimer(this);
  13. songTimer->setSingleShot(true);
  14. connect(songTimer, SIGNAL(timeout()), this, SLOT(songEnded()));
  15. player = new QMediaPlayer(this);
  16. player->setVolume(50);
  17. }
  18. QString Radio::getName()
  19. {
  20. return name;
  21. }
  22. QString Radio::getClaim()
  23. {
  24. return claim;
  25. }
  26. QColor Radio::getTextColor()
  27. {
  28. return textColor;
  29. }
  30. QUrl Radio::getLogoUrl()
  31. {
  32. return logoUrl;
  33. }
  34. QUrl Radio::getStreamUrl()
  35. {
  36. return streamUrl;
  37. }
  38. int Radio::getId()
  39. {
  40. return id;
  41. }
  42. QUrl Radio::getApiBase()
  43. {
  44. return apiBase;
  45. }
  46. QUrl Radio::getCoverBase()
  47. {
  48. return coverBase;
  49. }
  50. Song *Radio::getCurrentSong()
  51. {
  52. return currentSong;
  53. }
  54. void Radio::setName(QString n)
  55. {
  56. name = n;
  57. }
  58. void Radio::setClaim(QString c)
  59. {
  60. claim = c;
  61. }
  62. void Radio::setTextColor(QColor c)
  63. {
  64. textColor = c;
  65. }
  66. void Radio::setLogoUrl(QUrl u)
  67. {
  68. logoUrl = u;
  69. }
  70. void Radio::setStreamUrl(QUrl u)
  71. {
  72. streamUrl = u;
  73. }
  74. void Radio::setId(int i)
  75. {
  76. id = i;
  77. }
  78. void Radio::setApiBase(QUrl u)
  79. {
  80. apiBase = u;
  81. }
  82. void Radio::setCoverBase(QUrl u)
  83. {
  84. coverBase = u;
  85. }
  86. void Radio::setCurrentSong(Song *s)
  87. {
  88. if(currentSong != 0)
  89. currentSong->deleteLater();
  90. currentSong = s;
  91. songTimer->stop();
  92. if(s->getDuration() < 0)
  93. qDebug()<<s->getDuration()<<name;
  94. songTimer->setInterval(s->getDuration());
  95. songTimer->start();
  96. nextSongs.removeOne(currentSong);
  97. updateNextSongs();
  98. emit songChanged(currentSong);
  99. }
  100. void Radio::updateNextSongs()
  101. {
  102. songsTries = 0;
  103. QUrl url = apiBase;
  104. QUrlQuery query;
  105. query.addQueryItem("act", "get_plist");
  106. query.addQueryItem("id_wr", QString::number(id));
  107. url.setQuery(query);
  108. QNetworkReply* reply = mgr->get(QNetworkRequest(url));
  109. connect(reply, SIGNAL(finished()), this, SLOT(songsFinished()));
  110. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(songsError(QNetworkReply::NetworkError)));
  111. }
  112. void Radio::downloadLogo()
  113. {
  114. if(logo.isNull())
  115. {
  116. logoTries = 0;
  117. QNetworkReply* reply = mgr->get(QNetworkRequest(logoUrl));
  118. connect(reply, SIGNAL(finished()), this, SLOT(logoFinished()));
  119. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(logoError(QNetworkReply::NetworkError)));
  120. }
  121. else
  122. emit logoDownloaded(logo);
  123. }
  124. void Radio::startStream()
  125. {
  126. stopStream();
  127. streamTries = 0;
  128. player->setMedia(streamUrl);
  129. player->play();
  130. }
  131. void Radio::stopStream()
  132. {
  133. player->stop();
  134. }
  135. void Radio::logoFinished()
  136. {
  137. QNetworkReply* reply = (QNetworkReply*)sender();
  138. if(logo.loadFromData(reply->readAll()) && !logo.isNull())
  139. emit logoDownloaded(logo);
  140. }
  141. void Radio::logoError(QNetworkReply::NetworkError)
  142. {
  143. if(logoTries < 3)
  144. {
  145. ++logoTries;
  146. downloadLogo();
  147. }
  148. }
  149. void Radio::songsFinished()
  150. {
  151. QNetworkReply* reply = (QNetworkReply*)sender();
  152. YAML::Node itms = YAML::Load(reply->readAll().constData())["itms"];
  153. qDeleteAll(nextSongs);
  154. for(unsigned i = 1;i < itms.size(); ++i)
  155. {
  156. YAML::Node itm = itms[i];
  157. if(itm["id"].as<int>() == -1)
  158. continue;
  159. Song* song = new Song(this);
  160. song->setDuration(itm["t_dur"].as<int>());
  161. song->setArtist(itm["art"].as<std::string>().c_str());
  162. song->setTitle(itm["tit"].as<std::string>().c_str());
  163. QUrl url = coverBase;
  164. url.setPath(url.path() + itm["cov"].as<std::string>().c_str());
  165. song->setCoverUrl(url);
  166. nextSongs.append(song);
  167. }
  168. }
  169. void Radio::songsError(QNetworkReply::NetworkError)
  170. {
  171. if(songsTries < 3)
  172. {
  173. ++songsTries;
  174. updateNextSongs();
  175. }
  176. }
  177. /*void Radio::streamRead()
  178. {
  179. if(stream->header(QNetworkRequest::LocationHeader).toUrl().isEmpty())
  180. emit streamData(stream->readAll());
  181. else
  182. {
  183. streamUrl = stream->header(QNetworkRequest::LocationHeader).toUrl();
  184. stopStream();
  185. startStream();
  186. }
  187. }*/
  188. void Radio::streamError()
  189. {
  190. if(streamTries < 3)
  191. {
  192. ++streamTries;
  193. stopStream();
  194. startStream();
  195. }
  196. }
  197. void Radio::songEnded()
  198. {
  199. if(!nextSongs.isEmpty())
  200. setCurrentSong(nextSongs.at(0));
  201. }
  202. void Radio::setNetworkManager(QNetworkAccessManager *m)
  203. {
  204. mgr = m;
  205. }