Phonon.MediaObject Class Reference

[ phonon module]

The MediaObject class provides an interface for media playback. 更多...

继承 QObject and MediaNode .

方法

Qt Signals


详细描述

The MediaObject class provides an interface for media playback.

The media object manages a MediaSource , which supplies the media object with multimedia content, e.g., from a file. A playback in Phonon is always started by calling the play() 函数。

The state of play (play, pause, stop, seek) is controlled by the media object, and you can also query the current state() . It keeps track of the playback position in the media stream, and emits the tick() signal when the current position in the stream changes.

Notice that most functions of this class are asynchronous, so you cannot rely on that a state is entered after a function call before you receive the stateChanged() 信号。 The description of the State enum gives a description of the different states.

Before play () 是 called, the media object should be connected to output nodes , which outputs the media to the underlying hardware. The output nodes required are dependent on the contents of the multimedia file that is played back. Phonon has currently two output nodes: the AudioOutput for audio content and VideoWidget for video content. If a MediaSource contains both audio and video, both nodes need to be connected to the media object.

     Phonon.MediaObject *mediaObject = new Phonon.MediaObject(this);
     Phonon.VideoWidget *videoWidget = new Phonon.VideoWidget(this);
     Phonon.createPath(mediaObject, videoWidget);
     Phonon.AudioOutput *audioOutput =
         new Phonon.AudioOutput(Phonon.VideoCategory, this);
     Phonon.createPath(mediaObject, audioOutput);
     mediaObject->play();
			

The media object can queue sources for playback. When it has finished to play one source, it will start playing the next in the queue; the new source is then removed from the queue. The queue can be altered at any time.

 media->setCurrentSource(":/sounds/startsound.ogg");
 media->enqueue("/home/username/music/song.mp3");
 media->enqueue(":/sounds/endsound.ogg");
			

You can also make use of the aboutToFinish() signal, which is guaranteed to be emitted in time for altering the queue.

   media->setCurrentSource(":/sounds/startsound.ogg");
   connect(media, SIGNAL(aboutToFinish()), SLOT(enqueueNextSource()));
 }
 void enqueueNextSource()
 {
   media->enqueue("/home/username/music/song.mp3");
 }
			

When playback is finishing, i.e., when a media source has been played to the end and the queue is empty, several signals are emitted. First, the media object will emit aboutToFinish () - shortly before the playback has finished - and then finished ()。 stateChanged () signal will also be emitted with PausedState , which is the state the media object takes when the playback is finished. If you wish to enter another state, you can connect a slot to finished () and set a new state there.

The media object resolves the meta information, such as title, artist, and album. The meta data is not resolved immediately after a new source is provided, but will be resolved before the object leaves the LoadingState 。 data is queried by string keys - which should follow the Ogg Vorbis specification http://xiph.org/vorbis/doc/v-comment.html - or by using the MetaData enum. The data available will depend on the type and content of the individual media files. metaDataChanged () will be emitted when the media object has resolved new meta data.

Errors encountered during playback and loading of media sources are reported by emitting a state changed signal with ErrorState . The severity of the error can be queried by the ErrorType . With a NormalError , it might be possible to continue the playback, for instance, if only audio playback fails for a media source which also has video. A FatalError indicates that Phonon cannot continue playback of the current source, but it is possible to try with a different one. A user readable error message is given by errorString ().


方法文档编制

MediaObject.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

MediaObject.clear ( self )

This method is also a Qt slot with the C++ signature void clear() .

Stops and removes all playing and enqueued media sources.

另请参阅 setCurrentSource ().

MediaObject.clearQueue ( self )

Clears the queue of media sources.

另请参阅 queue () 和 enqueue ().

MediaSource MediaObject.currentSource ( self )

Returns the current media source, i.e., the media source that is being played back. The current source is either set with setCurrentSource () or taken from the media queue () when a media source has finished playing.

另请参阅 setCurrentSource ().

int MediaObject.currentTime ( self )

Returns the current time (in milliseconds), i.e., position in the media stream, of the file currently being played.

另请参阅 tick (), totalTime (),和 remainingTime ().

MediaObject.enqueue ( self , MediaSource   source )

追加 source to the queue.

You can use this function to provide the next source after the aboutToFinish () 信号已被发射。

另请参阅 aboutToFinish (), setQueue (),和 clearQueue ().

MediaObject.enqueue ( self , list-of-Phonon.MediaSource  sources )

Appends multiple sources to the queue.

另请参阅 setQueue () 和 clearQueue ().

MediaObject.enqueue ( self , list-of-QUrl  urls )

Appends the URLs in urls to the media source queue.

The function will create MediaSource s from the QUrl s, and append these to the queue.

另请参阅 setQueue () 和 clearQueue ().

QString MediaObject.errorString ( self )

Returns a human-readable description of the last error that occurred. The strings given may vary between backends.

The error description can be used to give a message to the user - and the developer - when the stateChanged () signal is emitted with ErrorState .

Qt 后端

On Windows, Qt fetches its error messages from the DirectShow backend. This usually includes an error number, which can be looked up in the DirectShow documentation: http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dx81_c/directx_cpp/htm/errorandsuccesscodes.asp .

On Linux and Mac, the error strings are not fetched directly from the backend, but are created in the backend.

另请参阅 Phonon.ErrorState and stateChanged ().

ErrorType MediaObject.errorType ( self )

Tells your program what to do about the last error that occurred. Use this function after receiving a stateChanged () signal with ErrorState .

另请参阅 Phonon.ErrorType , Phonon.ErrorState ,和 stateChanged ().

bool MediaObject.hasVideo ( self )

Check whether the current media source includes a video stream.

警告: This information is not resolved immediately after a media object gets a new source. Listen to the hasVideoChanged () signal instead.

   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;
 }
			

返回 true if the media contains video data; otherwise, returns false .

另请参阅 hasVideoChanged ().

bool MediaObject.isSeekable ( self )

Check whether it is possible to seek, i.e., change the playback position in the media stream.

警告: This information is not solved immediately after the media object gets a new media source. The hasVideoChanged () signal is emitted after this information is available.

   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;
 }
			

返回 true if the current media may be seeked; otherwise, returns false .

另请参阅 seekableChanged ().

QStringList MediaObject.metaData ( self , QString  key )

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:

Note that meta data is not resolved before the metaDataChanged() 信号被发射。

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 MediaObject.metaData ( self , MetaData   key )

Returns the strings associated with the given key .

Same as above except that the keys are defined in the Phonon.MetaData 枚举。

另请参阅 metaDataChanged ().

dict-of-QString-list-of-QString MediaObject.metaData ( self )

Returns all meta data in a multi map.

另请参阅 metaDataChanged ().

MediaObject.pause ( self )

This method is also a Qt slot with the C++ signature void pause() .

Requests playback to pause, and the media object to enter the PausedState . If it was paused already, nothing changes.

This function is asynchronous and the media might not be paused immediately.

另请参阅 play (), stop (),和 stateChanged ().

MediaObject.play ( self )

This method is also a Qt slot with the C++ signature void play() .

Requests playback of the media data to start.

Playback starts when the stateChanged () signal is emitted with PlayingState .

If the media object is already in a PlayingState ,什么都不发生。

另请参阅 stop (), pause (),和 stateChanged ().

int MediaObject.prefinishMark ( self )

list-of-Phonon.MediaSource MediaObject.queue ( self )

Returns the queued media sources.

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

另请参阅 setQueue () 和 enqueue ().

int MediaObject.remainingTime ( self )

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

Returns the remaining time in milliseconds.

另请参阅 totalTime (), currentTime (),和 totalTimeChanged ().

MediaObject.seek ( self , int  time )

This method is also a Qt slot with the C++ signature void seek(qint64) .

Requests a seek to the time indicated, specified in 毫秒。

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 .

If the current source of the media object is not seekable, calls to this functions do nothing.

另请参阅 SeekSlider and tick ().

MediaObject.setCurrentSource ( self , MediaSource   source )

Set the media source the MediaObject should use.

After the media object receives a new source, it will enter the LoadingState . When it is ready to play, it enters the StoppedState unless another state has been requested, e.g., by calling play ().

source 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);
			

另请参阅 currentSource () 和 MediaSource .

MediaObject.setPrefinishMark ( self , int  msecToEnd )

MediaObject.setQueue ( self , list-of-Phonon.MediaSource  sources )

设置 sources to play when the current source has finished.

This function will overwrite the current queue.

另请参阅 clearQueue () 和 enqueue ().

MediaObject.setQueue ( self , list-of-QUrl  urls )

设置 urls to play when the current media has finished.

This function overwrites the current queue.

另请参阅 clearQueue () 和 enqueue ().

MediaObject.setTickInterval ( self , int  newTickInterval )

This method is also a Qt slot with the C++ signature void setTickInterval(qint32) .

MediaObject.setTransitionTime ( self , int  msec )

State MediaObject.state ( self )

返回当前 Phonon.State of the object.

另请参阅 Phonon.State and stateChanged ().

MediaObject.stop ( self )

This method is also a Qt slot with the C++ signature void stop() .

Requests playback to stop, and the media object to enter the StoppedState . If it was stopped before nothing changes.

This function is asynchronous and the media might not be stopped immediately.

另请参阅 play (), pause (),和 stateChanged ().

int MediaObject.tickInterval ( self )

int MediaObject.totalTime ( self )

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

Returns the total time in milliseconds.

警告: The total time is not defined before the media object enters the LoadingState .

另请参阅 totalTimeChanged ().

int MediaObject.transitionTime ( self )


Qt Signal Documentation

void aboutToFinish ()

This is the default overload of this signal.

Emitted before the playback of the whole queue ends. When this signal is emitted you still have time to enqueue () a new MediaSource , so that playback continues.

If you need a signal to be emitted at a specific time before playback is finished, you should use the prefinishMarkReached () signal instead.

另请参阅 enqueue (), prefinishMark ,和 prefinishMarkReached ().

void bufferStatus (int)

This is the default overload of this signal.

Provides information about the status of the buffer.

MediaObject 是在 the BufferingState , it will send this signal regularly. percentFilled is a number between 0 and 100 telling you how much the buffer is filled.

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)));
			

注意: BufferingState is commonly used when waiting for data over a network connection, but this might not be true for all backends.

void currentSourceChanged (const Phonon::MediaSource&)

This is the default overload of this signal.

Emitted when the MediaObject fetches a new MediaSource queue () and before it enters the LoadingState for the new source. The media object will take a new source from the queue () when it has finished the playback of the current source .

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

void finished ()

This is the default overload of this signal.

Emitted when the object has finished playback. It is not emitted if you call stop (), pause () or load(). It is emitted only when the current media source has finished playing and the media queue () is empty, or when a fatal error occurs.

警告: 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.

另请参阅 currentSourceChanged (), aboutToFinish (),和 prefinishMarkReached ().

void hasVideoChanged (bool)

This is the default overload of this signal.

Emitted whenever the return value of hasVideo () changes, i.e., the media source being played back contains video.

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 ().

hasVideo is true when the stream contains video and adding a VideoWidget will show a video, and false if there is no video data in the stream and adding a VideoWidget will show an empty (black) VideoWidget .

void metaDataChanged ()

This is the default overload of this signal.

This signal is emitted when the media object has resolved new meta data. This will happen before the media object leaves the LoadingState after a new source has been set.

This signal is not emitted when the media object removes the current data, i.e., when a new source is set or an error has occurred. If you need to know this, you can listen for the ErrorState , and connect to the currentSourceChanged() 信号。

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

另请参阅 metaData (), currentSourceChanged (), stateChanged (), and Phonon.State .

void prefinishMarkReached (qint32)

This is the default overload of this signal.

Emitted when there are only msecToEnd milliseconds left of playback.

警告: This signal is not emitted when there is another source in the queue. It is only emitted when the queue is empty.

另请参阅 setPrefinishMark (), prefinishMark (), aboutToFinish (),和 finished ().

void seekableChanged (bool)

This is the default overload of this signal.

This signal is emitted when the media object's ability to seek in the media stream changes. isSeekable is true if it is possible to seek (); otherwise, it is false.

Change in the ability to seek in the stream usually happens when the current source changes or when an error occurs.

Normally you'll check isSeekable () after setting a new media source, and then let this signal tell you when seeking is possible. That way you don't have to poll isSeekable ().

void stateChanged (Phonon::State,Phonon::State)

This is the default overload of this signal.

This signal is emitted when the state of the MediaObject has changed. The oldstate and newstate parameters indicate the previous state and current state of the media object.

If you are only interested in the new state of the media object, you can connect this signal to a slot that accepts only one State 自变量。

void tick (qint64)

This is the default overload of this signal.

This signal is emitted in intervals defined by the tickInterval property. The current position of the media object in the stream is given by the time parameter. The time 指定在 毫秒。

另请参阅 tickInterval .

void totalTimeChanged (qint64)

This is the default overload of this 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.

newTotalTime is the length of the media file in 毫秒。

另请参阅 totalTime ().