TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musiccoremplayer.cpp
Go to the documentation of this file.
1 #include "musiccoremplayer.h"
2 #include "musicabstractnetwork.h"
3 #include "miniprocess.h"
4 
5 #include <QProcess>
6 
8  : QObject(parent),
9  m_process(nullptr),
10  m_playState(TTK::PlayState::Stopped),
11  m_category(Module::Null)
12 {
13  m_timer.setInterval(TTK_DN_S2MS);
14  connect(&m_timer, SIGNAL(timeout()), SLOT(timeout()));
15 
16  m_checkTimer.setInterval(10 * TTK_DN_S2MS);
17  connect(&m_checkTimer, SIGNAL(timeout()), SLOT(checkTimerout()));
18 }
19 
21 {
22  closeModule();
23 }
24 
25 void MusicCoreMPlayer::setMedia(Module type, const QString &data, int winId)
26 {
27  closeModule();
28  if(!QFile::exists(MAKE_PLAYER_PATH_FULL))
29  {
30  TTK_ERROR_STREAM("Lack of plugin file");
31  return;
32  }
33 
34  m_category = type;
36  m_process = new QProcess(this);
37  connect(m_process, SIGNAL(finished(int)), SIGNAL(finished(int)));
38 
39  QString url = data;
40  if(url.startsWith(HTTPS_PROTOCOL))
41  {
42  url.replace(HTTPS_PROTOCOL, HTTP_PROTOCOL);
43  }
44 
45  switch(m_category)
46  {
47  case Module::Radio: setRadioMedia(url); break;
48  case Module::Music: setMusicMedia(url); break;
49  case Module::Video: setVideoMedia(url, winId); break;
50  case Module::Null: break;
51  default: break;
52  }
53 }
54 
56 {
57  m_timer.stop();
58  m_checkTimer.stop();
59 
60  if(m_process)
61  {
62  m_process->kill();
63  delete m_process;
64  m_process = nullptr;
66  }
67 }
68 
69 void MusicCoreMPlayer::setRadioMedia(const QString &data)
70 {
71  Q_EMIT mediaChanged(data);
72 
73  QStringList arguments;
74  arguments << "-softvol" << "-slave" << "-quiet" << "-vo" << "directx:noaccel" << data;
75  connect(m_process, SIGNAL(readyReadStandardOutput()), SLOT(dataRecieve()));
76  m_process->start(MAKE_PLAYER_PATH_FULL, arguments);
77 }
78 
79 void MusicCoreMPlayer::setMusicMedia(const QString &data)
80 {
81  Q_EMIT mediaChanged(data);
82 
83  QStringList arguments;
84  arguments << "-cache" << "5000" << "-softvol" << "-slave" << "-quiet" << "-vo" << "directx:noaccel" << data;
85  connect(m_process, SIGNAL(readyReadStandardOutput()), SLOT(dataRecieve()));
86  m_process->start(MAKE_PLAYER_PATH_FULL, arguments);
87 }
88 
89 void MusicCoreMPlayer::setVideoMedia(const QString &data, int winId)
90 {
91  QStringList arguments;
92  arguments << "-cache" << "5000" << "-softvol" << "-slave" << "-quiet" << "-zoom" << "-wid" << QString::number(winId);
93 #ifdef Q_OS_WIN
94  arguments << "-vo" << "direct3d";
95 #else
96  arguments << "-vo" << "x11";
97 #endif
98  arguments << data;
99  Q_EMIT mediaChanged(data);
100 
101  m_process->setProcessChannelMode(QProcess::MergedChannels);
102  connect(m_process, SIGNAL(readyReadStandardOutput()), SLOT(durationRecieve()));
103  m_process->start(MAKE_PLAYER_PATH_FULL, arguments);
104 }
105 
107 {
108  if(!m_process)
109  {
110  return;
111  }
112 
113  m_process->write(QString("seek %1 2\n").arg(pos).toUtf8());
114 }
115 
117 {
118  if(!m_process)
119  {
120  return;
121  }
122 
123  m_process->write(QString("af channels=1:1:1\n").toUtf8());
124 }
125 
127 {
128  if(!m_process)
129  {
130  return;
131  }
132 
133  m_process->write(QString("af channels=1:1\n").toUtf8());
134 }
135 
137 {
138  if(!m_process)
139  {
140  return;
141  }
142 
143  m_process->write(QString("switch_audio %1\n").arg(number).toUtf8());
144 }
145 
147 {
148  if(!m_process)
149  {
150  return;
151  }
152 
153  m_process->write(QString("mute %1\n").arg(mute ? 1 : 0).toUtf8());
154 }
155 
157 {
158  if(!m_process)
159  {
160  return;
161  }
162 
163  m_process->write(QString("volume %1 1\n").arg(value).toUtf8());
164 }
165 
167 {
169 }
170 
172 {
173  m_timer.stop();
174  m_checkTimer.start();
175 
176  if(!m_process)
177  {
178  return;
179  }
180 
181  m_process->write("pause\n");
183  {
185  connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(positionRecieve()));
186  m_process->write("get_time_pos\n");
187  }
188  else
189  {
191  disconnect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(positionRecieve()));
192  }
193 }
194 
196 {
198  m_timer.stop();
199  m_checkTimer.stop();
200 
201  if(!m_process)
202  {
203  return;
204  }
205 
206  m_process->write("quit\n");
207 }
208 
210 {
211  while(m_process->canReadLine())
212  {
213  QString message(m_process->readLine());
214  if(message.startsWith("ANS_LENGTH"))
215  {
216  message.remove("\r\n");
217  disconnect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(durationRecieve()));
218  Q_EMIT durationChanged(message.mid(11).toFloat());
219  return;
220  }
221  }
222 }
223 
225 {
226  switch(m_category)
227  {
228  case Module::Radio: positionRecieve(); break;
229  case Module::Music: standardRecieve(); break;
230  case Module::Video: positionRecieve(); break;
231  case Module::Null: break;
232  default: break;
233  }
234 }
235 
237 {
238  m_timer.start();
239  while(m_process->canReadLine())
240  {
241  QString message(m_process->readLine());
242  if(message.startsWith("ANS_TIME_POSITION"))
243  {
244  message.remove("\r\n");
245  Q_EMIT positionChanged(message.mid(18).toFloat());
246  }
247  }
248 }
249 
251 {
252  while(m_process->canReadLine())
253  {
254  QString message(m_process->readLine());
255  if(message.startsWith("ANS_LENGTH"))
256  {
257  message.remove("\r\n");
258  Q_EMIT durationChanged(message.mid(11).toFloat());
259  }
260 
261  if(message.startsWith("ANS_TIME_POSITION"))
262  {
263  message.remove("\r\n");
264  Q_EMIT positionChanged(message.mid(18).toFloat());
265  }
266  }
267 }
268 
270 {
271  m_process->write("get_time_length\n");
272  m_process->write("get_time_pos\n");
273 }
274 
276 {
277  if(m_process && m_process->state() == QProcess::NotRunning)
278  {
279  m_checkTimer.stop();
280  Q_EMIT finished(TTK_LOW_LEVEL);
281  }
282 }
void mediaChanged(const QString &data)
bool isPlaying() const
PlayState
Definition: musicobject.h:164
#define MAKE_PLAYER_PATH_FULL
Definition: musicobject.h:149
#define TTK_LOW_LEVEL
Definition: ttkglobal.h:253
#define HTTP_PROTOCOL
Definition: ttkglobal.h:208
void setMute(bool mute)
void setVideoMedia(const QString &data, int winId)
void positionChanged(qint64 position)
void finished(int code)
void setMultiVoice(int number)
void setVolume(int value)
#define MAKE_PLAYER_NAME
Definition: musicobject.h:99
#define HTTPS_PROTOCOL
Definition: ttkglobal.h:209
TTK::PlayState m_playState
void durationChanged(qint64 duration)
The namespace of the process utils.
Definition: ttkcompat.h:24
void setRadioMedia(const QString &data)
#define TTK_DN_S2MS
Definition: ttkglobal.h:276
void setMusicMedia(const QString &data)
TTK_MODULE_EXPORT void killProcessByName(const QString &process)
void setMedia(Module type, const QString &data, int winId=-1)
void setPosition(qint64 pos)
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:69
MusicCoreMPlayer(QObject *parent=nullptr)