Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Multimedia

Multimedia provides a set of APIs that allow the developer to play, record and manage a collection of media content. It is dependent on the QtMultimediaKit module. This API is planned to replace Phonon at a later stage.

No Special Namespace

Unlike the other APIs in Qt Mobility, the Multimedia API is not in the QtMobility namespace.

Overview

This library will require Qt 4.6.

This API delivers an easy to use interface to multimedia functions. The developer can use the API to display an image, or a video, record sound or play a multimedia stream.

There are several benefits this API brings to Qt. Firstly, the developer can now implement fundamental multimedia functions with minimal code, mostly because they are already implemented. Also there is a great deal of flexibility with the media source or the generated multimedia. The source file does not need to be local to the device, it could be streamed from a remote location and identified by a URL. Finally, many different codecs are supported 'out of the box'.

The supplied examples give a good idea at the ease of use of the API. When the supporting user interface code is ignored we can see that functionality is immediately available with minimal effort.

Audio

The Audio Recorder example is a good introduction to the basic use of the API. We will use snippets from this example to illustrate how to use the API to quickly build functionality.

The first step is to demonstrate recording audio to a file. When recording from an audio source there are a number of things we may want to control beyond the essential user interface. We may want a particular encoding of the file, MP3 or Ogg Vorbis for instance, or select a different input source. The user may modify the bitrate, number of channels, quality and sample rate. Here the example will only modify the codec and the source device, since they are essential.

To begin, the developer sets up a source and a recorder object. A QAudioCaptureSource object is created and used to initialize a QMediaRecorder object. The output file name is then set for the QMediaRecorder object.

    audiosource = new QAudioCaptureSource;
    capture = new QMediaRecorder(audiosource);

    capture->setOutputLocation(QUrl("test.raw"));

A list of devices is needed so that an input can be selected in the user interface

    for(int i = 0; i < audiosource->deviceCount(); i++)
        deviceBox->addItem(audiosource->name(i));

and a list of the supported codecs for the user to select a codec,

    QStringList codecs = capture->supportedAudioCodecs();
    for(int i = 0; i < codecs.count(); i++)
        codecsBox->addItem(codecs.at(i));

To set the selected device or codec just use the index of the device or codec by calling the setter in audiosource or capture as appropriate, for example,

    audiosource->setSelectedDevice(i);
    ...
    capture->setAudioCodec(codecIdx);

Now start recording by using the record() function from the new QMediaRecorder object

    capture->record();

And stop recording by calling the matching function stop() in QMediaRecorder.

    capture->stop();

How then would this audio file be played? The QMediaPlayer class will be used as a generic player. Since the player can play both video and audio files the interface will be more complex, but for now the example will concentrate on the audio aspect.

Playing the file is simple: create a player object, pass in the filename, set the volume or other parameters, then play. Not forgetting that the code will need to be hooked up to the user interface.

    QMediaPlayer *player = new QMediaPlayer;
    ...
    player->setMedia(QUrl::fromLocalFile("test.raw"));
    player->setVolume(50);
    player->play();

The filename does not have to be a local file. It could be a URL to a remote resource. Also by using the QMediaPlaylist class from this API we can play a list of local or remote files. The QMediaPlaylist class supports constructing, managing and playing playlists.

    player = new QMediaPlayer;

    playlist = new QMediaPlaylist(player);
    playlist->append(QUrl("http://example.com/myfile1.mp3"));
    playlist->append(QUrl("http://example.com/myfile2.mp3"));
    ...
    playlist->setCurrentPosition(1);
    player->play();

To manipulate the playlist there are the usual management functions (which are in fact slots): previous, next, setCurrentPosition and shuffle. Playlists can be built, saved and loaded using the API.

Video

Continuing with the example discussed for an Audio recorder/player, we can use this to show how to play video files with little change to the code.

Moving from audio to video requires few changes in the sample code. To play a video playlist the code can be changed to include another new Mobility Project class: QVideoWidget. This class enables control of a video resource with signals and slots for the control of brightness, contrast, hue, saturation and full screen mode.

    player = new QMediaPlayer;

    playlist = new QMediaPlaylist(player);
    playlist->append(QUrl("http://example.com/myclip1.mp4"));
    playlist->append(QUrl("http://example.com/myclip2.mp4"));
    ...
    widget = new QVideoWidget(player);
    widget->show();

    playlist->setCurrentPosition(1);
    player->play();

The Player example does things a bit differently to our sample code. instead of using a QVideoWidget object directly, the Player example has a VideoWidget class that inherits from QVideoWidget. This means that functions can be added to provide functions such as full screen display, either on a double click or on a particular keypress.

        videoWidget = new VideoWidget(this);
        player->setVideoOutput(videoWidget);

        playlistModel = new PlaylistModel(this);
        playlistModel->setPlaylist(playlist);

Radio

QRadioTunerControl is a pure virtual base class that will be the basis for any platform specific radio device control. When the functions are implemented the developer will be able to quickly produce an application that supports the typical uses of an FM radio including tuning, volume, start, stop and various other controls.

Extending the API for Symbian and Maemo

For the developer who wishes to extend the functionality of the Multimedia classes there are several classes of particular importance. The default classes are QMediaService, QMediaServiceProvider and QMediaControl.

Basically, the idea is that to use the Multimedia API you would use these three classes or classes derived from them as follows

  • QMediaServiceProvider is used by the top level client class to request a service. The top level class knowing what kind of service it needs.
  • QMediaService provides a service and when asked by the top level object, say a component, will return a QMediaControl object.
  • QMediaControl allows the control of the service using a known interface.

Consider a developer creating, for example, a media player class called MyPlayer. It may have special requirements beyond ordinary media players and so may need a custom service and a custom control. We can subclass QMediaServiceProvider to create out MyServiceProvider class. Also we will create a MyMediaService, and the MyMediaControl to manipulate the media service.

The MyPlayer object calls MyServiceProvider::requestService() to get an instance of MyMediaService. Then the MyPlayer object calls this service object it has just received and calling control() it will receive the control object derived from QMediaControl. Now we have all the parts necessary for our media application. We have the service provider, the service it provides and the control used to manipulate the service. Since our MyPlayer object has instances of the service and its control then it would be possible for these to be used by associated classes that could do additional actions, perhaps with their own control since the parameter to control() is a c-type string, const char *, for the interface.

Adding a Media Service Provider

The base class for creating new service providers is QMediaServiceProvider. The user must implement the requestService() function

    QMediaService* requestService(const QByteArray &type, const QMediaServiceProviderHint &hint);

The details of implementation will depend on the provider. Looking at the class QMediaServiceProvider for the default implementation. Notice that requestService() uses the QMediaServiceProviderHint to look for the appropriate plugin and then to insert it into the plugin map. However, for a specific service provider there is probably no need for this approach, it will simply depend on what the developer wants to implement.

Other methods that may be overloaded

    void releaseService(QMediaService *service);

    QtMediaServices::SupportEstimate hasSupport(const QByteArray &serviceType,
                                        const QString &mimeType,
                                        const QStringList& codecs,
                                        int flags) const;

    QStringList supportedMimeTypes(const QByteArray &serviceType, int flags) const;

    QList<QByteArray> devices(const QByteArray &serviceType) const;

    QString deviceDescription(const QByteArray &serviceType, const QByteArray &device);

The choice of what needs to be done depends on what the developer wishes to do with the service.

Examples

Record a Sound Source

AudioRecorder is a demonstration of the discovery of the supported devices and codecs and the use of recording functions in the QMediaRecorder class.

Play a Media File

The Player example is a simple multimedia player. Select a video file to play, stop, pause, show in fullscreen or manipulate various image attributes using the Color Options button.

Slide Show

The Slide Show shows the use of the QMediaImageViewer and QVideoWidget classes.

Reference documentation

Main classes

QAudioContains enums used by the audio classes
QAudioCaptureSourceInterface to query and select an audio input endpoint
QAudioDeviceInfoInterface to query audio devices and their functionality
QAudioEncoderControlAccess to the settings of a media service that performs audio encoding
QAudioEncoderSettingsSet of audio encoder settings
QAudioEndpointSelectorAudio endpoint selector media control
QAudioFormatStores audio parameter information
QAudioInputInterface for receiving audio data from an audio input device
QAudioOutputInterface for sending audio data to an audio output device
QGraphicsVideoItemGraphics item which display video produced by a QMediaObject
QImageEncoderControlAccess to the settings of a media service that performs image encoding
QMediaBindableInterfaceThe base class for objects extending media objects functionality
QMediaContainerControlAccess to the output container format of a QMediaService
QMediaContentAccess to the resources relating to a media content
QMediaControlBase interface for media service controls
QMediaImageViewerMeans of viewing image media
QMediaObjectCommon base for multimedia objects
QMediaPlayerAllows the playing of a media source
QMediaPlayerControlAccess to the media playing functionality of a QMediaService
QMediaPlaylistList of media content to play
QMediaPlaylistControlAccess to the playlist functionality of a QMediaService
QMediaPlaylistSourceControlAccess to the playlist playback functionality of a QMediaService
QMediaRecorderUsed for the recording of media content
QMediaRecorderControlAccess to the recording functionality of a QMediaService
QMediaResourceDescription of a media resource
QMediaServiceCommon base class for media service implementations
QMediaServiceProviderHintDescribes what is required of a QMediaService
QMediaStreamsControlMedia stream selection control
QMediaTimeIntervalRepresents a time interval with integer precision
QMediaTimeRangeRepresents a set of zero or more disjoint time intervals
QMetaDataWriterControlWrite access to the meta-data of a QMediaService's media
QRadioTunerInterface to the systems analog radio device
QRadioTunerControlAccess to the radio tuning functionality of a QMediaService
QVideoDeviceControlVideo device selector media control
QVideoEncoderControlAccess to the settings of a media service that performs video encoding
QVideoRendererControlControl for rendering to a video surface
QVideoWidgetWidget which presents video produced by a media object
QVideoWidgetControlMedia control which implements a video widget
QVideoWindowControlMedia control for rendering video to a window
QtMultimediaKitContains miscellaneous identifiers used throughout the Qt Media services library

Classes for service implementers.

QMetaDataReaderControlRead access to the meta-data of a QMediaService's media

QML multimedia elements

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 80
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 6
Page suivante

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt qtmobility-1.0
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web