doc/src/snippets/code/doc_src_phonon-api.qdoc
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 //! [0]
       
    43 PushStream::PushStream(QObject *parent)
       
    44   : AbstractMediaStream(parent), m_timer(new QTimer(this))
       
    45 {
       
    46   setStreamSize(getMediaStreamSize());
       
    47 
       
    48   connect(m_timer, SIGNAL(timeout()), SLOT(moreData()));
       
    49   m_timer->setInterval(0);
       
    50 }
       
    51 
       
    52 void PushStream::moreData()
       
    53 {
       
    54   const QByteArray data = getMediaData();
       
    55   if (data.isEmpty()) {
       
    56     endOfData();
       
    57   } else {
       
    58     writeData(data);
       
    59   }
       
    60 }
       
    61 
       
    62 void PushStream::needData()
       
    63 {
       
    64   m_timer->start();
       
    65   moreData();
       
    66 }
       
    67 
       
    68 void PushStream::enoughData()
       
    69 {
       
    70   m_timer->stop();
       
    71 }
       
    72 //! [0]
       
    73 
       
    74 
       
    75 //! [1]
       
    76 PullStream::PullStream(QObject *parent)
       
    77   : AbstractMediaStream(parent)
       
    78 {
       
    79   setStreamSize(getMediaStreamSize());
       
    80 }
       
    81 
       
    82 void PullStream::needData()
       
    83 {
       
    84   const QByteArray data = getMediaData();
       
    85   if (data.isEmpty()) {
       
    86     endOfData();
       
    87   } else {
       
    88     writeData(data);
       
    89   }
       
    90 }
       
    91 //! [1]
       
    92 
       
    93 
       
    94 //! [2]
       
    95 seekStream(0);
       
    96 //! [2]
       
    97 
       
    98 
       
    99 //! [3]
       
   100 MediaObject m;
       
   101 QString fileName("/home/foo/bar.ogg");
       
   102 QUrl url("http://www.example.com/stream.mp3");
       
   103 QBuffer *someBuffer;
       
   104 m.setCurrentSource(fileName);
       
   105 m.setCurrentSource(url);
       
   106 m.setCurrentSource(someBuffer);
       
   107 m.setCurrentSource(Phonon::Cd);
       
   108 //! [3]
       
   109 
       
   110 
       
   111 //! [4]
       
   112 VideoPlayer *player = new VideoPlayer(Phonon::VideoCategory, parentWidget);
       
   113 connect(player, SIGNAL(finished()), player, SLOT(deleteLater()));
       
   114 player->play(url);
       
   115 //! [4]
       
   116 
       
   117 
       
   118 //! [5]
       
   119 audioPlayer->load(url);
       
   120 audioPlayer->play();
       
   121 //! [5]
       
   122 
       
   123 
       
   124 //! [6]
       
   125 media = new MediaObject(this);
       
   126 connect(media, SIGNAL(finished()), SLOT(slotFinished());
       
   127 media->setCurrentSource("/home/username/music/filename.ogg");
       
   128 
       
   129 ...
       
   130 
       
   131 media->play();
       
   132 //! [6]
       
   133 
       
   134 
       
   135 //! [7]
       
   136 media->setCurrentSource(":/sounds/startsound.ogg");
       
   137 media->enqueue("/home/username/music/song.mp3");
       
   138 media->enqueue(":/sounds/endsound.ogg");
       
   139 //! [7]
       
   140 
       
   141 
       
   142 //! [8]
       
   143   media->setCurrentSource(":/sounds/startsound.ogg");
       
   144   connect(media, SIGNAL(aboutToFinish()), SLOT(enqueueNextSource()));
       
   145 }
       
   146 
       
   147 void enqueueNextSource()
       
   148 {
       
   149   media->enqueue("/home/username/music/song.mp3");
       
   150 }
       
   151 //! [8]
       
   152 
       
   153 
       
   154 //! [9]
       
   155 int x = 200;
       
   156 media->setTickInterval(x);
       
   157 Q_ASSERT(x == producer->tickInterval());
       
   158 //! [9]
       
   159 
       
   160 
       
   161 //! [10]
       
   162 int x = 200;
       
   163 media->setTickInterval(x);
       
   164 Q_ASSERT(x >= producer->tickInterval() &&
       
   165          x <= 2producer->tickInterval());
       
   166 //! [10]
       
   167 
       
   168 
       
   169 //! [11]
       
   170   connect(media, SIGNAL(hasVideoChanged(bool)), hasVideoChanged(bool));
       
   171   media->setCurrentSource("somevideo.avi");
       
   172   media->hasVideo(); // returns false;
       
   173 }
       
   174 
       
   175 void hasVideoChanged(bool b)
       
   176 {
       
   177   // b == true
       
   178   media->hasVideo(); // returns true;
       
   179 }
       
   180 //! [11]
       
   181 
       
   182 
       
   183 //! [12]
       
   184   connect(media, SIGNAL(hasVideoChanged(bool)), hasVideoChanged(bool));
       
   185   media->setCurrentSource("somevideo.avi");
       
   186   media->hasVideo(); // returns false;
       
   187 }
       
   188 
       
   189 void hasVideoChanged(bool b)
       
   190 {
       
   191   // b == true
       
   192   media->hasVideo(); // returns true;
       
   193 }
       
   194 //! [12]
       
   195 
       
   196 
       
   197 //! [13]
       
   198 setMetaArtist(media->metaData("ARTIST"));
       
   199 setMetaAlbum(media->metaData("ALBUM"));
       
   200 setMetaTitle(media->metaData("TITLE"));
       
   201 setMetaDate(media->metaData("DATE"));
       
   202 setMetaGenre(media->metaData("GENRE"));
       
   203 setMetaTrack(media->metaData("TRACKNUMBER"));
       
   204 setMetaComment(media->metaData("DESCRIPTION"));
       
   205 //! [13]
       
   206 
       
   207 
       
   208 //! [14]
       
   209 QUrl url("http://www.example.com/music.ogg");
       
   210 media->setCurrentSource(url);
       
   211 //! [14]
       
   212 
       
   213 
       
   214 //! [15]
       
   215 progressBar->setRange(0, 100); // this is the default
       
   216 connect(media, SIGNAL(bufferStatus(int)), progressBar, SLOT(setValue(int)));
       
   217 //! [15]
       
   218 
       
   219 
       
   220 //! [16]
       
   221 QObject::connect(BackendCapabilities::notifier(), SIGNAL(capabilitiesChanged()), ...
       
   222 //! [16]
       
   223 
       
   224 
       
   225 //! [17]
       
   226 QComboBox *cb = new QComboBox(parentWidget);
       
   227 ObjectDescriptionModel *model = new ObjectDescriptionModel(cb);
       
   228 model->setModelData(BackendCapabilities::availableAudioOutputDevices());
       
   229 cb->setModel(model);
       
   230 cb->setCurrentIndex(0); // select first entry
       
   231 //! [17]
       
   232 
       
   233 
       
   234 //! [18]
       
   235 int cbIndex = cb->currentIndex();
       
   236 AudioOutputDevice selectedDevice = model->modelData(cbIndex);
       
   237 //! [18]
       
   238 
       
   239 
       
   240 //! [19]
       
   241 Path path = Phonon::createPath(...);
       
   242 Effect *effect = new Effect(this);
       
   243 path.insertEffect(effect);
       
   244 //! [19]
       
   245 
       
   246 
       
   247 //! [20]
       
   248 MediaObject *media = new MediaObject;
       
   249 AudioOutput *output = new AudioOutput(Phonon::MusicCategory);
       
   250 Path path = Phonon::createPath(media, output);
       
   251 Q_ASSERT(path.isValid()); // for this simple case the path should always be
       
   252                           //valid - there are unit tests to ensure it
       
   253 // insert an effect
       
   254 QList<EffectDescription> effectList = BackendCapabilities::availableAudioEffects();
       
   255 if (!effectList.isEmpty()) {
       
   256     Effect *effect = path.insertEffect(effectList.first());
       
   257 }
       
   258 //! [20]
       
   259 
       
   260 
       
   261 //! [21]
       
   262 MediaObject *media = new MediaObject(parent);
       
   263 VideoWidget *vwidget = new VideoWidget(parent);
       
   264 Phonon::createPath(media, vwidget);
       
   265 //! [21]