etVoiceTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #include "etVoiceTest.h"
  2. #include "ui_etVoiceTest.h"
  3. #include "CAppInfo.h"
  4. #include "CAudioPlayerProc.h"
  5. #include "awMsgBox.h"
  6. #include <QScrollBar>
  7. #include <QHBoxLayout>
  8. #include <QLabel>
  9. #include <QFile>
  10. #include <QFileInfo>
  11. #include <QSettings>
  12. #include <QFileDialog>
  13. etVoiceTest::etVoiceTest(QWidget *parent) :
  14. QWidget(parent),
  15. ui(new Ui::etVoiceTest)
  16. {
  17. ui->setupUi(this);
  18. setStyleSheet(g_appInfoPtr->m_sQssStr);
  19. m_nVoiceStatus = 2;
  20. m_nAudioSecord = 0;
  21. qRegisterMetaType<CDownLoadFileInfo>("CDownLoadFileInfo");
  22. connect(g_httpBllPtr.get(), &CHttpBll::sgnDownLoadFile, this, &etVoiceTest::onDownLoadFile);
  23. QString sFileName = sAudioUrl.right(sAudioUrl.length() - sAudioUrl.lastIndexOf("/") - 1);
  24. sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
  25. m_sDownLoadAudioFile = sFileName;
  26. CHttpRequestPackage hrp;
  27. hrp.sUri = sAudioUrl;
  28. hrp.sCommonStr = sFileName;
  29. hrp.nRequestType = RequestType::rtDownLoadFile;
  30. hrp.nRetryCount = 3;
  31. g_httpBllPtr->downLoad(hrp);
  32. QFileInfo file("coe.cfgi");
  33. QString sFilePath = file.absoluteFilePath();
  34. QSettings set(sFilePath, QSettings::IniFormat);
  35. m_nPlayVol = set.value("config/playVol", 100).toInt();
  36. ui->hslider_etvt_volume->setValue(m_nPlayVol);
  37. if (g_audioPalyerPtr == nullptr)
  38. {
  39. g_audioPalyerPtr = std::make_shared<CAudioPlayerProc>();
  40. }
  41. m_pTimer = std::make_shared<QTimer>();
  42. m_pTimer->setInterval(1000);
  43. connect(m_pTimer.get(), &QTimer::timeout, this, [&]() {
  44. ui->hslider_etvt_voice->setValue(ui->hslider_etvt_voice->value() + 1);
  45. ui->label_etvt_time->setText(QString("%1:%2 / %3:%4").arg((int)(ui->hslider_etvt_voice->value() / 60), 2, 10, QChar('0'))
  46. .arg((int)(ui->hslider_etvt_voice->value() % 60), 2, 10, QChar('0'))
  47. .arg((int)(m_nAudioSecord / 60), 2, 10, QChar('0'))
  48. .arg((int)(m_nAudioSecord % 60), 2, 10, QChar('0')));
  49. if (ui->hslider_etvt_voice->value() >= m_nAudioSecord)
  50. {
  51. ui->btn_etvt_play->setStyleSheet("border-image:url(:/images/btn-etvt-play.png);");
  52. m_pTimer->stop();
  53. g_audioPalyerPtr->stopPlay();
  54. }
  55. else
  56. {
  57. std::string sDeciceName = "";
  58. if (!g_audioPalyerPtr->getDeviceinfo(sDeciceName))
  59. {
  60. m_pTimer->stop();
  61. QString sErrorStr = g_audioPalyerPtr->getErrMsg();
  62. ShowMsg(sErrorStr, this, MSG_ICON_TYPE::mit_error);
  63. }
  64. if (sDeciceName != m_sRecordDevice)
  65. {
  66. ShowMsg(QString::fromLocal8Bit("播放设备拔出,请检查设备!"), this, MSG_ICON_TYPE::mit_error);
  67. // myDebug()<<QString::fromLocal8Bit("播放设备拔出,请检查设备!");
  68. }
  69. }
  70. });
  71. }
  72. etVoiceTest::~etVoiceTest()
  73. {
  74. awMsgBox::clear(this);
  75. delete ui;
  76. }
  77. int etVoiceTest::setUI(const int nLeft, const int nTop, const int nWidth)
  78. {
  79. setGeometry(nLeft, nTop, nWidth, g_appInfoPtr->m_fRate*247);
  80. ui->widget_etvt_voice->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30,
  81. g_appInfoPtr->m_fRate*423, g_appInfoPtr->m_fRate*40);
  82. ui->btn_etvt_play->setGeometry(g_appInfoPtr->m_fRate*8, (ui->widget_etvt_voice->height() - g_appInfoPtr->m_fRate*24)/2,
  83. g_appInfoPtr->m_fRate*24, g_appInfoPtr->m_fRate*24);
  84. ui->label_etvt_time->adjustSize();
  85. ui->label_etvt_time->setGeometry(ui->btn_etvt_play->x() + ui->btn_etvt_play->width() + g_appInfoPtr->m_fRate * 10,
  86. (ui->widget_etvt_voice->height() - ui->label_etvt_time->height()) / 2,
  87. ui->label_etvt_time->width(), ui->label_etvt_time->height());
  88. ui->hslider_etvt_voice->setGeometry(ui->label_etvt_time->x() + ui->label_etvt_time->width() + g_appInfoPtr->m_fRate*10,
  89. (ui->widget_etvt_voice->height() - g_appInfoPtr->m_fRate*12)/2,
  90. g_appInfoPtr->m_fRate*150, g_appInfoPtr->m_fRate*16);
  91. ui->btn_etvt_volume->setGeometry(ui->hslider_etvt_voice->x() + ui->hslider_etvt_voice->width() + g_appInfoPtr->m_fRate*20,
  92. ui->btn_etvt_play->y(), ui->btn_etvt_play->width(), ui->btn_etvt_play->height());
  93. ui->hslider_etvt_volume->setGeometry(ui->btn_etvt_volume->x() + ui->btn_etvt_volume->width() + g_appInfoPtr->m_fRate * 10, ui->hslider_etvt_voice->y(),
  94. g_appInfoPtr->m_fRate*50, ui->hslider_etvt_voice->height());
  95. ui->btn_etvt_download->setGeometry(ui->hslider_etvt_volume->x() + ui->hslider_etvt_volume->width() + g_appInfoPtr->m_fRate*10,
  96. ui->btn_etvt_play->y(), ui->btn_etvt_play->width(), ui->btn_etvt_play->height());
  97. ui->btn_etvt_canotPlay->setGeometry(ui->widget_etvt_voice->x() + ui->widget_etvt_voice->width() + g_appInfoPtr->m_fRate*20,
  98. ui->widget_etvt_voice->y(), g_appInfoPtr->m_fRate*141, g_appInfoPtr->m_fRate*40);
  99. ui->btn_etvt_canotPlay->setIconSize(QSize(g_appInfoPtr->m_fRate*13, g_appInfoPtr->m_fRate*13));
  100. ui->btn_etvt_canotPlay->setIcon(QIcon(":/images/btn-can-not-play"));
  101. ui->btn_etvt_canPlay->setGeometry(ui->btn_etvt_canotPlay->x() + ui->btn_etvt_canotPlay->width() + g_appInfoPtr->m_fRate*10,
  102. ui->btn_etvt_canotPlay->y(), g_appInfoPtr->m_fRate*146, ui->btn_etvt_canotPlay->height());
  103. ui->btn_etvt_canPlay->setIconSize(QSize(g_appInfoPtr->m_fRate*18, g_appInfoPtr->m_fRate*12));
  104. ui->btn_etvt_canPlay->setIcon(QIcon(":/images/btn_can-play.png"));
  105. ui->label_HLine->setGeometry(ui->widget_etvt_voice->x(), ui->widget_etvt_voice->y() + ui->widget_etvt_voice->height() + g_appInfoPtr->m_fRate*30,
  106. width() - g_appInfoPtr->m_fRate*30*2, g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1);
  107. ui->tablewt_envTest->setGeometry(ui->widget_etvt_voice->x(), ui->label_HLine->y() + ui->label_HLine->height(),
  108. width() - g_appInfoPtr->m_fRate*30*2, height() - ui->label_HLine->y() - ui->label_HLine->height());
  109. ui->tablewt_envTest->setEditTriggers(QAbstractItemView::NoEditTriggers);
  110. //整行选中的方式
  111. ui->tablewt_envTest->setSelectionBehavior(QAbstractItemView::SelectRows);
  112. //设置为只能选中一行
  113. ui->tablewt_envTest->setSelectionMode(QAbstractItemView::SingleSelection);
  114. //隐藏列表头
  115. ui->tablewt_envTest->verticalHeader()->setVisible(false);
  116. //隐藏边框
  117. ui->tablewt_envTest->setShowGrid(false);
  118. //表头不高亮显示
  119. ui->tablewt_envTest->horizontalHeader()->setHighlightSections(false);
  120. //设置行数
  121. ui->tablewt_envTest->setRowCount(2);
  122. //设置列数
  123. ui->tablewt_envTest->setColumnCount(3);
  124. ui->tablewt_envTest->setHorizontalScrollMode(QTableWidget::ScrollPerPixel);
  125. ui->tablewt_envTest->horizontalScrollBar()->setSingleStep(g_appInfoPtr->m_fRate*5);
  126. QStringList sHeadStr;
  127. sHeadStr << QString::fromLocal8Bit("检测项") <<
  128. QString::fromLocal8Bit("值") <<
  129. QString::fromLocal8Bit("状态");
  130. ui->tablewt_envTest->setHorizontalHeaderLabels(sHeadStr);
  131. ui->tablewt_envTest->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  132. ui->tablewt_envTest->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
  133. ui->tablewt_envTest->verticalHeader()->setDefaultSectionSize(g_appInfoPtr->m_fRate*48);
  134. ui->tablewt_envTest->setColumnWidth(0, g_appInfoPtr->m_fRate*525);
  135. ui->tablewt_envTest->setColumnWidth(1, g_appInfoPtr->m_fRate*170);
  136. ui->tablewt_envTest->setColumnWidth(2, g_appInfoPtr->m_fRate*24);
  137. ui->tablewt_envTest->horizontalHeader()->setStretchLastSection(true);
  138. for(int i = 0; i < 2; i++)
  139. {
  140. ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("")));
  141. ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("")));
  142. /* QWidget *widget = new QWidget;
  143. QHBoxLayout *layout = new QHBoxLayout;
  144. widget->setLayout(layout);
  145. QLabel *icon = new QLabel;
  146. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
  147. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
  148. layout->addWidget(icon);
  149. layout->addStretch();
  150. ui->tablewt_envTest->setCellWidget(i, 2, widget);
  151. */
  152. }
  153. return height();
  154. }
  155. int etVoiceTest::widgetHeight()
  156. {
  157. return height();
  158. }
  159. void etVoiceTest::onDownLoadFile(CDownLoadFileInfo downLoadFileInfo)
  160. {
  161. //QString sFileName = sAudioUrl.right(sAudioUrl.length() - sAudioUrl.lastIndexOf("/") - 1);
  162. //sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
  163. if (downLoadFileInfo.sFileName == m_sDownLoadAudioFile)
  164. {
  165. if (downLoadFileInfo.nCode == 200)
  166. {
  167. initAudioDuration();
  168. ui->tablewt_envTest->setItem(0, 0, new QTableWidgetItem(QString::fromLocal8Bit("文件下载")));
  169. ui->tablewt_envTest->setItem(0, 1, new QTableWidgetItem(QString::fromLocal8Bit("正常")));
  170. QWidget *widget = new QWidget;
  171. QHBoxLayout *layout = new QHBoxLayout;
  172. widget->setLayout(layout);
  173. QLabel *icon = new QLabel;
  174. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
  175. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
  176. layout->addWidget(icon);
  177. layout->addStretch();
  178. ui->tablewt_envTest->setCellWidget(0, 2, widget);
  179. }
  180. else
  181. {
  182. ui->tablewt_envTest->setItem(0, 0, new QTableWidgetItem(QString::fromLocal8Bit("文件下载")));
  183. ui->tablewt_envTest->setItem(0, 1, new QTableWidgetItem(QString::fromLocal8Bit("请检查网络")));
  184. QWidget *widget = new QWidget;
  185. QHBoxLayout *layout = new QHBoxLayout;
  186. widget->setLayout(layout);
  187. QLabel *icon = new QLabel;
  188. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  189. icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  190. layout->addWidget(icon);
  191. layout->addStretch();
  192. ui->tablewt_envTest->setCellWidget(0, 2, widget);
  193. }
  194. }
  195. }
  196. void etVoiceTest::on_btn_etvt_canotPlay_clicked()
  197. {
  198. ui->tablewt_envTest->setItem(1, 0, new QTableWidgetItem(QString::fromLocal8Bit("声音播放")));
  199. ui->tablewt_envTest->setItem(1, 1, new QTableWidgetItem(QString::fromLocal8Bit("请检查设备")));
  200. QWidget *widget = new QWidget;
  201. QHBoxLayout *layout = new QHBoxLayout;
  202. widget->setLayout(layout);
  203. QLabel *icon = new QLabel;
  204. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  205. icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  206. layout->addWidget(icon);
  207. layout->addStretch();
  208. ui->tablewt_envTest->setCellWidget(1, 2, widget);
  209. m_nVoiceStatus = 0;
  210. }
  211. void etVoiceTest::on_btn_etvt_canPlay_clicked()
  212. {
  213. ui->tablewt_envTest->setItem(1, 0, new QTableWidgetItem(QString::fromLocal8Bit("声音播放")));
  214. ui->tablewt_envTest->setItem(1, 1, new QTableWidgetItem(QString::fromLocal8Bit("正常")));
  215. QWidget *widget = new QWidget;
  216. QHBoxLayout *layout = new QHBoxLayout;
  217. widget->setLayout(layout);
  218. QLabel *icon = new QLabel;
  219. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  220. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  221. layout->addWidget(icon);
  222. layout->addStretch();
  223. ui->tablewt_envTest->setCellWidget(1, 2, widget);
  224. m_nVoiceStatus = 1;
  225. }
  226. void etVoiceTest::initAudioDuration()
  227. {
  228. QString sFileName = sAudioUrl.right(sAudioUrl.length() - sAudioUrl.lastIndexOf("/") - 1);
  229. sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
  230. m_nAudioSecord = g_audioPalyerPtr->GetMediaDuration(sFileName);
  231. ui->label_etvt_time->setText(QString("%1:%2 / %3:%4").arg(0, 2, 10, QChar('0'))
  232. .arg(0, 2, 10, QChar('0'))
  233. .arg((int)(m_nAudioSecord / 60), 2, 10, QChar('0'))
  234. .arg((int)(m_nAudioSecord % 60), 2, 10, QChar('0')));
  235. ui->hslider_etvt_voice->setMaximum(m_nAudioSecord);
  236. ui->hslider_etvt_voice->setValue(0);
  237. }
  238. void etVoiceTest::on_btn_etvt_play_clicked()
  239. {
  240. if (m_pTimer->isActive())
  241. {
  242. m_pTimer->stop();
  243. g_audioPalyerPtr->pausePlay();
  244. ui->btn_etvt_play->setStyleSheet("border-image:url(:/images/btn-etvt-play.png);");
  245. }
  246. else
  247. {
  248. if (m_nAudioSecord == 0 || ui->hslider_etvt_voice->value() == 0
  249. || ui->hslider_etvt_voice->value() >= m_nAudioSecord)
  250. {
  251. QString sFileName = sAudioUrl.right(sAudioUrl.length() - sAudioUrl.lastIndexOf("/") - 1);
  252. sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
  253. if (QFile::exists(sFileName))
  254. {
  255. ui->btn_etvt_play->setStyleSheet("border-image:url(:/images/btn-etvt-stop.png);");
  256. initAudioDuration();
  257. g_audioPalyerPtr->setVol(m_nPlayVol);
  258. if (!g_audioPalyerPtr->getDeviceinfo(m_sRecordDevice))
  259. {
  260. m_pTimer->stop();
  261. QString sErrorStr = g_audioPalyerPtr->getErrMsg();
  262. ShowMsg(sErrorStr, this);
  263. }
  264. g_audioPalyerPtr->playAudio(sFileName);
  265. m_pTimer->start();
  266. }
  267. }
  268. else
  269. {
  270. ui->btn_etvt_play->setStyleSheet("border-image:url(:/images/btn-etvt-stop.png);");
  271. g_audioPalyerPtr->play();
  272. m_pTimer->start();
  273. }
  274. }
  275. }
  276. void etVoiceTest::on_btn_etvt_download_clicked()
  277. {
  278. QString sFileName = sAudioUrl.right(sAudioUrl.length() - sAudioUrl.lastIndexOf("/") - 1);
  279. sFileName = QFileDialog::getSaveFileName(
  280. this, sAudioUrl, QFileInfo(sFileName).fileName(),
  281. "All Files(*.*)");
  282. m_sDownLoadAudioFile = sFileName;
  283. CHttpRequestPackage hrp;
  284. hrp.sUri = sAudioUrl;
  285. hrp.sCommonStr = sFileName;
  286. hrp.nRequestType = RequestType::rtDownLoadFile;
  287. hrp.nRetryCount = 3;
  288. g_httpBllPtr->downLoad(hrp);
  289. }
  290. int etVoiceTest::getCheckStatus()
  291. {
  292. return m_nVoiceStatus;
  293. }
  294. void etVoiceTest::on_hslider_etvt_volume_valueChanged(int value)
  295. {
  296. QFileInfo file("coe.cfgi");
  297. QString sFilePath = file.absoluteFilePath();
  298. QSettings set(sFilePath, QSettings::IniFormat);
  299. m_nPlayVol = value;
  300. set.setValue("config/playVol", value);
  301. if (m_pTimer && m_pTimer->isActive())
  302. {
  303. g_audioPalyerPtr->setVol(m_nPlayVol);
  304. }
  305. }
  306. void etVoiceTest::on_btn_etvt_volume_clicked()
  307. {
  308. if(m_bMute)
  309. {
  310. ui->btn_etvt_volume->setStyleSheet("border-image:url(:/images/btn-etvt-volume.png);");
  311. ui->hslider_etvt_volume->setValue(m_nLastVol);
  312. }
  313. else
  314. {
  315. ui->btn_etvt_volume->setStyleSheet("border-image:url(:/images/btn-etvt-mute.png);");
  316. m_nLastVol = ui->hslider_etvt_volume->value();
  317. ui->hslider_etvt_volume->setValue(0);
  318. }
  319. m_bMute = !m_bMute;
  320. }