1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example phonon/qmusicplayer
|
---|
30 | \title Music Player Example
|
---|
31 |
|
---|
32 | The Music Player Example shows how to use Phonon - the multimedia
|
---|
33 | framework that comes with Qt - to create a simple music player.
|
---|
34 | The player can play music files, and provides simple playback
|
---|
35 | control, such as pausing, stopping, and resuming the music.
|
---|
36 |
|
---|
37 | \image musicplayer.png
|
---|
38 |
|
---|
39 | The player has a button group with the play, pause, and stop
|
---|
40 | buttons familiar from most music players. The top-most slider
|
---|
41 | controls the position in the media stream, and the bottom slider
|
---|
42 | allows adjusting the sound volume.
|
---|
43 |
|
---|
44 | The user can use a file dialog to add music files to a table,
|
---|
45 | which displays meta information about the music - such as the
|
---|
46 | title, album, and artist. Each row contains information about a
|
---|
47 | single music file; to play it, the user selects that row and
|
---|
48 | presses the play button. Also, when a row is selected, the files
|
---|
49 | in the table are queued for playback.
|
---|
50 |
|
---|
51 | Phonon offers playback of sound using an available audio device,
|
---|
52 | e.g., a sound card or an USB headset. For the implementation, we
|
---|
53 | use two objects: a \l{Phonon::}{MediaObject}, which controls the
|
---|
54 | playback, and an \l{Phonon::}{AudioOutput}, which can output the
|
---|
55 | audio to a sound device. We will explain how they cooperate when
|
---|
56 | we encounter them in the code. For a high-level introduction to
|
---|
57 | Phonon, see its \l{Phonon Overview}{overview}.
|
---|
58 |
|
---|
59 | The API of Phonon is implemented through an intermediate
|
---|
60 | technology on each supported platform: DirectShow, QuickTime, and
|
---|
61 | GStreamer. The sound formats supported may therefore vary from
|
---|
62 | system to system. We do not in this example try to determine which
|
---|
63 | formats are supported, but let Phonon report an error if the user
|
---|
64 | tries to play an unsupported sound file.
|
---|
65 |
|
---|
66 | Our player consists of one class, \c MainWindow, which both
|
---|
67 | constructs the GUI and handles the playback. We will now go
|
---|
68 | through the parts of its definition and implementation that
|
---|
69 | concerns Phonon.
|
---|
70 |
|
---|
71 | \section1 MainWindow Class Definition
|
---|
72 |
|
---|
73 | Most of the API in \c MainWindow is private, as is often the case
|
---|
74 | for classes that represent self-contained windows. We list Phonon
|
---|
75 | objects and slots we connect to their signals; we take a closer
|
---|
76 | look at them when we walk through the \c MainWindow
|
---|
77 | implementation.
|
---|
78 |
|
---|
79 | \snippet examples/phonon/qmusicplayer/mainwindow.h 2
|
---|
80 |
|
---|
81 | We use the \l{Phonon::}{SeekSlider} to move the current playback
|
---|
82 | position in the media stream, and the \l{Phonon::}{VolumeSlider}
|
---|
83 | controls the sound volume. Both of these widgets come ready made
|
---|
84 | with Phonon. We use another \l{Phonon::}{MediaObject},
|
---|
85 | metaInformationProvider, to get the meta information from the
|
---|
86 | music files. More on this later.
|
---|
87 |
|
---|
88 | \snippet examples/phonon/qmusicplayer/mainwindow.h 1
|
---|
89 |
|
---|
90 | The \l{Phonon::}{MediaObject} informs us of the state of the playback and
|
---|
91 | properties of the media it is playing back through a series of
|
---|
92 | signals. We connect the signals we need to slots in \c MainWindow.
|
---|
93 | The \c tableClicked() slot is connected to the table, so that we
|
---|
94 | know when the user requests playback of a new music file, by
|
---|
95 | clicking on the table.
|
---|
96 |
|
---|
97 | \section1 MainWindow Class Implementation
|
---|
98 |
|
---|
99 | The \c MainWindow class handles both the user interface and
|
---|
100 | Phonon. We will now take a look at the code relevant for Phonon.
|
---|
101 | The code required for setting up the GUI is explained elsewhere.
|
---|
102 |
|
---|
103 | We start with the constructor:
|
---|
104 |
|
---|
105 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 0
|
---|
106 |
|
---|
107 | We start by instantiating our media and audio output objects.
|
---|
108 | As mentioned, the media object knows how to playback
|
---|
109 | multimedia (in our case sound files) while the audio output
|
---|
110 | can send it to a sound device.
|
---|
111 |
|
---|
112 | For the playback to work, the media and audio output objects need
|
---|
113 | to get in contact with each other, so that the media object can
|
---|
114 | send the sound to the audio output. Phonon is a graph based
|
---|
115 | framework, i.e., its objects are nodes that can be connected by
|
---|
116 | paths. Objects are connected using the \c createPath() function,
|
---|
117 | which is part of the Phonon namespace.
|
---|
118 |
|
---|
119 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 1
|
---|
120 |
|
---|
121 | We also connect signals of the media object to slots in our \c
|
---|
122 | MainWindow. We will examine them shortly.
|
---|
123 |
|
---|
124 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 2
|
---|
125 |
|
---|
126 | Finally, we call private helper functions to set up the GUI.
|
---|
127 | The \c setupUi() function contains code for setting up the seek
|
---|
128 | , and volume slider. We move on to \c setupUi():
|
---|
129 |
|
---|
130 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 3
|
---|
131 | \dots
|
---|
132 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 4
|
---|
133 |
|
---|
134 | After creating the widgets, they must be supplied with the
|
---|
135 | \l{Phonon::}{MediaObject} and \l{Phonon::}{AudioOutput} objects
|
---|
136 | they should control.
|
---|
137 |
|
---|
138 | In the \c setupActions(), we connect the actions for the play,
|
---|
139 | pause, and stop tool buttons, to slots of the media object.
|
---|
140 |
|
---|
141 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 5
|
---|
142 |
|
---|
143 | We move on to the slots of \c MainWindow, starting with \c
|
---|
144 | addFiles():
|
---|
145 |
|
---|
146 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 6
|
---|
147 |
|
---|
148 | In the \c addFiles() slot, we add files selected by the user to
|
---|
149 | the \c sources list. We then set the first source selected on the
|
---|
150 | \c metaInformationProvider \l{Phonon::}{MediaObject}, which will
|
---|
151 | send a state changed signal when the meta information is resolved;
|
---|
152 | we have this signal connected to the \c metaStateChanged() slot.
|
---|
153 |
|
---|
154 | The media object informs us of state changes by sending the \c
|
---|
155 | stateChanged() signal. The \c stateChanged() slot is connected
|
---|
156 | to this signal.
|
---|
157 |
|
---|
158 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 9
|
---|
159 |
|
---|
160 | The \l{Phonon::MediaObject::}{errorString()} function gives a
|
---|
161 | description of the error that is suitable for users of a Phonon
|
---|
162 | application. The two values of the \l{Phonon::}{ErrorState} enum
|
---|
163 | helps us determine whether it is possible to try to play the same
|
---|
164 | file again.
|
---|
165 |
|
---|
166 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 10
|
---|
167 |
|
---|
168 | We update the GUI when the playback state changes, i.e., when it
|
---|
169 | starts, pauses, stops, or resumes.
|
---|
170 |
|
---|
171 | The media object will report other state changes, as defined by the
|
---|
172 | \l{Phonon::}{State} enum.
|
---|
173 |
|
---|
174 | The \c tick() slot is connected to a \l{Phonon::}{MediaObject} signal which is
|
---|
175 | emitted when the playback position changes:
|
---|
176 |
|
---|
177 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 11
|
---|
178 |
|
---|
179 | The \c time is given in milliseconds.
|
---|
180 |
|
---|
181 | When the table is clicked on with the mouse, \c tableClick()
|
---|
182 | is invoked:
|
---|
183 |
|
---|
184 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 12
|
---|
185 |
|
---|
186 | Since we stop the media object, we first check whether it is
|
---|
187 | currently playing. \c row contains the row in the table that was
|
---|
188 | clicked upon; the indices of \c sources follows the table, so we
|
---|
189 | can simply use \c row to find the new source.
|
---|
190 |
|
---|
191 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 13
|
---|
192 |
|
---|
193 | When the media source changes, we simply need to select the
|
---|
194 | corresponding row in the table.
|
---|
195 |
|
---|
196 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 14
|
---|
197 |
|
---|
198 | When \c metaStateChanged() is invoked, \c
|
---|
199 | metaInformationProvider has resolved the meta data for its current
|
---|
200 | source. A \l{Phonon::}{MediaObject} will do this before
|
---|
201 | entering \l{Phonon::}{StoppedState}. Note that we could also
|
---|
202 | have used the \l{Phonon::MediaObject::}{metaDataChanged()} signal for
|
---|
203 | this purpose.
|
---|
204 |
|
---|
205 | Some of the meta data is then chosen to be displayed in the
|
---|
206 | music table. A file might not contain the meta data requested,
|
---|
207 | in which case an empty string is returned.
|
---|
208 |
|
---|
209 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 15
|
---|
210 |
|
---|
211 | If we have media sources in \c sources of which meta information
|
---|
212 | is not resolved, we set a new source on the \c
|
---|
213 | metaInformationProvider, which will invoke \c metaStateChanged()
|
---|
214 | again.
|
---|
215 |
|
---|
216 | We move on to the \c aboutToFinish() slot:
|
---|
217 |
|
---|
218 | \snippet examples/phonon/qmusicplayer/mainwindow.cpp 16
|
---|
219 |
|
---|
220 | When a file is finished playing, the Music Player will move on and
|
---|
221 | play the next file in the table. This slot is connected to the
|
---|
222 | \l{Phonon::}{MediaObject}'s
|
---|
223 | \l{Phonon::MediaObject::}{aboutToFinish()} signal, which is
|
---|
224 | guaranteed to be emitted while there is still time to enqueue
|
---|
225 | another file for playback.
|
---|
226 |
|
---|
227 | \section1 The main() function.
|
---|
228 |
|
---|
229 | Phonon requires that the application has a name; it is set with
|
---|
230 | \l{QCoreApplication::}{setApplicationName()}. This is because
|
---|
231 | D-Bus, which is used by Phonon on Linux systems, demands this.
|
---|
232 |
|
---|
233 | \snippet examples/phonon/qmusicplayer/main.cpp 1
|
---|
234 | */
|
---|