void Player::dProgress(qint64 current, qint64 total) { cLenght = total; int percent = 0; int playPercent = 0; if(total > 0 && current > 0) { percent = (current * 100 ) / total; int tPercent = (duration * 100 ) / 150; if(tPercent > 100) { playPercent = 2; } else { if(tPercent+40 < 100) playPercent = 102 - (tPercent+40); else playPercent = 2; } } if(percent > playPercent && !canPlay) { buffer->setBuffer(ba); buffer->open(QBuffer::ReadWrite); setState(Player::STOPED); mediaObject->stop(); mediaObject->setCurrentSource(buffer); mediaObject->play(); setState(Player::PLAY); canPlay = true; } emit bufferProgress(current, total); mediaObject->state(); if(curState == Player::PLAY) { if(mediaObject->state() == 5) { mediaObject->setCurrentSource(buffer); mediaObject->play(); } int tTotal = mediaObject->totalTime(); int tCurrent = mediaObject->currentTime(); if(tTotal > 1 && tCurrent > 1) { int tPercent = (tCurrent * 100 ) / tTotal; if((percent - tPercent) < 5) { mediaObject->pause(); } else { if(mediaObject->state() != 2) { mediaObject->play(); } } } } if(current == total) { saveTrack(); } }
void DownloadItem::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) { // qDebug() << bytesReceived << bytesTotal << m_downloadTime.elapsed(); if (m_lastProgressTime.elapsed() < 150) return; m_lastProgressTime.start(); m_bytesReceived = bytesReceived; if (m_status != Downloading) { int neededBytes = (int) (bytesTotal * .005); int bufferSize = initialBufferSize(); if (bufferSize > bytesTotal) bufferSize = bytesTotal; // qDebug() << bytesReceived << bytesTotal << neededBytes << bufferSize << m_downloadTime.elapsed(); if (bytesReceived > bufferSize && bytesReceived > neededBytes && m_downloadTime.elapsed() > 2000) { emit bufferProgress(100); m_status = Downloading; emit statusChanged(); } else { int bytes = qMax(bufferSize, neededBytes); int bufferPercent = 0; if (bytes > 0) bufferPercent = bytesReceived * 100 / bytes; emit bufferProgress(bufferPercent); } } else { if (bytesTotal > 0) { int percent = bytesReceived * 100 / bytesTotal; if (percent != this->percent) { this->percent = percent; emit progress(percent); } } } }
void Player::playIndex(int index, int listIndex) { qDebug() << "PlayIndex,index: " << index << "listIndex: " << listIndex; if(listIndex > -1) { if(currentListIndex != listIndex || !listSync) { setList(parser->sources[listIndex]); currentListIndex = listIndex; } } if(index > playList.size() - 1) index = 0; prevIndex = curIndex; curIndex = index; mediaObject->stop(); setState(Player::STOPED); buffer->close(); buffer->setData(""); buffer = new QBuffer(mediaObject); QFile file; QDir::setCurrent(savePath); QString fileName = playList[curIndex]["artist"]+" - "+playList[curIndex]["title"]+"__"+playList[curIndex]["opt4"]+".mp3"; fileName.replace(QRegExp("[?*/\"<>]"), "_"); file.setFileName(fileName); if(file.open(QIODevice::ReadOnly)) { emit bufferProgress(1, 1); //buffer->setData(file.readAll()); //buffer->open(QBuffer::ReadWrite); if(obCreated) { nReply->reset(); if(nReply->isRunning()) nReply->abort(); } mediaObject->stop(); mediaObject->setCurrentSource(Phonon::MediaSource(fileName)); mediaObject->play(); setState(Player::PLAY); ob2Created = true; } else { if(bufferOff) { emit bufferProgress(-1, -1); mediaObject->stop(); mediaObject->setCurrentSource(Phonon::MediaSource(playList[curIndex]["link"])); mediaObject->play(); setState(Player::PLAY); } else { blockNum = 0; QNetworkRequest req; req.setUrl(QUrl(playList[curIndex]["link"])); if(obCreated) { if(nReply->isRunning()) nReply->abort(); } canPlay = false; nReply = nManager.get(req); connect(nReply, SIGNAL(readyRead()), this, SLOT(readData())); connect(nReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(dProgress(qint64,qint64))); connect(nReply, SIGNAL(finished()), this, SLOT(rFinished())); connect(nReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(nError(QNetworkReply::NetworkError))); obCreated = true; } } file.close(); emit curSong(playList[curIndex], curIndex, autoNext); autoNext = false; QRegExp rx("(.*):(.*)"); // Сиськиии!) rx.indexIn(playList[curIndex]["duration"]); QString c1 = rx.capturedTexts()[1]; QString c2 = rx.capturedTexts()[2]; int dur1 = c1.toInt(); int dur2 = c2.toInt(); duration = dur1 * 60; duration += dur2; }