QT编程读取歌词
我的分数就这些了,全给了,只求能够解答我的疑惑,若解决,感激不尽!!!! 展开
先上图一张.
主要是使用了QPainter的叠加:
[cpp] view plaincopy
pTmp.setCompositionMode(QPainter::CompositionMode_Source);
pTmp.fillRect(QRect(0, 0, paintw, rtText.height()), QBrush(Qt::white));
pTmp.fillRect(QRect(0, 0, mPrecent, rtText.height()), QBrush(Qt::blue));
pTmp.setCompositionMode(QPainter::CompositionMode_DestinationIn);
pTmp.drawImage(0, 0, pmMask);
pTmp.setCompositionMode(QPainter::CompositionMode_DestinationOver);
主要函数:
void testLyrics::DrawLyric(QPainter &p, const QRect& rt)
{
p.setBrush(Qt::darkGray);
p.drawRect(rect());
const QString strLyric = mCurLyrics.lyric;
QFont m_ftLyric(QString::fromLocal8Bit("微软雅黑"), 24, QFont::Bold);
QPainter pTmp;
QFontMetrics fm(m_ftLyric);
QSize textSize = fm.size(Qt::TextSingleLine, strLyric);
QRect textRect(QPoint(0,0),textSize);
int paintw = textSize.width();
int playedW = textSize.width()*0.5;
QRect rtText = rect();
// pTmp.setRenderHints(QPainter::TextAntialiasing, true);
QImage pmMask(paintw, rtText.height(),QImage::Format_ARGB32);
pmMask.fill(Qt::transparent);
if (pTmp.begin(&pmMask))
{
pTmp.setFont(m_ftLyric);
pTmp.setPen(Qt::black);
pTmp.drawText(pmMask.rect(), strLyric);
}
pTmp.end();
QImage pmFG(paintw, rtText.height(),QImage::Format_ARGB32);
if (pTmp.begin(&pmFG))
{
pTmp.setCompositionMode(QPainter::CompositionMode_Source);
pTmp.fillRect(QRect(0, 0, paintw, rtText.height()), QBrush(Qt::white));
pTmp.fillRect(QRect(0, 0, mPrecent, rtText.height()), QBrush(Qt::blue));
pTmp.setCompositionMode(QPainter::CompositionMode_DestinationIn);
pTmp.drawImage(0, 0, pmMask);
pTmp.setCompositionMode(QPainter::CompositionMode_DestinationOver);
// shadow
pTmp.setFont(m_ftLyric);
pTmp.setPen(QColor(0,0,0,0x80));
pTmp.drawText(pmMask.rect(), strLyric);
}
pTmp.end();
p.drawImage(rtText.left(), rtText.top(), pmFG);
}
struct LyricLine
{
int time;
QString lyric;
}
class LyricHandler
{
.............
public:
QString getLyricAt( int currentTime ); //通过时间获取歌词
.........
public slots:
void parseLyricFile(const QString& fileName);//解析歌词
......
private:
QList<LyricLine*> m_lyricList;
};
当然要实现所以功能上面的接口是不够的,你自己可以按自己的要求添加。
然后当你在播放音乐的时候,可以根据当前的播放时间去查找对应的歌词,显示在界面上就OK了。
可以根据播放的时间,然后显示lrc文件中对应的歌词。