• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegraphics API Reference
  • KDE Home
  • Contact Us
 

okular

  • sources
  • kde-4.12
  • kdegraphics
  • okular
  • core
audioplayer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  ***************************************************************************/
9 
10 #include "audioplayer.h"
11 #include "audioplayer_p.h"
12 
13 // qt/kde includes
14 #include <qbuffer.h>
15 #include <qdir.h>
16 #include <kdebug.h>
17 #include <krandom.h>
18 #include <Phonon/Path>
19 #include <phonon/audiooutput.h>
20 #include <phonon/abstractmediastream.h>
21 #include <phonon/mediaobject.h>
22 
23 // local includes
24 #include "action.h"
25 #include "debug_p.h"
26 #include "sound.h"
27 
28 using namespace Okular;
29 
30 // helper class used to store info about a sound to be played
31 class SoundInfo
32 {
33 public:
34  explicit SoundInfo( const Sound * s = 0, const SoundAction * ls = 0 )
35  : sound( s ), volume( 0.5 ), synchronous( false ), repeat( false ),
36  mix( false )
37  {
38  if ( ls )
39  {
40  volume = ls->volume();
41  synchronous = ls->synchronous();
42  repeat = ls->repeat();
43  mix = ls->mix();
44  }
45  }
46 
47  const Sound * sound;
48  double volume;
49  bool synchronous;
50  bool repeat;
51  bool mix;
52 };
53 
54 
55 class PlayData
56 {
57 public:
58  PlayData()
59  : m_mediaobject( 0 ), m_output( 0 ), m_buffer( 0 )
60  {
61  }
62 
63  void play()
64  {
65  if ( m_buffer )
66  {
67  m_buffer->open( QIODevice::ReadOnly );
68  }
69  m_mediaobject->play();
70  }
71 
72  ~PlayData()
73  {
74  m_mediaobject->stop();
75  delete m_mediaobject;
76  delete m_output;
77  delete m_buffer;
78  }
79 
80  Phonon::MediaObject * m_mediaobject;
81  Phonon::AudioOutput * m_output;
82  QBuffer * m_buffer;
83  SoundInfo m_info;
84 };
85 
86 
87 AudioPlayerPrivate::AudioPlayerPrivate( AudioPlayer * qq )
88  : q( qq )
89 {
90  QObject::connect( &m_mapper, SIGNAL(mapped(int)), q, SLOT(finished(int)) );
91 }
92 
93 AudioPlayerPrivate::~AudioPlayerPrivate()
94 {
95  stopPlayings();
96 }
97 
98 int AudioPlayerPrivate::newId() const
99 {
100  int newid = 0;
101  QHash< int, PlayData * >::const_iterator it;
102  QHash< int, PlayData * >::const_iterator itEnd = m_playing.constEnd();
103  do
104  {
105  newid = KRandom::random();
106  it = m_playing.constFind( newid );
107  } while ( it != itEnd );
108  return newid;
109 }
110 
111 bool AudioPlayerPrivate::play( const SoundInfo& si )
112 {
113  kDebug() ;
114  PlayData * data = new PlayData();
115  data->m_output = new Phonon::AudioOutput( Phonon::NotificationCategory );
116  data->m_output->setVolume( si.volume );
117  data->m_mediaobject = new Phonon::MediaObject();
118  Phonon::createPath(data->m_mediaobject, data->m_output);
119  data->m_info = si;
120  bool valid = false;
121 
122  switch ( si.sound->soundType() )
123  {
124  case Sound::External:
125  {
126  QString url = si.sound->url();
127  kDebug(OkularDebug) << "External," << url;
128  if ( !url.isEmpty() )
129  {
130  int newid = newId();
131  m_mapper.setMapping( data->m_mediaobject, newid );
132  KUrl newurl;
133  if ( KUrl::isRelativeUrl( url ) )
134  {
135  newurl = m_currentDocument;
136  newurl.setFileName( url );
137  }
138  else
139  {
140  newurl = url;
141  }
142  data->m_mediaobject->setCurrentSource( newurl );
143  m_playing.insert( newid, data );
144  valid = true;
145  }
146  break;
147  }
148  case Sound::Embedded:
149  {
150  QByteArray filedata = si.sound->data();
151  kDebug(OkularDebug) << "Embedded," << filedata.length();
152  if ( !filedata.isEmpty() )
153  {
154  kDebug(OkularDebug) << "Mediaobject:" << data->m_mediaobject;
155  int newid = newId();
156  m_mapper.setMapping( data->m_mediaobject, newid );
157  data->m_buffer = new QBuffer();
158  data->m_buffer->setData( filedata );
159  data->m_mediaobject->setCurrentSource( Phonon::MediaSource( data->m_buffer ) );
160  m_playing.insert( newid, data );
161  valid = true;
162  }
163  break;
164  }
165  }
166  if ( !valid )
167  {
168  delete data;
169  data = 0;
170  }
171  if ( data )
172  {
173  QObject::connect( data->m_mediaobject, SIGNAL(finished()), &m_mapper, SLOT(map()) );
174  kDebug(OkularDebug) << "PLAY";
175  data->play();
176  }
177  return valid;
178 }
179 
180 void AudioPlayerPrivate::stopPlayings()
181 {
182  qDeleteAll( m_playing );
183  m_playing.clear();
184 }
185 
186 void AudioPlayerPrivate::finished( int id )
187 {
188  QHash< int, PlayData * >::iterator it = m_playing.find( id );
189  if ( it == m_playing.end() )
190  return;
191 
192  SoundInfo si = it.value()->m_info;
193  // if the sound must be repeated indefinitely, then start the playback
194  // again, otherwise destroy the PlayData as it's no more useful
195  if ( si.repeat )
196  {
197  it.value()->play();
198  }
199  else
200  {
201  m_mapper.removeMappings( it.value()->m_mediaobject );
202  delete it.value();
203  m_playing.erase( it );
204  }
205  kDebug(OkularDebug) << "finished," << m_playing.count();
206 }
207 
208 
209 AudioPlayer::AudioPlayer()
210  : QObject(), d( new AudioPlayerPrivate( this ) )
211 {
212 }
213 
214 AudioPlayer::~AudioPlayer()
215 {
216  delete d;
217 }
218 
219 AudioPlayer * AudioPlayer::instance()
220 {
221  static AudioPlayer ap;
222  return &ap;
223 }
224 
225 void AudioPlayer::playSound( const Sound * sound, const SoundAction * linksound )
226 {
227  // we can't play null pointers ;)
228  if ( !sound )
229  return;
230 
231  // we don't play external sounds for remote documents
232  if ( sound->soundType() == Sound::External && !d->m_currentDocument.isLocalFile() )
233  return;
234 
235  kDebug() ;
236  SoundInfo si( sound, linksound );
237 
238  // if the mix flag of the new sound is false, then the currently playing
239  // sounds must be stopped.
240  if ( !si.mix )
241  d->stopPlayings();
242 
243  d->play( si );
244 }
245 
246 void AudioPlayer::stopPlaybacks()
247 {
248  d->stopPlayings();
249 }
250 
251 #include "audioplayer.moc"
Okular::AudioPlayer::instance
static AudioPlayer * instance()
Gets the instance of the audio player.
Definition: audioplayer.cpp:219
sound.h
debug_p.h
QObject
Okular::AudioPlayerPrivate::play
bool play(const SoundInfo &si)
Definition: audioplayer.cpp:111
Okular::AudioPlayerPrivate::~AudioPlayerPrivate
~AudioPlayerPrivate()
Definition: audioplayer.cpp:93
Okular::Sound::Embedded
Is stored embedded in the document.
Definition: sound.h:34
action.h
Okular::AudioPlayer
An audio player.
Definition: audioplayer.h:30
OkularDebug
#define OkularDebug
Definition: debug_p.h:13
Okular::Sound
Contains information about a sound object.
Definition: sound.h:26
Okular::AudioPlayerPrivate::m_currentDocument
KUrl m_currentDocument
Definition: audioplayer_p.h:44
Okular::AudioPlayer::playSound
void playSound(const Sound *sound, const SoundAction *linksound=0)
Enqueue the specified sound for playing, optionally taking more information about the playing from th...
Definition: audioplayer.cpp:225
Okular::AudioPlayerPrivate::stopPlayings
void stopPlayings()
Definition: audioplayer.cpp:180
Okular::Sound::External
Is stored at external resource (e.g. url)
Definition: sound.h:33
Okular::AudioPlayerPrivate::newId
int newId() const
Definition: audioplayer.cpp:98
Okular::AudioPlayer::~AudioPlayer
~AudioPlayer()
Definition: audioplayer.cpp:214
Okular::Sound::soundType
SoundType soundType() const
Returns the type of the sound object.
Definition: sound.cpp:64
Okular::AudioPlayerPrivate
Definition: audioplayer_p.h:26
Okular::AudioPlayerPrivate::q
AudioPlayer * q
Definition: audioplayer_p.h:40
Okular::AudioPlayerPrivate::m_mapper
QSignalMapper m_mapper
Definition: audioplayer_p.h:43
Okular::AudioPlayerPrivate::AudioPlayerPrivate
AudioPlayerPrivate(AudioPlayer *qq)
Definition: audioplayer.cpp:87
Okular::AudioPlayer::stopPlaybacks
void stopPlaybacks()
Tell the AudioPlayer to stop all the playbacks.
Definition: audioplayer.cpp:246
Okular::AudioPlayerPrivate::finished
void finished(int)
Definition: audioplayer.cpp:186
audioplayer_p.h
Okular::AudioPlayerPrivate::m_playing
QHash< int, PlayData * > m_playing
Definition: audioplayer_p.h:42
Okular::SoundAction
The Sound action plays a sound on activation.
Definition: action.h:323
audioplayer.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:45:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okular

Skip menu "okular"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegraphics API Reference

Skip menu "kdegraphics API Reference"
  •     libkdcraw
  •     libkexiv2
  •     libkipi
  •     libksane
  • okular

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal