• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Phonon

Phonon::MediaObject

MediaObject Class Reference
[Playback, Recording]

#include <Phonon/MediaObject>

Inheritance diagram for MediaObject:

Inheritance graph
[legend]

List of all members.


Detailed Description

Interface for media playback of a given URL.

This class is the most important class in Phonon. Use it to open a media file at an arbitrary location, a CD or DVD or to stream media data from the application to the backend.

This class controls the state (play, pause, stop, seek) and you can use it to get a lot of information about the media data.

Notice that most functions of this class are asynchronous. That means if you call play() the object only starts playing when the stateChanged() signal tells you that the object changed into PlayingState. The states you can expect are documented for those methods.

A common usage example is the following:

 media = new MediaObject(this);
 connect(media, SIGNAL(finished()), SLOT(slotFinished());
 media->setCurrentSource("/home/username/music/filename.ogg");
 media->play();

If you want to play more that one media file (one after another) you can either tell MediaObject about all those files

 media->setCurrentSource(":/sounds/startsound.ogg");
 media->enqueue("/home/username/music/song.mp3");
 media->enqueue(":/sounds/endsound.ogg");
or provide the next file just in time:
   media->setCurrentSource(":/sounds/startsound.ogg");
   connect(media, SIGNAL(aboutToFinish()), SLOT(enqueueNextSource()));
 }

 void enqueueNextSource()
 {
   media->enqueue("/home/username/music/song.mp3");
 }

Author:
Matthias Kretz <kretz@kde.org>

Public Slots

void setTickInterval (qint32 newTickInterval)
void play ()
void pause ()
void stop ()
void seek (qint64 time)

Signals

void stateChanged (Phonon::State newstate, Phonon::State oldstate)
void tick (qint64 time)
void metaDataChanged ()
void seekableChanged (bool isSeekable)
void hasVideoChanged (bool hasVideo)
void bufferStatus (int percentFilled)
void finished ()
void currentSourceChanged (const MediaSource &newSource)
void aboutToFinish ()
void prefinishMarkReached (qint32 msecToEnd)
void totalTimeChanged (qint64 newTotalTime)

Public Member Functions

 ~MediaObject ()
State state () const
bool hasVideo () const
bool isSeekable () const
QStringList metaData (const QString &key) const
QStringList metaData (Phonon::MetaData key) const
QMultiMap< QString, QString > metaData () const
QString errorString () const
ErrorType errorType () const
MediaSource currentSource () const
void setCurrentSource (const MediaSource &source)
QList< MediaSource > queue () const
void setQueue (const QList< MediaSource > &sources)
void setQueue (const QList< QUrl > &urls)
void enqueue (const MediaSource &source)
void enqueue (const QList< MediaSource > &sources)
void enqueue (const QList< QUrl > &urls)
void clearQueue ()
qint64 currentTime () const
qint64 totalTime () const
qint64 remainingTime () const

Properties

qint32 transitionTime
qint32 prefinishMark
qint32 tickInterval

Constructor & Destructor Documentation

~MediaObject (  ) 

Destroys the MediaObject.


Member Function Documentation

State state (  )  const

Get the current state.

Returns:
The state of the object.
See also:
State

stateChanged

bool hasVideo (  )  const

Check whether the media data includes a video stream.

Warning:
This information cannot be known immediately. It is best to also listen to the hasVideoChanged signal.
   connect(media, SIGNAL(hasVideoChanged(bool)), hasVideoChanged(bool));
   media->setCurrentSource("somevideo.avi");
   media->hasVideo(); // returns false;
 }

 void hasVideoChanged(bool b)
 {
   // b == true
   media->hasVideo(); // returns true;
 }

Returns:
true if the media contains video data. false otherwise.
See also:
hasVideoChanged

bool isSeekable (  )  const

Check whether the current media may be seeked.

Warning:
This information cannot be known immediately. It is best to also listen to the hasVideoChanged signal.
   connect(media, SIGNAL(hasVideoChanged(bool)), hasVideoChanged(bool));
   media->setCurrentSource("somevideo.avi");
   media->hasVideo(); // returns false;
 }

 void hasVideoChanged(bool b)
 {
   // b == true
   media->hasVideo(); // returns true;
 }

Returns:
true when the current media may be seeked. false otherwise.
See also:
seekableChanged()

QStringList metaData ( const QString &  key  )  const

Returns the selected audio stream.

See also:
availableAudioStreams

setCurrentAudioStream Returns the selected video stream.

availableVideoStreams

setCurrentVideoStream Returns the selected subtitle stream.

availableSubtitleStreams

setCurrentSubtitleStream Returns the audio streams that can be selected by the user. The strings can directly be used in the user interface.

selectedAudioStream

setCurrentAudioStream Returns the video streams that can be selected by the user. The strings can directly be used in the user interface.

selectedVideoStream

setCurrentVideoStream Returns the subtitle streams that can be selected by the user. The strings can directly be used in the user interface.

selectedSubtitleStream

setCurrentSubtitleStream Returns the strings associated with the given key.

Backends should use the keys specified in the Ogg Vorbis documentation: http://xiph.org/vorbis/doc/v-comment.html

Therefore the following should work with every backend:

A typical usage looks like this:

 setMetaArtist (media->metaData("ARTIST"     ));
 setMetaAlbum  (media->metaData("ALBUM"      ));
 setMetaTitle  (media->metaData("TITLE"      ));
 setMetaDate   (media->metaData("DATE"       ));
 setMetaGenre  (media->metaData("GENRE"      ));
 setMetaTrack  (media->metaData("TRACKNUMBER"));
 setMetaComment(media->metaData("DESCRIPTION"));

QStringList metaData ( Phonon::MetaData  key  )  const

Returns the strings associated with the given key.

Same as above except that the keys are defined in the Phonon::MetaData enum.

QMultiMap<QString, QString> metaData (  )  const

Returns all meta data.

QString errorString (  )  const

Returns a human-readable description of the last error that occurred.

ErrorType errorType (  )  const

Tells your program what to do about the error.

See also:
Phonon::ErrorType

MediaSource currentSource (  )  const

Returns the current media source.

See also:
setCurrentSource

void setCurrentSource ( const MediaSource &  source  ) 

Set the media source the MediaObject should use.

Parameters:
source The MediaSource object to the media data. You can just as well use a QUrl or QString (for a local file) here.
 QUrl url("http://www.example.com/music.ogg");
 media->setCurrentSource(url);

See also:
currentSource

QList<MediaSource> queue (  )  const

Returns the queued media sources.

This does list does not include the current source (returned by currentSource).

void setQueue ( const QList< MediaSource > &  sources  ) 

Set the MediaSources to play when the current media has finished.

This function will overwrite the current queue.

See also:
clearQueue

enqueue

void setQueue ( const QList< QUrl > &  urls  ) 

Set the MediaSources to play when the current media has finished.

This function overwrites the current queue.

See also:
clearQueue

enqueue

void enqueue ( const MediaSource &  source  ) 

Appends one source to the queue.

Use this function to provide the next source just in time after the aboutToFinish signal was emitted.

See also:
aboutToFinish

setQueue

clearQueue

void enqueue ( const QList< MediaSource > &  sources  ) 

Appends multiple sources to the queue.

See also:
setQueue

clearQueue

void enqueue ( const QList< QUrl > &  urls  ) 

Appends multiple sources to the queue.

See also:
setQueue

clearQueue

void clearQueue (  ) 

Clears the queue of sources.

qint64 currentTime (  )  const

Get the current time (in milliseconds) of the file currently being played.

Returns:
The current time in milliseconds.
See also:
tick

qint64 totalTime (  )  const

Get the total time (in milliseconds) of the file currently being played.

Returns:
The total time in milliseconds.
See also:
totalTimeChanged

qint64 remainingTime (  )  const

Get the remaining time (in milliseconds) of the file currently being played.

Returns:
The remaining time in milliseconds.

void setTickInterval ( qint32  newTickInterval  )  [slot]

Selects an audio stream from the media.

Some media formats allow multiple audio streams to be stored in the same file. Normally only one should be played back.

Parameters:
stream Description of an audio stream
See also:
availableAudioStreams()

selectedAudioStream() Selects a video stream from the media.

Some media formats allow multiple video streams to be stored in the same file. Normally only one should be played back. The latter allows to play, two or more different video streams simultaneously.

Parameters:
stream description of a video stream.
See also:
availableVideoStreams()

selectedVideoStream() Selects a subtitle stream from the media.

Some media formats allow multiple subtitle streams to be stored in the same file. Normally only one should be displayed.

Parameters:
stream description of a subtitle stream
See also:
availableSubtitleStreams()

selectedSubtitleStream()

void play (  )  [slot]

Requests playback of the media data to start.

Playback only starts when stateChanged() signals that it goes into PlayingState, though.

Possible states right after this call:
  • BufferingState
  • PlayingState
  • (ErrorState)

void pause (  )  [slot]

Requests playback to pause.

If it was paused before nothing changes.

Possible states right after this call:
  • PlayingState
  • PausedState
  • (ErrorState)

void stop (  )  [slot]

Requests playback to stop.

If it was stopped before nothing changes.

Possible states right after this call:
  • the state it was in before (e.g. PlayingState)
  • StoppedState
  • (ErrorState)

void seek ( qint64  time  )  [slot]

Requests a seek to the time indicated.

You can only seek if state() == PlayingState, BufferingState or PausedState.

The call is asynchronous, so currentTime can still be the old value right after this method was called. If all you need is a slider that shows the current position and allows the user to seek use the class SeekSlider.

Parameters:
time The time in milliseconds where to continue playing.
Possible states right after this call:
  • BufferingState
  • PlayingState
  • (ErrorState)
See also:
SeekSlider

void stateChanged ( Phonon::State  newstate,
Phonon::State  oldstate 
) [signal]

Emitted when the state of the MediaObject has changed.

In case you're not interested in the old state you can also connect to a slot that only has one State argument.

Parameters:
newstate The state the Player is in now.
oldstate The state the Player was in before.

void tick ( qint64  time  )  [signal]

This signal gets emitted every tickInterval milliseconds.

Parameters:
time The position of the media file in milliseconds.
See also:
setTickInterval, tickInterval

void metaDataChanged (  )  [signal]

This signal is emitted whenever the audio/video data that is being played is associated with new meta data.

E.g. for radio streams this happens when the next song is played.

You can get the new meta data with the metaData methods.

void seekableChanged ( bool  isSeekable  )  [signal]

Emitted whenever the return value of isSeekable() changes.

Normally you'll check isSeekable() first and then let this signal tell you whether seeking is possible now or not. That way you don't have to poll isSeekable().

Parameters:
isSeekable true if the stream is seekable (i.e. calling seek() works) false if the stream is not seekable (i.e. all calls to seek() will be ignored)

void hasVideoChanged ( bool  hasVideo  )  [signal]

Emitted whenever the return value of hasVideo() changes.

Normally you'll check hasVideo() first and then let this signal tell you whether video is available now or not. That way you don't have to poll hasVideo().

Parameters:
hasVideo true The stream contains video and adding a VideoWidget will show a video. false There is no video data in the stream and adding a VideoWidget will show an empty (black) VideoWidget.

void bufferStatus ( int  percentFilled  )  [signal]

Tells about the status of the buffer.

You can use this signal to show a progress bar to the user when in BufferingState:

 progressBar->setRange(0, 100); // this is the default
 connect(media, SIGNAL(bufferStatus(int)), progressBar, SLOT(setValue(int)));

Parameters:
percentFilled A number between 0 and 100 telling you how much the buffer is filled.

void finished (  )  [signal]

Emitted when the object has finished playback.

It is not emitted if you call stop(), pause() or load(), but only on end-of-queue or a critical error.

Warning:
This signal is not emitted when the current source has finished and there's another source in the queue. It is only emitted when the queue is empty.
See also:
currentSourceChanged

aboutToFinish

prefinishMarkReached

void currentSourceChanged ( const MediaSource &  newSource  )  [signal]

Emitted when the MediaObject makes a transition to the next MediaSource in the queue.

In other words, it is emitted when an individual MediaSource is finished.

Parameters:
newSource The source that starts to play at the time the signal is emitted.

void aboutToFinish (  )  [signal]

Emitted before the playback of the whole queue stops.

When this signal is emitted you still have time to provide the next MediaSource (using enqueue) so that playback continues.

This signal can be used to provide the next MediaSource just in time for the transition still to work.

See also:
enqueue

void prefinishMarkReached ( qint32  msecToEnd  )  [signal]

Emitted when there are only msecToEnd milliseconds left for playback.

Parameters:
msecToEnd The remaining time until the playback queue finishes.
Warning:
This signal is not emitted when there is another source in the queue. It is only emitted when the queue is empty.
See also:
setPrefinishMark

prefinishMark

aboutToFinish

finished

void totalTimeChanged ( qint64  newTotalTime  )  [signal]

This signal is emitted as soon as the total time of the media file is known or has changed.

For most non-local media data the total time of the media can only be known after some time. At that time the totalTime function can not return useful information. You have to wait for this signal to know the real total time.

Parameters:
newTotalTime The length of the media file in milliseconds.
See also:
totalTime


Property Documentation

qint32 transitionTime [read, write]

Defines the time between media sources.

A positive transition time defines a gap of silence between queued media sources.

A transition time of 0 ms requests gapless playback (sample precise queueing of the next source).

A negative transition time defines a crossfade between the queued media sources.

Defaults to 0 (gapless playback).

Warning:
This feature might not work reliably on every platform.

qint32 prefinishMark [read, write]

Get a signal before playback finishes.

This property specifies the time in milliseconds the prefinishMarkReached signal is emitted before the playback finishes. A value of 0 disables the signal.

Defaults to 0 (disabled).

Warning:
For some media data the total time cannot be determined accurately, therefore the accuracy of the prefinishMarkReached signal can be bad sometimes. Still, it is better to use this method than to look at totalTime and currentTime to emulate the behaviour because the backend might have more information available than your application does through totalTime and currentTime.
See also:
prefinishMarkReached

qint32 tickInterval [read, write]

The time interval in milliseconds between two ticks.

The tick interval is the time that elapses between the emission of two tick signals. If you set the interval to 0 the tick signal gets disabled.

Defaults to 0 (disabled).

Warning:
The back-end is free to choose a different tick interval close to what you asked for. This means that the following code may fail:
 int x = 200;
 media->setTickInterval(x);
 Q_ASSERT(x == producer->tickInterval());
On the other hand the following is guaranteed:
 int x = 200;
 media->setTickInterval(x);
 Q_ASSERT(x >= producer->tickInterval() &&
          x <= 2 * producer->tickInterval());
See also:
tick


The documentation for this class was generated from the following file:
  • mediaobject.h

Phonon

Skip menu "Phonon"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   WTF
  • KJSEmbed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  •   core
  • Phonon
  •   Backend
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal