Qmediaplayer detailed and simple music playback implementation

Source: Internet
Author: User

Qmediaplayer can be used to parse audio files and video files, inherit from Qmediaobject, and design to qmediacontent objects. Qmediaobject can provide access to media content, initialize it via a URL, and also provide a list of media files that can be played, closely related to the Qmediaplayer object. With Qmediaplayer, in addition to adding the necessary header files, you will need to add qt + = Multimediato the. Pro (project configuration file for Qt). The following is an analysis of the relevant knowledge of qmediaplayer.

    • Type Information

The Qmediaplayer enumeration has information about where the read and play files are likely to appear, first of all error messages .

Enum Qmediaplayer::error

as well Status (state) information for Media Player:

Enum Qmediaplayer::state

and status information that is relevant to state information:

Enum Qmediaplayer::mediastatus


    • Related signals and functions

of course. , it is necessary to obtain the change of the state information when the media content is acquired and needs to be played or played. These include whether it is playing, whether it is tentatively played, whether it has been played, etc.

As a result, this information needs to be understood in a timely manner, so some sets of signals are noted:

    • voidpositionchanged (qint64 position)
    • voidstatechanged (qmediaplayer::state State)
    • voiddurationchanged (qint64 duration)
    • voidmetadataavailablechanged (bool available)

po the sitionchanged signal monitors the properties postiion, readable writable properties. Fast forward and backward functions can be achieved by changing the position of the position. Durationchanged signal is the duration of the recording

The duration transform, which is a readable property. metadavaavailablechaned () signal. When the Qmediaplayer is read into the media file, it will offend the signal. We can use this signal to read into the media file

Related information. Read-In related information can be qvariant qmediaobject::metadata (const QString & key) const to achieve. The types of key are: Author,title and so on. Because too much,

The inconvenience is all listed. Complete list: Qmediametadata Namespace

above is a basic point of reference for the cases that will be covered below. The following is an implementation for easy playback.

#ifndef audioplayer_h#define audioplayer_h#include <QWidget> #include <qmediaplayer>qt_forward_declare_ Class (Qlabel) Qt_forward_declare_class (qslider) Qt_forward_declare_class (Qtoolbutton) class Denoiser;class Audioplayer:public qwidget{q_objectpublic:explicit audioplayer (qwidget *parent = 0); Signals:public slots:voi    D OpenFile ();    void Playfile (const qstring& FilePath);    void Toggleplayback ();    void Seekforward ();    void Seekbackward ();p rivate slots:void updatestate (qmediaplayer::state state);    void Updateposition (Qint64 position);    void Updateduration (Qint64 duration);    void setposition (int position);    void UpdateInfo ();p rivate:void createwidgets ();    Qmediaplayer MediaPlayer;    Qtoolbutton *playbutton;    Qtoolbutton *forwardtoolbutton;    Qtoolbutton *backwardtoolbutton;    Qtoolbutton *openfilebutton;    Qslider *positionslider;    Qlabel *positionlabel;    Qlabel *infolabel; Denoiser *denoiserwidget;}; #endif//Audioplayer_h

descrption function function in the head file function:

1-1. Description of functions of function buttons:

    void OpenFile ();    void Playfile (const qstring& filePath);    void Toggleplayback ();    void Seekforward ();    void Seekbackward ();
The first is the ability to implement file opening to get the audio file path.

The second is the ability to play the file by getting the path to the

The third one is the ability to pause and resume playback.

The fourth one is the ability to move forward and backward.

1-2, the function of the specific button implementation mode:

void Audioplayer::openfile () {const Qstringlist musicpaths = Qstandardpaths::standardlocations (QStandardPaths::    Musiclocation); Const QString FilePath = Qfiledialog::getopenfilename (This, tr ("Open File"), mu Sicpaths.isempty ()? Qdir::homepath (): Musicpaths.first (), tr ("MP3 files (*.mp3);;    All Files (*. *) "); if (!filepath.isempty ()) playfile (FilePath);}    void Audioplayer::p layfile (const QString &filepath) {playbutton->setenabled (true);    Infolabel->settext (Qfileinfo (FilePath). FileName ());    Mediaplayer.setmedia (Qurl::fromlocalfile (FilePath)); Mediaplayer.play ();}    void Audioplayer::toggleplayback () {if (mediaplayer.mediastatus () = = Qmediaplayer::nomedia) openFile ();    else if (mediaplayer.state () = = Qmediaplayer::P layingstate) Mediaplayer.pause (); else Mediaplayer.play ();} void Audioplayer::seekforward () {positionslider->triggeraction (QSLIDER::SLIderpagestepadd);} void Audioplayer::seekbackward () {positionslider->triggeraction (qslider::sliderpagestepsub);}

2-1. Update and get information when the file is played

The acquisition and update of information is definitely an event-driven approach, so in QT the implementation is definitely the signal and slot, so the following sets of slots for obtaining and updating information are defined:

    void Updatestate (qmediaplayer::state state);    void Updateposition (Qint64 position);    void Updateduration (Qint64 duration);    void setposition (int position);    void UpdateInfo ();

1, Updatestate is by the media playback state, that is, Playingstate and Pausestate and Stopstate to the PlayButton set different display state, that is, the pause icon and play icon.

2. updateposition controls the display status and position of the sliders by monitoring the progress of the media playback.

3, Updataduration is used to set the range of the slider bar

4, SetPosition is through the slider pull to achieve the audio file playback progress control

5, Updateinfo is to display the media file information acquisition and reality.

2-2 implementation of information acquisition and updating

void Audioplayer::updatestate (Qmediaplayer::state state) {if (state = = Qmediaplayer::P layingstate) {playbutton-        >settooltip (tr ("Pause"));    Playbutton->seticon (Qicon (":/images/pause.png"));        } else {Playbutton->settooltip (tr ("Play"));    Playbutton->seticon (Qicon (":/images/play.png"));    }}void audioplayer::updateduration (qint64 duration) {positionslider->setrange (0,duration);    Positionslider->setenabled (duration>0); Positionslider->setpagestep (DURATION/10);}    void Audioplayer::updateposition (Qint64 position) {positionslider->setvalue (position);    Qtime Duration (0, position/60000, qround ((position% 60000)/1000.0); Positionlabel->settext (duration.tostring (tr ("Mm:ss"))); void audioplayer::setposition (int position) {if (Qabs (mediaplayer.position ()-position) > ©) mediaplayer.se Tposition (position);}    void Audioplayer::updateinfo () {qstringlist info; QString author = mediaplayer.metadata ("AuthoR "). ToString ();    if (!author.isempty ()) info + = author;    QString title = Mediaplayer.metadata ("title"). ToString ();    if (!title.isempty ()) info + = title; if (!info.isempty ()) Infolabel->settext (Info.join (TR ("-")));}

The above is the implementation of all the code and specific explanations, the need for complete code, you can click to open the link to download. Achieve Results


Qmediaplayer detailed and simple music playback implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.