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