TTKMusicPlayer  4.1.3.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 &url, 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  switch(m_category)
40  {
41  case Module::Radio: setRadioMedia(url); break;
42  case Module::Music: setMusicMedia(url); break;
43  case Module::Video: setVideoMedia(url, winId); break;
44  case Module::Movie: setMovieMedia(url, winId); break;
45  case Module::Null: break;
46  default: break;
47  }
48 }
49 
51 {
52  m_timer.stop();
53  m_checkTimer.stop();
54 
55  if(m_process)
56  {
57  m_process->kill();
58  delete m_process;
59  m_process = nullptr;
61  }
62 }
63 
64 void MusicCoreMPlayer::setRadioMedia(const QString &url)
65 {
66  Q_EMIT mediaChanged(url);
67 
68  QStringList arguments;
69  arguments << "-softvol" << "-slave" << "-quiet" << "-vo" << "directx:noaccel" << url;
70  connect(m_process, SIGNAL(readyReadStandardOutput()), SLOT(dataRecieved()));
71  m_process->start(MAKE_PLAYER_PATH_FULL, arguments);
72 }
73 
74 void MusicCoreMPlayer::setMusicMedia(const QString &url)
75 {
76  Q_EMIT mediaChanged(url);
77 
78  QStringList arguments;
79  arguments << "-cache" << "5000" << "-softvol" << "-slave" << "-quiet" << "-vo" << "directx:noaccel" << url;
80  connect(m_process, SIGNAL(readyReadStandardOutput()), SLOT(dataRecieved()));
81  m_process->start(MAKE_PLAYER_PATH_FULL, arguments);
82 }
83 
84 void MusicCoreMPlayer::setVideoMedia(const QString &url, int winId)
85 {
86  Q_EMIT mediaChanged(url);
87 
88  QStringList arguments;
89  arguments << "-cache" << "5000" << "-softvol" << "-slave" << "-quiet" << "-zoom" << "-wid" << QString::number(winId);
90 #ifdef Q_OS_WIN
91  arguments << "-vo" << "direct3d";
92 #else
93  arguments << "-vo" << "x11";
94 #endif
95  arguments << url;
96 
97  m_process->setProcessChannelMode(QProcess::MergedChannels);
98  connect(m_process, SIGNAL(readyReadStandardOutput()), SLOT(durationRecieved()));
99  m_process->start(MAKE_PLAYER_PATH_FULL, arguments);
100 }
101 
102 void MusicCoreMPlayer::setMovieMedia(const QString &url, int winId)
103 {
104  Q_EMIT mediaChanged(url);
105 
106  QStringList arguments;
107  arguments << "-cache" << "5000" << "-softvol" << "-slave" << "-quiet" << "-zoom" << "-loop" << 0 << "-wid" << QString::number(winId);
108 #ifdef Q_OS_WIN
109  arguments << "-vo" << "direct3d";
110 #else
111  arguments << "-vo" << "x11";
112 #endif
113  arguments << url;
114 
115  m_process->setProcessChannelMode(QProcess::MergedChannels);
116  connect(m_process, SIGNAL(readyReadStandardOutput()), SLOT(durationRecieved()));
117  m_process->start(MAKE_PLAYER_PATH_FULL, arguments);
118 }
119 
121 {
122  if(!m_process)
123  {
124  return;
125  }
126 
127  m_process->write(QString("seek %1 2\n").arg(pos).toUtf8());
128 }
129 
131 {
132  if(!m_process)
133  {
134  return;
135  }
136 
137  m_process->write(QString("mute %1\n").arg(mute ? 1 : 0).toUtf8());
138 }
139 
141 {
142  if(!m_process)
143  {
144  return;
145  }
146 
147  m_process->write(QString("volume %1 1\n").arg(value).toUtf8());
148 }
149 
151 {
153 }
154 
156 {
157  m_timer.stop();
158  m_checkTimer.start();
159 
160  if(!m_process)
161  {
162  return;
163  }
164 
165  m_process->write("pause\n");
167  {
169  connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(positionRecieved()));
170  m_process->write("get_time_pos\n");
171  }
172  else
173  {
175  disconnect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(positionRecieved()));
176  }
177 }
178 
180 {
182  m_timer.stop();
183  m_checkTimer.stop();
184 
185  if(!m_process)
186  {
187  return;
188  }
189 
190  m_process->write("quit\n");
191 }
192 
194 {
195  while(m_process->canReadLine())
196  {
197  QString message(m_process->readLine());
198  if(message.startsWith("ANS_LENGTH"))
199  {
200  message.remove(TTK_WLINEFEED);
201  disconnect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(durationRecieved()));
202  Q_EMIT durationChanged(message.mid(11).toFloat());
203  return;
204  }
205  }
206 }
207 
209 {
210  switch(m_category)
211  {
212  case Module::Radio: positionRecieved(); break;
213  case Module::Music: standardRecieved(); break;
214  case Module::Video: positionRecieved(); break;
215  case Module::Movie: positionRecieved(); break;
216  case Module::Null: break;
217  default: break;
218  }
219 }
220 
222 {
223  m_timer.start();
224  while(m_process->canReadLine())
225  {
226  QString message(m_process->readLine());
227  if(message.startsWith("ANS_TIME_POSITION"))
228  {
229  message.remove(TTK_WLINEFEED);
230  Q_EMIT positionChanged(message.mid(18).toFloat());
231  }
232  }
233 }
234 
236 {
237  while(m_process->canReadLine())
238  {
239  QString message(m_process->readLine());
240  if(message.startsWith("ANS_LENGTH"))
241  {
242  message.remove(TTK_WLINEFEED);
243  Q_EMIT durationChanged(message.mid(11).toFloat());
244  }
245 
246  if(message.startsWith("ANS_TIME_POSITION"))
247  {
248  message.remove(TTK_WLINEFEED);
249  Q_EMIT positionChanged(message.mid(18).toFloat());
250  }
251  }
252 }
253 
255 {
256  m_process->write("get_time_length\n");
257  m_process->write("get_time_pos\n");
258 }
259 
261 {
262  if(m_process && m_process->state() == QProcess::NotRunning)
263  {
264  m_checkTimer.stop();
265  Q_EMIT finished(TTK_LOW_LEVEL);
266  }
267 }
bool isPlaying() const
PlayState
Definition: musicobject.h:166
void setMedia(Module type, const QString &url, int winId=-1)
#define MAKE_PLAYER_PATH_FULL
Definition: musicobject.h:151
#define TTK_LOW_LEVEL
Definition: ttkglobal.h:258
void positionChanged(qint64 position)
void setRadioMedia(const QString &url)
void setVideoMedia(const QString &url, int winId)
void finished(int code)
void setVolume(int value)
#define MAKE_PLAYER_NAME
Definition: musicobject.h:101
TTK::PlayState m_playState
void durationChanged(qint64 duration)
The namespace of the process utils.
Definition: ttkcompat.h:24
void setMovieMedia(const QString &url, int winId)
void setMusicMedia(const QString &url)
void mediaChanged(const QString &url)
void setMuted(bool mute)
#define TTK_DN_S2MS
Definition: ttkglobal.h:281
#define TTK_WLINEFEED
Definition: ttkglobal.h:199
TTK_MODULE_EXPORT void killProcessByName(const QString &process)
void setPosition(qint64 pos)
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:70
MusicCoreMPlayer(QObject *parent=nullptr)