Qt Multimedia QML API
Overview
The Qt Multimedia module gives developers a simplified way to use audio and video playback, and access camera functionality. The Multimedia QML API provides a QML friendly interface to these features.
Types
Audio
Audio is an easy way to add audio playback to a Qt Quick scene. Qt Multimedia provides properties for control, methods (functions), and signals.
The code extract below shows the creation and use of an Audio instance.
Item { width: 640 height: 360 Audio { id: playMusic source: "music.wav" } MouseArea { id: playArea anchors.fill: parent onPressed: { playMusic.play() } } }
The snippet above shows how the inclusion of playMusic enables audio features on the type that contains it. So that when the parent's MouseArea is clicked the play() method of Audio is run. Other typical audio control methods are available such as pause() and stop().
Much of the getting and setting of Audio parameters is done through properties. These include:
Property | Description |
---|---|
source | The source URL of the media. |
autoLoad | Indicates if loading of media should begin immediately. |
playing | Indicates that the media is playing. |
paused | The media is paused. |
status | The status of media loading. |
duration | Amount of time in milliseconds the media will play. |
position | Current position in the media in milliseconds of play. |
volume | Audio output volume: from 0.0 (silent) to 1.0 (maximum) |
muted | Indicates audio is muted. |
bufferProgress | Indicates how full the data buffer is: 0.0 (empty) to 1.0 (full). |
seekable | Indicates whether the audio position can be changed. |
playbackRate | The rate at which audio is played at as a multiple of the normal rate. |
error | An error code for the error state including NoError |
errorString | A description of the current error condition. |
The set of signals available allow the developer to create custom behavior when the following events occur:
Signal | Description |
---|---|
playing | Called when playback is started, or when resumed from paused state. |
paused | Called when playback is paused. |
stopped | Called when playback is stopped. |
error | Called when the specified error occurs. |
Camera
Camera enables still image and video capture using QML. It has a number of properties that help setting it up.
The details of using a Camera are described in further depth in the Camera Overview and in the corresponding reference documentation.
Video
Adding video playback, with sound, to a Qt Quick scene is also easy. The process is very similar to that of Audio above, in fact Video shares many of the property names, methods and signals. Here is the equivalent sample code to implement video playback in a scene.
Video { id: video width : 800 height : 600 source: "video.avi" MouseArea { anchors.fill: parent onClicked: { video.play() } } focus: true Keys.onSpacePressed: video.paused = !video.paused Keys.onLeftPressed: video.position -= 5000 Keys.onRightPressed: video.position += 5000 }
There are similar features like play() with new features specific to video.
The behavior and properties of the video object are defined in the above snippet. The source file (video.avi) plays when you click the parent of MouseArea. The video plays in an area of 800 by 600 pixels, and its id property has the value video.
Notice that because signals for the Keys have been defined pressing the:
- Spacebar toggles the pause button.
- Left Arrow moves the current position in the video to 5 seconds previously.
- {Right Arrow} advances the current position in the video by 5 seconds.
Most of the differences will obviously be about video control and information. There are many properties associated with Video, most of them deal with meta-data, control of the video media and aspects of presentation.
SoundEffect
SoundEffect provides a way to play short sound effects, as used in video games. Multiple sound effect instances can be played simultaneously. You should use Audio for music playback.
Item { width: 640 height: 360 SoundEffect { id: effect source: "test.wav" } MouseArea { id: playArea anchors.fill: parent onPressed: { effect.play() } } }
In the above sample the sound effect will be played when the MouseArea is clicked.
For a complete description of this type, see SoundEffect
Multimedia QML Types
Add audio playback to a scene | |
An interface for focus and zoom related camera settings | |
An interface for capturing camera images | |
Add media playback to a scene | |
For specifying a list of media to be played | |
Defines an item in a Playlist | |
Type provides a way to play sound effects in QML | |
A convenience type for showing a specified video | |
Render video or camera viewfinder |