audioPlay.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "audioPlay.h"
  2. #include "ui_audioPlay.h"
  3. #include <QFileInfo>
  4. #include "awMsgBox.h"
  5. #include "CCommonTools.h"
  6. #include <QSettings>
  7. #include "CAppInfo.h"
  8. #include "logproc.h"
  9. #include "CAudioPlayerProc.h"
  10. #include "awMsgBox.h"
  11. audioPlay::audioPlay(QString sAudioName, QString sAudioUrl, int nlimitedPlayTimes, QWidget *parent) :
  12. CBaseWidget(parent),
  13. ui(new Ui::audioPlay), m_sAudioName(sAudioName), m_sAudioUrl(sAudioUrl)
  14. {
  15. ui->setupUi(this);
  16. setAttribute(Qt::WA_DeleteOnClose);
  17. setStyleSheet(g_appInfoPtr->m_sQssStr);
  18. m_sRecordDevice = "";
  19. m_nLeftPalyCount = 0;
  20. m_nPlayedCont = 0;
  21. if(g_appInfoPtr->m_jAudioPlayedCount != Json::Value::null &&
  22. g_appInfoPtr->m_jAudioPlayedCount.isMember(sAudioName.toStdString()))
  23. {
  24. m_nPlayedCont = g_appInfoPtr->m_jAudioPlayedCount[sAudioName.toStdString()].asInt();
  25. m_nLeftPalyCount = nlimitedPlayTimes - m_nPlayedCont;
  26. }
  27. else
  28. {
  29. m_nLeftPalyCount = nlimitedPlayTimes;
  30. }
  31. QFileInfo file("coe.cfgi");
  32. QString sFilePath = file.absoluteFilePath();
  33. QSettings set(sFilePath, QSettings::IniFormat);
  34. m_nPlayVol = 100;//set.value("config/playVol", 100).toInt();
  35. if(g_audioPalyerPtr == nullptr)
  36. {
  37. g_audioPalyerPtr = std::make_shared<CAudioPlayerProc>();
  38. }
  39. m_pTimer = new QTimer;
  40. m_pTimer->setInterval(1000);
  41. connect(m_pTimer, &QTimer::timeout, this, &audioPlay::timeOut);
  42. qRegisterMetaType<CDownLoadFileInfo>("CDownLoadFileInfo");
  43. connect(g_httpBllPtr.get(), &CHttpBll::sgnDownLoadFile, this, &audioPlay::onDownLoadFile);
  44. QString sFileName = sAudioUrl.right(sAudioUrl.length() - sAudioUrl.lastIndexOf("/") - 1);
  45. QString sAudioFile = g_appInfoPtr->m_sCacheFileDir + "cache/" + sFileName;
  46. if(!QFile::exists(sAudioFile))
  47. {
  48. CHttpRequestPackage hrp;
  49. hrp.sUri = sAudioUrl;
  50. hrp.sCommonStr = sAudioFile;
  51. hrp.nRequestType = RequestType::rtDownLoadFile;
  52. hrp.nRetryCount = 3;
  53. g_httpBllPtr->downLoad(hrp);
  54. }
  55. this->installEventFilter(this);
  56. }
  57. audioPlay::~audioPlay()
  58. {
  59. if (m_pTimer->isActive())
  60. {
  61. m_pTimer->stop();
  62. g_audioPalyerPtr->stopPlay();
  63. }
  64. delete ui;
  65. }
  66. int audioPlay::setUI(const int nWidth)
  67. {
  68. ui->label_ap_playTimes->setText(QString::fromLocal8Bit(R"(<html>
  69. <style>
  70. .span
  71. {
  72. font-size:%1px;
  73. font-family:"Microsoft YaHei";
  74. font-weight:600;
  75. }
  76. #nText
  77. {
  78. color:rgba(102,102,102,1);
  79. }
  80. #gText
  81. {
  82. color:rgba(19,187,138,1);
  83. }
  84. #rText
  85. {
  86. color:rgba(254,119,100,1);
  87. }
  88. </style>
  89. <body>
  90. <span id="nText">(请<span id="gText">点击播放</span>按钮听音作答。已播次数:<span id="rText">%2</span> ,剩余播放次数:<span id="rText">%3</span> )</span>
  91. </body>
  92. </html>)").arg((int)(g_appInfoPtr->m_fRate*14)).arg(m_nPlayedCont).arg(m_nLeftPalyCount));
  93. QString sFileName = m_sAudioUrl.right(m_sAudioUrl.length() - m_sAudioUrl.lastIndexOf("/") - 1);
  94. QString sAudioFile = g_appInfoPtr->m_sCacheFileDir + "cache/" + sFileName;
  95. int nSecord = g_audioPalyerPtr->GetMediaDuration(sAudioFile);
  96. m_nMaxTestSecord = nSecord;
  97. QString sText = QString("00:00 / %1:%2")
  98. .arg((int)(nSecord/60), 2, 10, QChar('0'))
  99. .arg((int)(nSecord%60), 2, 10, QChar('0'));
  100. ui->label_ap_time->setText(sText);
  101. setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*60);
  102. ui->widget_audioPlay->setGeometry(0, g_appInfoPtr->m_fRate * 10, g_appInfoPtr->m_fRate*555, g_appInfoPtr->m_fRate*40);
  103. ui->btn_ap_play->setGeometry(g_appInfoPtr->m_fRate*8, (ui->widget_audioPlay->height() - g_appInfoPtr->m_fRate*24)/2, g_appInfoPtr->m_fRate*24, g_appInfoPtr->m_fRate*24);
  104. ui->label_ap_time->adjustSize();
  105. ui->label_ap_time->setGeometry(ui->btn_ap_play->x() + ui->btn_ap_play->width() + g_appInfoPtr->m_fRate*7,
  106. (ui->widget_audioPlay->height() - ui->label_ap_time->height())/2, ui->label_ap_time->width(), ui->label_ap_time->height());
  107. if (!QFile::exists(sAudioFile))
  108. {
  109. ui->btn_ap_play->setVisible(false);
  110. ui->label_ap_time->setText(QString::fromLocal8Bit("正在下载音频"));
  111. }
  112. ui->label_ap_playTimes->adjustSize();
  113. ui->label_ap_playTimes->setGeometry(ui->label_ap_time->x() + ui->label_ap_time->width() + g_appInfoPtr->m_fRate*10,
  114. (ui->widget_audioPlay->height() - ui->label_ap_playTimes->height())/2, ui->label_ap_playTimes->width(), ui->label_ap_playTimes->height());
  115. ui->widget_audioPlay->setGeometry(0, g_appInfoPtr->m_fRate * 10, ui->label_ap_playTimes->x() + ui->label_ap_playTimes->width() + g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate*40);
  116. if (m_nLeftPalyCount == 0)
  117. {
  118. ui->btn_ap_play->setStyleSheet("border-image:url(:/images/btn-play_disable.png);");
  119. }
  120. return g_appInfoPtr->m_fRate*60;
  121. }
  122. void audioPlay::onDownLoadFile(CDownLoadFileInfo downLoadFileInfo)
  123. {
  124. QString sFileName = m_sAudioUrl.right(m_sAudioUrl.length() - m_sAudioUrl.lastIndexOf("/") - 1);
  125. QString sAudioFile = g_appInfoPtr->m_sCacheFileDir + "cache/" + sFileName;
  126. if (downLoadFileInfo.sFileName == sAudioFile)
  127. {
  128. if (downLoadFileInfo.nCode == 200)
  129. {
  130. int nSecord = g_audioPalyerPtr->GetMediaDuration(sAudioFile);
  131. m_nMaxTestSecord = nSecord;
  132. QString sText = QString("00:00 / %1:%2")
  133. .arg((int)(nSecord/60), 2, 10, QChar('0'))
  134. .arg((int)(nSecord%60), 2, 10, QChar('0'));
  135. ui->label_ap_time->setText(sText);
  136. ui->btn_ap_play->setVisible(true);
  137. }
  138. else
  139. {
  140. if(downLoadFileInfo.sMessage.isEmpty())
  141. {
  142. ShowMsg(QString::fromLocal8Bit("音频下载失败,请检查网络连接"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
  143. }
  144. else
  145. {
  146. ShowMsg(downLoadFileInfo.sMessage, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
  147. }
  148. }
  149. }
  150. }
  151. bool audioPlay::eventFilter(QObject *obj, QEvent *ev)
  152. {
  153. if(obj == this)
  154. {
  155. //判断事件
  156. if(ev->type() == QEvent::Hide)
  157. {
  158. if(m_pTimer->isActive())
  159. {
  160. m_pTimer->stop();
  161. g_audioPalyerPtr->stopPlay();
  162. }
  163. }
  164. }
  165. return QWidget::eventFilter(obj, ev);
  166. }
  167. void audioPlay::on_btn_ap_play_clicked()
  168. {
  169. if (m_pTimer->isActive())
  170. {
  171. m_pTimer->stop();
  172. g_audioPalyerPtr->pausePlay();
  173. ui->btn_ap_play->setStyleSheet("border-image:url(:/images/btn-audio-play.png);");
  174. }
  175. else
  176. {
  177. if (m_nTestSecord == 0 || m_nMaxTestSecord == 0 || m_nTestSecord >= m_nMaxTestSecord)
  178. {
  179. //播放
  180. if (m_nLeftPalyCount < 1)
  181. {
  182. return;
  183. }
  184. m_nTestSecord = 0;
  185. // m_nMaxTestSecord = 0;
  186. m_sRecordDevice = "";
  187. if (!g_audioPalyerPtr->getDeviceinfo(m_sRecordDevice))
  188. {
  189. m_pTimer->stop();
  190. QString sErrorStr = g_audioPalyerPtr->getErrMsg();
  191. // ShowMsg(sErrorStr, g_appInfoPtr->m_answerWidget);
  192. return;
  193. }
  194. g_audioPalyerPtr->setVol(m_nPlayVol);
  195. QString sFileName = m_sAudioUrl.right(m_sAudioUrl.length() - m_sAudioUrl.lastIndexOf("/") - 1);
  196. QString sAudioFile = g_appInfoPtr->m_sCacheFileDir + "cache/" + sFileName;
  197. /* m_nMaxTestSecord =*/ g_audioPalyerPtr->playAudio(sAudioFile);
  198. m_pTimer->start();
  199. m_nPlayedCont++;
  200. m_nLeftPalyCount--;
  201. ui->label_ap_playTimes->setText(QString::fromLocal8Bit(R"(<html>
  202. <style>
  203. .span
  204. {
  205. font-size:%1px;
  206. font-family:"Microsoft YaHei";
  207. font-weight:400;
  208. }
  209. #nText
  210. {
  211. color:rgba(102,102,102,1);
  212. }
  213. #gText
  214. {
  215. color:rgba(19,187,138,1);
  216. }
  217. #rText
  218. {
  219. color:rgba(254,119,100,1);
  220. }
  221. </style>
  222. <body>
  223. <span id="nText">(请<span id="gText">点击播放</span>按钮听音作答。已播次数:<span id="rText">%2</span> ,剩余播放次数:<span id="rText">%3</span> )</span>
  224. </body>
  225. </html>)").arg((int)(g_appInfoPtr->m_fRate * 14)).arg(m_nPlayedCont).arg(m_nLeftPalyCount));
  226. g_appInfoPtr->m_jAudioPlayedCount[m_sAudioName.toStdString()] = m_nPlayedCont;
  227. ui->btn_ap_play->setStyleSheet("border-image:url(:/images/btn-etvt-stop.png);");
  228. emit playedCountChange();
  229. }
  230. else
  231. {
  232. ui->btn_ap_play->setStyleSheet("border-image:url(:/images/btn-etvt-stop.png);");
  233. g_audioPalyerPtr->play();
  234. m_pTimer->start();
  235. }
  236. }
  237. }
  238. void audioPlay::getPalyedCount(QString &name, int &nCount)
  239. {
  240. name = m_sAudioName;
  241. nCount = m_nPlayedCont;
  242. }
  243. void audioPlay::timeOut()
  244. {
  245. m_nTestSecord++;
  246. int nSecord = m_nMaxTestSecord;
  247. QString sText = QString("%1:%2 / %3:%4").arg((int)(m_nTestSecord / 60), 2, 10, QChar('0'))
  248. .arg((int)(m_nTestSecord % 60), 2, 10, QChar('0'))
  249. .arg((int)(nSecord / 60), 2, 10, QChar('0'))
  250. .arg((int)(nSecord % 60), 2, 10, QChar('0'));
  251. ui->label_ap_time->setText(sText);
  252. if(m_nTestSecord == m_nMaxTestSecord)
  253. {
  254. m_pTimer->stop();
  255. g_audioPalyerPtr->stopPlay();
  256. ui->btn_ap_play->setStyleSheet("border-image:url(:/images/btn-audio-play.png);");
  257. if (m_nLeftPalyCount == 0)
  258. {
  259. ui->btn_ap_play->setStyleSheet("border-image:url(:/images/btn-play_disable.png);");
  260. }
  261. }
  262. else
  263. {
  264. std::string sDeciceName = "";
  265. if(!g_audioPalyerPtr->getDeviceinfo(sDeciceName))
  266. {
  267. m_pTimer->stop();
  268. QString sErrorStr = g_audioPalyerPtr->getErrMsg();
  269. ShowMsg(sErrorStr, this, MSG_ICON_TYPE::mit_error);
  270. }
  271. if(sDeciceName != m_sRecordDevice)
  272. {
  273. ShowMsg(QString::fromLocal8Bit("播放设备拔出,请检查设备!"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
  274. // myDebug()<<QString::fromLocal8Bit("播放设备拔出,请检查设备!");
  275. }
  276. }
  277. }
  278. int audioPlay::widgetType()
  279. {
  280. return wt_aduioPlay;
  281. }