Function introduction:
MP3 player
Conception process: meets basic playback requirements (playing/pausing, fast forward, and fast return), displays with lyrics, tags, tracks, and Chinese
Tools used (platform: Windows XP SP3 ):
Programming Language: C ++
Icon creation: Photo Shop
Audio File tag Library: mediainfo (third-party Library)
Tag encoding conversion: sqlite3 database and sqlite3 C ++ Library (the corresponding Unicode and GBK encoding have been written into the sqlite3 database using C ++ in advance. If this process is required, you can call the query table directly)
File directory:
Program running interface:
Initial status
Add multiple file statuses
Horizontal list placement
Main Code
/** Function: audio player * kakasi ([email protected]) * Time: 2014-6-1 * version: V1.0 **/# include "musicplayer. H "# include <qhboxlayout> # include <qvboxlayout> # include <qgridlayout> # include <qpixmap> # include <qbitmap> # include <qpainter> # include <qicon> # include <qfile> # include <qtextstream> # include <qpushbutton> # include <qmediametadata> # include <qdebug> # include <qlibrary> # include <stdio. h> # include <qfileinfo> # include <Qfiledialog ># include <qlocale> # include <iostream> # include <qbytearray> # include <string. h> # include <stdlib. h> # include <locale. h> # include <bitset> # include <iomanip> # include <qtextcodec> # include "qmediainfo. H "using namespace STD; # define cout qdebug () musicplayer: musicplayer (qwidget * parent): qwidget (parent) {// some information init this-> setwindowflags (QT :: FRAM Elesswindowhint | QT: windowstaysontophint); ismousedown = false; qpushbutton * listbtn = new qpushbutton ("L"); listbtn-> setmaximumsize (20, 20); Connect (listbtn, signal (clicked (), slot (showlist (); listbtn-> setstylesheet ("Background: Gray"); qpushbutton * loadbtn = new qpushbutton ("O "); loadbtn-> setmaximumsize (20, 20); Connect (loadbtn, signal (clicked (), slot (loadmusics (); loadbtn-> setstylesheet ("B Ackground: Gray "); qpushbutton * minbtn = new qpushbutton ("-"); minbtn-> setmaximumsize (20, 20); Connect (minbtn, signal (clicked ()), slot (setmin (); minbtn-> setstylesheet ("Background: Gray"); qpushbutton * exitbtn = new qpushbutton ("X"); exitbtn-> setmaximumsize (30,20 ); connect (exitbtn, signal (clicked (), slot (close (); exitbtn-> setstylesheet ("Background: Gray; text-align: Top "); qhboxlayout * titlelayout = New response; titlelayout-> addstretch (); titlelayout-> addwidget (listbtn, 0, QT: aligntop); response-> addwidget (loadbtn, 0, QT: aligntop ); titlelayout-> addwidget (minbtn, 0, QT: aligntop); titlelayout-> addwidget (exitbtn, 0, QT: aligntop); titlelayout-> setspacing (0 ); titlelayout-> setcontentsmargins (0, 0, 0); // layout musicnamelabel = new mylabel (); musicnamelabel-> setminimumwidth (200); musicnamelab El-> settext ("Take Me Away"); musicnamelabel-> setstylesheet ("font-size: 23px; font-family:; color: White "); authorlabel = new mylabel (); authorlabel-> settext ("avril"); authorlabel-> setstylesheet ("font-size: 14px; font-family:; color: white "); timelabel = new mylabel (); totaltime = 302; timelabel-> settext (TR (" T: <span style = 'color: coral'> % 1 </span>/% 2 "). arg (gettimestr (0 )). arg (gettimestr (totaltime) ); Timelabel-> setstylesheet ("font-size: 14px; font-family:; color: White"); vollabel = new mylabel; vollabel-> settext (TR ("V: % 1 "). arg ("30%"); vollabel-> setstylesheet ("font-size: 14px; font-family:; color: White"); qhboxlayout * timeandvollabel = new qhboxlayout; timeandvollabel-> addwidget (vollabel); timeandvollabel-> addstretch (); timeandvollabel-> addwidget (timelabel); prevlabel = new mylabel; playo Rpauselabel = new mylabel; nextlabel = new mylabel; Connect (playorpauselabel, signal (clicked (), slot (playorpause (); Connect (nextlabel, signal (clicked ()), slot (next (); Connect (prevlabel, signal (clicked (), slot (prev (); qpixmap img1 ("images/prev3.png "); qpixmap img2 ("images/play.png"); qpixmap img3 ("images/next3.png"); prevlabel-> setpixmap (img1.scaled (40,40); playorpauselabel-> setpixmap (img2. Scaled (60, 60); nextlabel-> setpixmap (img3.scaled (40, 40); prevlabel-> resize (40, 40); playorpauselabel-> resize (60, 60 ); nextlabel-> resize (40, 40); prevlabel-> setstylesheet ("border-style: flat;"); playorpauselabel-> setstylesheet ("border-style: flat "); nextlabel-> setstylesheet ("border-style: flat"); qstring smallstylestr = "width: 40px; Height: 40px;"; qstring bigstylestr = "width: 60px; Height: 60px; "; Pre Vlabel-> setstylesheet (smallstylestr); Short-> setstylesheet (bigstylestr); nextlabel-> setstylesheet (sheet); piclabel = new mylabel (); piclabel-> resize (85,85 ); piclabel-> setstylesheet ("margin-left: 6px; margin-Right: 10px;"); qpixmap PIC ("images/logo.png"); piclabel-> setpixmap (PIC ); qhboxlayout * piclayout = new qhboxlayout; piclayout-> addwidget (piclabel); space = new mylabel; space-> S Ettext ("<HR>"); currentlrc = new mylabel ("<center> </center>"); currentlrc-> setstylesheet ("font-size: 25px; font-family:; color: deepskyblue; "); nextlrc = new mylabel (" <center> </center> "); nextlrc-> setstylesheet (" font-size: 15px; font-family: ssslider; color: lightgrey; "); progressslider = new qslider (QT: horizontal); progressslider-> setrange (0, totaltime); Connect (progressslider, signal (slidermoved (INT )), Slot (setposition (INT); Connect (progressslider, signal (sliderreleased (), slot (setposition (); volslider = new qslider (QT: horizontal ); volslider-> setrange (0,100); volslider-> setvalue (30); Connect (volslider, signal (valuechanged (INT), slot (changevol (INT ))); qfile file ("qss/style1.qss"); file. open (qfile: readonly); volslider-> setstylesheet (file. readall (); file. close (); file. setfilename ("qss/styl E2.qss "); file. open (qfile: readonly); progressslider-> setstylesheet (file. readall (); progressslider-> setminimumwidth (240); qhboxlayout * slider = new qhboxlayout; slider-> addwidget (volslider); slider-> addstretch (); slider-> addwidget (progressslider); Parameters * midlayout = new layout (); midlayout-> addwidget (musicnamelabel); midlayout-> addwidget (authorlabel); midlayout-> addlayout El); qhboxlayout * operationlayout = new qhboxlayout (); operationlayout-> addwidget (prevlabel, 0, QT: alignright); operationlayout-> addwidget (values, 0, QT :: alignright); operationlayout-> addwidget (nextlabel, 0, QT: alignright); qhboxlayout * toplayout = new qhboxlayout; toplayout-> addlayout (midlayout ); toplayout-> addlayout (operationlayout); qvboxlayout * rightlayout = new qvboxlayout; rightla Yout-> addlayout (toplayout); rightlayout-> addlayout (slider); qvboxlayout * lrclayout = new layout; lrclayout-> addwidget (currentlrc); lrclayout-> addwidget (nextlrc ); optional * mainlayout1 = new qhboxlayout (); mainlayout1-> addlayout (piclayout); mainlayout1-> addlayout (rightlayout); qvboxlayout * mainlayout2 = new qvboxlayout (); mainlayout2-> addlayout (titlelayout); mainlayout2-> addlayout (mainlayou T1); mainlayout2-> addwidget (Space); mainlayout2-> addlayout (lrclayout); mainlayout2-> setspacing (0); mainlayout2-> setcontentsmargins (5, 0, 15, 15 ); this-> setlayout (mainlayout2); this-> resize (this-> sizehint (); // important for setrounded (bool) setattribute (QT: wa_translucentbackground, true ); setrounded (true); isplaying = false; listui = 0; List = new qstringlist; playerlist = new qmediaplaylist; player List-> setcurrentindex (0); playerlist-> setplaybackmode (qmediaplaylist: loop); player = new qmediaplayer; player-> setplaylist (playerlist); Connect (player, signal (positionchanged (qint64), slot (positionchange (qint64); Connect (player, signal (durationchanged (qint64), slot (durationchange (qint64 ))); volslider-> setvalue (player-> volume (); createactions (); LRC = new qmap <int, qstring>; updateinfo ();} void m Usicplayer: closeevent (qcloseevent * event) {If (listui! = 0) listui-> close (); event-> Accept ();} // display the void musicplayer: showlist () {If (listui = 0) {listui = new medialist (this-> sizehint (), this-> pos (), this); listui-> show (); return ;} if (listui-> ishidden () {listui-> show () ;}else {listui-> hide () ;}// open the audio file void musicplayer :: loadmusics () {qstringlist files = qfiledialog: getopenfilenames (this, "select one or more files to open", "/Home", "musics (*. MP3 *. wma *. wav) "); int begin = List-> length (); If (! Files. isempty () {qstring STR; foreach (STR, files) {list-> append (STR); playerlist-> addmedia (qurl :: fromlocalfile (list-> last ();} playerlist-> setcurrentindex (BEGIN); updateinfo (); If (listui = 0) {listui = new medialist (this-> sizehint (), this-> pos (), this);} If (listui! = 0) {listui-> addfiles (Files) ;}// the lyrics initialize initlrc (); player-> play (); qpixmap IMG ("images/pause.png"); playorpauselabel-> setpixmap (IMG. scaled (60, 60);} // right-click any region and choose void musicplayer: contextmenuevent (qcontextmenuevent * event) {menu-> clear (); menu-> addaction (openaction ); menu-> addaction (exitaction); menu-> exec (qcursor: pos (); event-> Accept ();} // void musicplayer: createactions () {menu = new qmenu (thi S); openaction = new qaction ("open", this); exitaction = new qaction ("exit", this) ;}// update the Interface Information void musicplayer: updateinfo () {qstring title; qstring author; qfileinfo F; If (! List-> Empty () {qstring filename = List-> at (playerlist-> currentindex (); Info = new qmediainfo (filename); F. setfile (filename); If (F. suffix (). toupper () = "MP3") {Title = Info-> getinfo ("title", true); author = Info-> getinfo ("timer mer", true );} else {Title = Info-> getinfo ("title"); author = Info-> getinfo ("timer mer");} Title = f. basename (); // cout <title <author; Delete Info;} title. truncat E (8); If (title. length ()> 8) Title + = ".. "; if (title. isempty () Title = "title: Unknown"; if (author. isempty () Author = "Author: Unknown"; musicnamelabel-> settext (title); authorlabel-> settext (author); curlrc = ""; nextlcstr = ""; currentlrc-> settext ("<center>" + curlrc + "</center> "); nextlrc-> settext ("<center>" + nextlcstr + "</center>"); // The lyrics initialize initlrc ();} // The next void musicplayer: Next () {playerl Ist-> next (); // qdebug () <playerlist-> currentindex (); updateinfo (); // The lyrics initialize initlrc ();} // The previous void musicplayer:: Prev () {playerlist-> previous (); updateinfo (); // The lyrics initialize initlrc ();} // time update void musicplayer: durationchange (qint64 duration) {totaltime = duration; progressslider-> setrange (0, duration); timelabel-> settext (TR ("T: <span style = 'color: coral'> % 1 </span>/% 2 "). arg (gettimestr (0 )). arg (gettimestr (total Time);} // progress update void musicplayer: positionchange (qint64 POS) {If (! Progressslider-> issliderdown () progressslider-> setvalue (POS); timelabel-> settext (TR ("T: <span style = 'color: coral'> % 1 </span>/% 2 "). arg (gettimestr (POS )). arg (gettimestr (totaltime); // update the lyrics to display gettimestr (POS) if (LRC-> Empty () return; int key = POS/1000; qmap <int, qstring >:: iterator it; if (LRC-> contains (key) {curlrc = LRC-> value (key); It = LRC-> Find (key ); if (it ++! = LRC-> end () {nextlcstr = it. value () ;}} curlrc = curlrc. trimmed (); curlrc. truncate (15); If (curlrc. length ()> 15) curlrc + = "... "; nextlcstr = nextlcstr. trimmed (); nextlcstr. truncate (15); If (nextlcstr. length ()> 15) nextlcstr + = "... "; currentlrc-> settext (" <center> "+ curlrc +" </center> "); nextlrc-> settext ("<center>" + nextlcstr + "</center>"); // cout <POS <curlrc;} // initialize the lyrics void musicpl Ayer: initlrc () {If (LRC! = 0) LRC-> clear (); If (list = 0) return; curlrc = ""; nextlcstr = ""; // train of thought, use a map to store 10-"string format, and load it after time. Wait for the next time if (list-> isempty () return; If (playerlist = 0) return; qstring filenames = List-> at (playerlist-> currentindex (); qstring lrcnames = filenames; int n = lrcnames. lastindexof ('. '); lrcnames. replace (n, lrcnames. length ()-N ,". LRC "); qfile file (lrcnames); qstring line; If (file. open (qio Device: readonly | qiodevice: Text) {qtextstream stream (& file); While (! Stream. atend () {line = stream. readline (); int num = line. count ('['); int last = line. lastindexof (']'); qstring STR = line. right (line. length ()-last-1); int begin = 0; while (Num --> 0) {qstring STRs = line. mid (begin, 10); begin + = 10; STRs. replace ("[", ""); STRs. replace ("]", ""); int S = getseconds (STRs); If (S> 0) {LRC-> insert (S, str. trimmed (); // cout <S <"=" <LRC-> value (s) ;}}} void m Usicplayer: setposition (int pos) {currentpos = Pos;} void musicplayer: setposition () {player-> setposition (currentpos);} // play or pausevoid musicplayer :: playorpause () {qpixmap IMG ("images/play.png"); If (! Isplaying) {isplaying = true; player-> play (); IMG. load ("images/pause.png");} else {isplaying = false; player-> pause (); IMG. load ("images/play.png");} playorpauselabel-> setpixmap (IMG. scaled (60, 60);} // time string 00:00:00 get millisecond/second, dependent on the operating system qint32 musicplayer: getseconds (qstring & timestr) {qstringlist STRs = timestr. split (":"); qstring s; If (STRs. size ()> 0) {int M = STRs. at (0 ). todouble (); int S = STRs. at (1). Todouble (); // cout <"M" <m; // cout <"S" <s; return M * 60 + S;} return 0 ;} // obtain the total number of seconds from the hour, minute, and second qint32 musicplayer: getseconds (qint32 seconds _, qint32 minutes _, qint32 hours _) {return seconds _ + minutes _ * 60 + hours _ * 3600;} // obtain the string qstring musicplayer: gettimestr (qint32 seconds _) from duuation {seconds _/= 1000; qint32 minutes = seconds _ % 3600/60; qint32 seconds = seconds _ % 3600% 60; minutes = m Inutes> 99? 99: minutes + 100; seconds = seconds + 100; return qstring: Number (minutes ). right (2 ). append (":"). append (qstring: Number (seconds ). right (2);} // change time prompt // void musicplayer: changetime (qint64 time) // {// timelabel-> settext (TR ("t: <span style = 'color: coral'> % 1 </span>/% 2 "). arg (gettimestr (Time/1000 )). arg (gettimestr (totaltime); // The minimum void musicplayer: setmin () {If (! This-> isminimized () {This-> showminimized () ;}}// the volume change prompt void musicplayer: changevol (INT vol) {vollabel-> settext (TR ("V: % 1% "). arg (qstring: Number (VOL); player-> setvolume (VOL);} // form transparency void musicplayer: paintevent (qpaintevent * event) {qpainter painter (this); painter. fillrect (this-> rect (), qcolor (100,100,100,255); // qcolor the last parameter 80 represents the transparency of the background} // void musicplayer: setrounded (bool B) of the rounded corner form) {If (! BMP) {Delete BMP; BMP = 0l;} If (! P) {Delete P; P = 0l;} BMP = new qbitmap (this-> size (); BMP-> Fill (); // fills the pixmap with white (default value) color P = new qpainter (BMP); P-> setpen (QT: nopen); P-> setbrush (QT :: black); If (B) P-> drawroundedrect (BMP-> rect (), 15, 15); else p-> drawroundedrect (BMP-> rect (), 0, 0 ); setmask (* BMP); // set form mask} // void musicplayer: mousepressevent (qmouseevent * event) {If (Event-> button () = QT:: leftbutt On) {ismousedown = true; oldlocation = event-> globalpos ()-This-> pos ();} event-> ignore ();} // void musicplayer related to the mobile form:: mousemoveevent (qmouseevent * event) {If (ismousedown) {This-> move (Event-> globalpos ()-oldlocation); If (listui! = 0) {listui-> moves (this-> sizehint (), this-> pos () ;}} event-> ignore ();} // void musicplayer: mousereleaseevent (qmouseevent * event) {ismousedown = false; event-> ignore ();}
(For reprinting, please indicate the author and source, and do not use them for commercial purposes without permission)