Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 4405

qt-doc-4.8.5-10.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- musicplayerexample.qdoc -->
  <title>Qt 4.8: Music Player Example</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Music Player Example</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#mainwindow-class-definition">MainWindow Class Definition</a></li>
<li class="level1"><a href="#mainwindow-class-implementation">MainWindow Class Implementation</a></li>
<li class="level1"><a href="#the-main-function">The main() function.</a></li>
</ul>
</div>
<h1 class="title">Music Player Example</h1>
<span class="subtitle"></span>
<!-- $$$phonon/qmusicplayer-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="phonon-qmusicplayer-mainwindow-cpp.html">phonon/qmusicplayer/mainwindow.cpp</a></li>
<li><a href="phonon-qmusicplayer-mainwindow-h.html">phonon/qmusicplayer/mainwindow.h</a></li>
<li><a href="phonon-qmusicplayer-main-cpp.html">phonon/qmusicplayer/main.cpp</a></li>
<li><a href="phonon-qmusicplayer-qmusicplayer-pro.html">phonon/qmusicplayer/qmusicplayer.pro</a></li>
</ul>
<p>The Music Player Example shows how to use Phonon - the multimedia framework that comes with Qt - to create a simple music player.<p>The player can play music files, and provides simple playback control, such as pausing, stopping, and resuming the music.</p>
<p class="centerAlign"><img src="images/musicplayer.png" alt="" /></p><p>The player has a button group with the play, pause, and stop buttons familiar from most music players. The top-most slider controls the position in the media stream, and the bottom slider allows adjusting the sound volume.</p>
<p>The user can use a file dialog to add music files to a table, which displays meta information about the music - such as the title, album, and artist. Each row contains information about a single music file; to play it, the user selects that row and presses the play button. Also, when a row is selected, the files in the table are queued for playback.</p>
<p>Phonon offers playback of sound using an available audio device, e.g&#x2e;, a sound card or an USB headset. For the implementation, we use two objects: a <a href="phonon-mediaobject.html">MediaObject</a>, which controls the playback, and an <a href="phonon-audiooutput.html">AudioOutput</a>, which can output the audio to a sound device. We will explain how they cooperate when we encounter them in the code. For a high-level introduction to Phonon, see its <a href="phonon-overview.html#phonon-overview">overview</a>.</p>
<p>The API of Phonon is implemented through an intermediate technology on each supported platform: DirectShow, QuickTime, and GStreamer. The sound formats supported may therefore vary from system to system. We do not in this example try to determine which formats are supported, but let Phonon report an error if the user tries to play an unsupported sound file.</p>
<p>Our player consists of one class, <tt>MainWindow</tt>, which both constructs the GUI and handles the playback. We will now go through the parts of its definition and implementation that concerns Phonon.</p>
<a name="mainwindow-class-definition"></a>
<h2>MainWindow Class Definition</h2>
<p>Most of the API in <tt>MainWindow</tt> is private, as is often the case for classes that represent self-contained windows. We list Phonon objects and slots we connect to their signals; we take a closer look at them when we walk through the <tt>MainWindow</tt> implementation.</p>
<pre class="cpp">     Phonon<span class="operator">::</span>SeekSlider <span class="operator">*</span>seekSlider;
     Phonon<span class="operator">::</span>MediaObject <span class="operator">*</span>mediaObject;
     Phonon<span class="operator">::</span>MediaObject <span class="operator">*</span>metaInformationResolver;
     Phonon<span class="operator">::</span>AudioOutput <span class="operator">*</span>audioOutput;
     Phonon<span class="operator">::</span>VolumeSlider <span class="operator">*</span>volumeSlider;
     <span class="type"><a href="qlist.html">QList</a></span><span class="operator">&lt;</span>Phonon<span class="operator">::</span>MediaSource<span class="operator">&gt;</span> sources;</pre>
<p>We use the <a href="phonon-seekslider.html">SeekSlider</a> to move the current playback position in the media stream, and the <a href="phonon-volumeslider.html">VolumeSlider</a> controls the sound volume. Both of these widgets come ready made with Phonon. We use another <a href="phonon-mediaobject.html">MediaObject</a>, metaInformationProvider, to get the meta information from the music files. More on this later.</p>
<pre class="cpp">     <span class="type">void</span> stateChanged(Phonon<span class="operator">::</span>State newState<span class="operator">,</span> Phonon<span class="operator">::</span>State oldState);
     <span class="type">void</span> tick(<span class="type"><a href="qtglobal.html#qint64-typedef">qint64</a></span> time);
     <span class="type">void</span> sourceChanged(<span class="keyword">const</span> Phonon<span class="operator">::</span>MediaSource <span class="operator">&amp;</span>source);
     <span class="type">void</span> metaStateChanged(Phonon<span class="operator">::</span>State newState<span class="operator">,</span> Phonon<span class="operator">::</span>State oldState);
     <span class="type">void</span> aboutToFinish();
     <span class="type">void</span> tableClicked(<span class="type">int</span> row<span class="operator">,</span> <span class="type">int</span> column);</pre>
<p>The <a href="phonon-mediaobject.html">MediaObject</a> informs us of the state of the playback and properties of the media it is playing back through a series of signals. We connect the signals we need to slots in <tt>MainWindow</tt>. The <tt>tableClicked()</tt> slot is connected to the table, so that we know when the user requests playback of a new music file, by clicking on the table.</p>
<a name="mainwindow-class-implementation"></a>
<h2>MainWindow Class Implementation</h2>
<p>The <tt>MainWindow</tt> class handles both the user interface and Phonon. We will now take a look at the code relevant for Phonon. The code required for setting up the GUI is explained elsewhere.</p>
<p>We start with the constructor:</p>
<pre class="cpp"> MainWindow<span class="operator">::</span>MainWindow()
 {
     audioOutput <span class="operator">=</span> <span class="keyword">new</span> Phonon<span class="operator">::</span>AudioOutput(Phonon<span class="operator">::</span>MusicCategory<span class="operator">,</span> <span class="keyword">this</span>);
     mediaObject <span class="operator">=</span> <span class="keyword">new</span> Phonon<span class="operator">::</span>MediaObject(<span class="keyword">this</span>);
     metaInformationResolver <span class="operator">=</span> <span class="keyword">new</span> Phonon<span class="operator">::</span>MediaObject(<span class="keyword">this</span>);

     mediaObject<span class="operator">-</span><span class="operator">&gt;</span>setTickInterval(<span class="number">1000</span>);</pre>
<p>We start by instantiating our media and audio output objects. As mentioned, the media object knows how to playback multimedia (in our case sound files) while the audio output can send it to a sound device.</p>
<p>For the playback to work, the media and audio output objects need to get in contact with each other, so that the media object can send the sound to the audio output. Phonon is a graph based framework, i.e&#x2e;, its objects are nodes that can be connected by paths. Objects are connected using the <tt>createPath()</tt> function, which is part of the Phonon namespace.</p>
<pre class="cpp">     Phonon<span class="operator">::</span>createPath(mediaObject<span class="operator">,</span> audioOutput);</pre>
<p>We also connect signals of the media object to slots in our <tt>MainWindow</tt>. We will examine them shortly.</p>
<pre class="cpp">     connect(mediaObject<span class="operator">,</span> SIGNAL(tick(<span class="type"><a href="qtglobal.html#qint64-typedef">qint64</a></span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(tick(<span class="type"><a href="qtglobal.html#qint64-typedef">qint64</a></span>)));
     connect(mediaObject<span class="operator">,</span> SIGNAL(stateChanged(Phonon<span class="operator">::</span>State<span class="operator">,</span>Phonon<span class="operator">::</span>State))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(stateChanged(Phonon<span class="operator">::</span>State<span class="operator">,</span>Phonon<span class="operator">::</span>State)));
     connect(metaInformationResolver<span class="operator">,</span> SIGNAL(stateChanged(Phonon<span class="operator">::</span>State<span class="operator">,</span>Phonon<span class="operator">::</span>State))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(metaStateChanged(Phonon<span class="operator">::</span>State<span class="operator">,</span>Phonon<span class="operator">::</span>State)));
     connect(mediaObject<span class="operator">,</span> SIGNAL(currentSourceChanged(Phonon<span class="operator">::</span>MediaSource))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(sourceChanged(Phonon<span class="operator">::</span>MediaSource)));
     connect(mediaObject<span class="operator">,</span> SIGNAL(aboutToFinish())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(aboutToFinish()));</pre>
<p>Finally, we call private helper functions to set up the GUI. The <tt>setupUi()</tt> function contains code for setting up the seek , and volume slider. We move on to <tt>setupUi()</tt>:</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>setupUi()
 {
     ...
     seekSlider <span class="operator">=</span> <span class="keyword">new</span> Phonon<span class="operator">::</span>SeekSlider(<span class="keyword">this</span>);
     seekSlider<span class="operator">-</span><span class="operator">&gt;</span>setMediaObject(mediaObject);

     volumeSlider <span class="operator">=</span> <span class="keyword">new</span> Phonon<span class="operator">::</span>VolumeSlider(<span class="keyword">this</span>);
     volumeSlider<span class="operator">-</span><span class="operator">&gt;</span>setAudioOutput(audioOutput);</pre>
<p>After creating the widgets, they must be supplied with the <a href="phonon-mediaobject.html">MediaObject</a> and <a href="phonon-audiooutput.html">AudioOutput</a> objects they should control.</p>
<p>In the <tt>setupActions()</tt>, we connect the actions for the play, pause, and stop tool buttons, to slots of the media object.</p>
<pre class="cpp">     connect(playAction<span class="operator">,</span> SIGNAL(triggered())<span class="operator">,</span> mediaObject<span class="operator">,</span> SLOT(play()));
     connect(pauseAction<span class="operator">,</span> SIGNAL(triggered())<span class="operator">,</span> mediaObject<span class="operator">,</span> SLOT(pause()) );
     connect(stopAction<span class="operator">,</span> SIGNAL(triggered())<span class="operator">,</span> mediaObject<span class="operator">,</span> SLOT(stop()));</pre>
<p>We move on to the slots of <tt>MainWindow</tt>, starting with <tt>addFiles()</tt>:</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>addFiles()
 {
     <span class="type"><a href="qstringlist.html">QStringList</a></span> files <span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getOpenFileNames(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Select Music Files&quot;</span>)<span class="operator">,</span>
         <span class="type"><a href="qdesktopservices.html">QDesktopServices</a></span><span class="operator">::</span>storageLocation(<span class="type"><a href="qdesktopservices.html">QDesktopServices</a></span><span class="operator">::</span>MusicLocation));

     <span class="keyword">if</span> (files<span class="operator">.</span>isEmpty())
         <span class="keyword">return</span>;

     <span class="type">int</span> index <span class="operator">=</span> sources<span class="operator">.</span>size();
     foreach (<span class="type"><a href="qstring.html">QString</a></span> string<span class="operator">,</span> files) {
             Phonon<span class="operator">::</span>MediaSource source(string);

         sources<span class="operator">.</span>append(source);
     }
     <span class="keyword">if</span> (<span class="operator">!</span>sources<span class="operator">.</span>isEmpty())
         metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>setCurrentSource(sources<span class="operator">.</span>at(index));

 }</pre>
<p>In the <tt>addFiles()</tt> slot, we add files selected by the user to the <tt>sources</tt> list. We then set the first source selected on the <tt>metaInformationProvider</tt> <a href="phonon-mediaobject.html">MediaObject</a>, which will send a state changed signal when the meta information is resolved; we have this signal connected to the <tt>metaStateChanged()</tt> slot.</p>
<p>The media object informs us of state changes by sending the <tt>stateChanged()</tt> signal. The <tt>stateChanged()</tt> slot is connected to this signal.</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>stateChanged(Phonon<span class="operator">::</span>State newState<span class="operator">,</span> Phonon<span class="operator">::</span>State <span class="comment">/* oldState */</span>)
 {
     <span class="keyword">switch</span> (newState) {
         <span class="keyword">case</span> Phonon<span class="operator">::</span>ErrorState:
             <span class="keyword">if</span> (mediaObject<span class="operator">-</span><span class="operator">&gt;</span>errorType() <span class="operator">=</span><span class="operator">=</span> Phonon<span class="operator">::</span>FatalError) {
                 <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Fatal Error&quot;</span>)<span class="operator">,</span>
                 mediaObject<span class="operator">-</span><span class="operator">&gt;</span>errorString());
             } <span class="keyword">else</span> {
                 <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Error&quot;</span>)<span class="operator">,</span>
                 mediaObject<span class="operator">-</span><span class="operator">&gt;</span>errorString());
             }
             <span class="keyword">break</span>;</pre>
<p>The <a href="phonon-mediaobject.html#errorString">errorString()</a> function gives a description of the error that is suitable for users of a Phonon application. The two values of the <a href="phonon.html#State-enum">ErrorState</a> enum helps us determine whether it is possible to try to play the same file again.</p>
<pre class="cpp">         <span class="keyword">case</span> Phonon<span class="operator">::</span>PlayingState:
                 playAction<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
                 pauseAction<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
                 stopAction<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
                 <span class="keyword">break</span>;
         <span class="keyword">case</span> Phonon<span class="operator">::</span>StoppedState:
                 stopAction<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
                 playAction<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
                 pauseAction<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
                 timeLcd<span class="operator">-</span><span class="operator">&gt;</span>display(<span class="string">&quot;00:00&quot;</span>);
                 <span class="keyword">break</span>;
         <span class="keyword">case</span> Phonon<span class="operator">::</span>PausedState:
                 pauseAction<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
                 stopAction<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
                 playAction<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
                 <span class="keyword">break</span>;</pre>
<p>We update the GUI when the playback state changes, i.e&#x2e;, when it starts, pauses, stops, or resumes.</p>
<p>The media object will report other state changes, as defined by the <a href="phonon.html#State-enum">State</a> enum.</p>
<p>The <tt>tick()</tt> slot is connected to a <a href="phonon-mediaobject.html">MediaObject</a> signal which is emitted when the playback position changes:</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>tick(<span class="type"><a href="qtglobal.html#qint64-typedef">qint64</a></span> time)
 {
     <span class="type"><a href="qtime.html">QTime</a></span> displayTime(<span class="number">0</span><span class="operator">,</span> (time <span class="operator">/</span> <span class="number">60000</span>) <span class="operator">%</span> <span class="number">60</span><span class="operator">,</span> (time <span class="operator">/</span> <span class="number">1000</span>) <span class="operator">%</span> <span class="number">60</span>);

     timeLcd<span class="operator">-</span><span class="operator">&gt;</span>display(displayTime<span class="operator">.</span>toString(<span class="string">&quot;mm:ss&quot;</span>));
 }</pre>
<p>The <tt>time</tt> is given in milliseconds.</p>
<p>When the table is clicked on with the mouse, <tt>tableClick()</tt> is invoked:</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>tableClicked(<span class="type">int</span> row<span class="operator">,</span> <span class="type">int</span> <span class="comment">/* column */</span>)
 {
     <span class="type">bool</span> wasPlaying <span class="operator">=</span> mediaObject<span class="operator">-</span><span class="operator">&gt;</span>state() <span class="operator">=</span><span class="operator">=</span> Phonon<span class="operator">::</span>PlayingState;

     mediaObject<span class="operator">-</span><span class="operator">&gt;</span>stop();
     mediaObject<span class="operator">-</span><span class="operator">&gt;</span>clearQueue();

     <span class="keyword">if</span> (row <span class="operator">&gt;</span><span class="operator">=</span> sources<span class="operator">.</span>size())
         <span class="keyword">return</span>;

     mediaObject<span class="operator">-</span><span class="operator">&gt;</span>setCurrentSource(sources<span class="operator">[</span>row<span class="operator">]</span>);

     <span class="keyword">if</span> (wasPlaying)
         mediaObject<span class="operator">-</span><span class="operator">&gt;</span>play();
     <span class="keyword">else</span>
         mediaObject<span class="operator">-</span><span class="operator">&gt;</span>stop();
 }</pre>
<p>Since we stop the media object, we first check whether it is currently playing. <tt>row</tt> contains the row in the table that was clicked upon; the indices of <tt>sources</tt> follows the table, so we can simply use <tt>row</tt> to find the new source.</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>sourceChanged(<span class="keyword">const</span> Phonon<span class="operator">::</span>MediaSource <span class="operator">&amp;</span>source)
 {
     musicTable<span class="operator">-</span><span class="operator">&gt;</span>selectRow(sources<span class="operator">.</span>indexOf(source));
     timeLcd<span class="operator">-</span><span class="operator">&gt;</span>display(<span class="string">&quot;00:00&quot;</span>);
 }</pre>
<p>When the media source changes, we simply need to select the corresponding row in the table.</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>metaStateChanged(Phonon<span class="operator">::</span>State newState<span class="operator">,</span> Phonon<span class="operator">::</span>State <span class="comment">/* oldState */</span>)
 {
     <span class="keyword">if</span> (newState <span class="operator">=</span><span class="operator">=</span> Phonon<span class="operator">::</span>ErrorState) {
         <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Error opening files&quot;</span>)<span class="operator">,</span>
             metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>errorString());
         <span class="keyword">while</span> (<span class="operator">!</span>sources<span class="operator">.</span>isEmpty() <span class="operator">&amp;</span><span class="operator">&amp;</span>
                <span class="operator">!</span>(sources<span class="operator">.</span>takeLast() <span class="operator">=</span><span class="operator">=</span> metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>currentSource())) {}  <span class="comment">/* loop */</span>;
         <span class="keyword">return</span>;
     }

     <span class="keyword">if</span> (newState <span class="operator">!</span><span class="operator">=</span> Phonon<span class="operator">::</span>StoppedState <span class="operator">&amp;</span><span class="operator">&amp;</span> newState <span class="operator">!</span><span class="operator">=</span> Phonon<span class="operator">::</span>PausedState)
         <span class="keyword">return</span>;

     <span class="keyword">if</span> (metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>currentSource()<span class="operator">.</span>type() <span class="operator">=</span><span class="operator">=</span> Phonon<span class="operator">::</span>MediaSource<span class="operator">::</span>Invalid)
             <span class="keyword">return</span>;

     <span class="type"><a href="qmap.html">QMap</a></span><span class="operator">&lt;</span><span class="type"><a href="qstring.html">QString</a></span><span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span><span class="operator">&gt;</span> metaData <span class="operator">=</span> metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>metaData();

     <span class="type"><a href="qstring.html">QString</a></span> title <span class="operator">=</span> metaData<span class="operator">.</span>value(<span class="string">&quot;TITLE&quot;</span>);
     <span class="keyword">if</span> (title <span class="operator">=</span><span class="operator">=</span> <span class="string">&quot;&quot;</span>)
         title <span class="operator">=</span> metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>currentSource()<span class="operator">.</span>fileName();

     <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>titleItem <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span>(title);
     titleItem<span class="operator">-</span><span class="operator">&gt;</span>setFlags(titleItem<span class="operator">-</span><span class="operator">&gt;</span>flags() <span class="operator">^</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>ItemIsEditable);
     <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>artistItem <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span>(metaData<span class="operator">.</span>value(<span class="string">&quot;ARTIST&quot;</span>));
     artistItem<span class="operator">-</span><span class="operator">&gt;</span>setFlags(artistItem<span class="operator">-</span><span class="operator">&gt;</span>flags() <span class="operator">^</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>ItemIsEditable);
     <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>albumItem <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span>(metaData<span class="operator">.</span>value(<span class="string">&quot;ALBUM&quot;</span>));
     albumItem<span class="operator">-</span><span class="operator">&gt;</span>setFlags(albumItem<span class="operator">-</span><span class="operator">&gt;</span>flags() <span class="operator">^</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>ItemIsEditable);
     <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>yearItem <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span>(metaData<span class="operator">.</span>value(<span class="string">&quot;DATE&quot;</span>));
     yearItem<span class="operator">-</span><span class="operator">&gt;</span>setFlags(yearItem<span class="operator">-</span><span class="operator">&gt;</span>flags() <span class="operator">^</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>ItemIsEditable);</pre>
<p>When <tt>metaStateChanged()</tt> is invoked, <tt>metaInformationProvider</tt> has resolved the meta data for its current source. A <a href="phonon-mediaobject.html">MediaObject</a> will do this before entering <a href="phonon.html#State-enum">StoppedState</a>. Note that we could also have used the <a href="phonon-mediaobject.html#metaDataChanged">metaDataChanged()</a> signal for this purpose.</p>
<p>Some of the meta data is then chosen to be displayed in the music table. A file might not contain the meta data requested, in which case an empty string is returned.</p>
<pre class="cpp">     <span class="keyword">if</span> (musicTable<span class="operator">-</span><span class="operator">&gt;</span>selectedItems()<span class="operator">.</span>isEmpty()) {
         musicTable<span class="operator">-</span><span class="operator">&gt;</span>selectRow(<span class="number">0</span>);
         mediaObject<span class="operator">-</span><span class="operator">&gt;</span>setCurrentSource(metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>currentSource());
     }

     Phonon<span class="operator">::</span>MediaSource source <span class="operator">=</span> metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>currentSource();
     <span class="type">int</span> index <span class="operator">=</span> sources<span class="operator">.</span>indexOf(metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>currentSource()) <span class="operator">+</span> <span class="number">1</span>;
     <span class="keyword">if</span> (sources<span class="operator">.</span>size() <span class="operator">&gt;</span> index) {
         metaInformationResolver<span class="operator">-</span><span class="operator">&gt;</span>setCurrentSource(sources<span class="operator">.</span>at(index));
     }
     <span class="keyword">else</span> {
         musicTable<span class="operator">-</span><span class="operator">&gt;</span>resizeColumnsToContents();
         <span class="keyword">if</span> (musicTable<span class="operator">-</span><span class="operator">&gt;</span>columnWidth(<span class="number">0</span>) <span class="operator">&gt;</span> <span class="number">300</span>)
             musicTable<span class="operator">-</span><span class="operator">&gt;</span>setColumnWidth(<span class="number">0</span><span class="operator">,</span> <span class="number">300</span>);
     }
 }</pre>
<p>If we have media sources in <tt>sources</tt> of which meta information is not resolved, we set a new source on the <tt>metaInformationProvider</tt>, which will invoke <tt>metaStateChanged()</tt> again.</p>
<p>We move on to the <tt>aboutToFinish()</tt> slot:</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>aboutToFinish()
 {
     <span class="type">int</span> index <span class="operator">=</span> sources<span class="operator">.</span>indexOf(mediaObject<span class="operator">-</span><span class="operator">&gt;</span>currentSource()) <span class="operator">+</span> <span class="number">1</span>;
     <span class="keyword">if</span> (sources<span class="operator">.</span>size() <span class="operator">&gt;</span> index) {
         mediaObject<span class="operator">-</span><span class="operator">&gt;</span>enqueue(sources<span class="operator">.</span>at(index));
     }
 }</pre>
<p>When a file is finished playing, the Music Player will move on and play the next file in the table. This slot is connected to the <a href="phonon-mediaobject.html">MediaObject</a>'s <a href="phonon-mediaobject.html#aboutToFinish">aboutToFinish()</a> signal, which is guaranteed to be emitted while there is still time to enqueue another file for playback.</p>
<a name="the-main-function"></a>
<h2>The main() function.</h2>
<p>Phonon requires that the application has a name; it is set with <a href="qcoreapplication.html#applicationName-prop">setApplicationName()</a>. This is because D-Bus, which is used by Phonon on Linux systems, demands this.</p>
<pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argv<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>args)
 {
     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argv<span class="operator">,</span> args);
     app<span class="operator">.</span>setApplicationName(<span class="string">&quot;Music Player&quot;</span>);
     app<span class="operator">.</span>setQuitOnLastWindowClosed(<span class="keyword">true</span>);

     MainWindow window;
 <span class="preprocessor">#if defined(Q_OS_SYMBIAN)</span>
     window<span class="operator">.</span>showMaximized();
 <span class="preprocessor">#else</span>
     window<span class="operator">.</span>show();
 <span class="preprocessor">#endif</span>

     <span class="keyword">return</span> app<span class="operator">.</span>exec();
 }</pre>
</div>
<!-- @@@phonon/qmusicplayer -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>