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.
音频 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 / setting of 音频 parameters is done through properties. These include
特性 | 描述 |
---|---|
source | 媒体的源 URL。 |
autoLoad | Indicates if loading of media should begin immediately. |
playing | Indicates that the media is playing. |
paused | 媒体已暂停。 |
status | 媒体加载的状态。 |
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,
信号 | 描述 |
---|---|
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 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 摄像头概述 and in the corresponding reference documentation.
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 视频 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.
In the above sample when the parent of MouseArea is clicked, an area of 800x600 pixels with an id of 'video', the source "video.avi" will play in that area. Notice also that signals for the Keys have been defined so that a spacebar will toggle the pause button; the left arrow will move the current position in the video to 5 seconds previously; and the right arrow will advance 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 视频 , most of them deal with meta-data, control of the video media and aspects of presentation.
SoundEffect provides a way to play short sound effects, like in video games. Multiple sound effect instances can be played simultaneously. You should use 音频 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 被点击。
此类型的完整描述,见 SoundEffect
视频 | 展示指定视频的方便类型 |
音频 | 把音频回放添加到场景 |
MediaPlayer | 把媒体回放添加到场景 |
Camera | 访问取景器帧,并拍摄照片及影片 |
CameraCapture | 捕获摄像头图像的接口 |
CameraExposure | 曝光相关摄像头设置的接口 |
CameraFlash | 闪光相关摄像头设置的接口 |
CameraFocus | 对焦相关摄像头设置的接口 |
CameraImageProcessing | 摄像头捕获相关设置的接口 |
CameraRecorder | 控制摄像头录制视频 |
QtMultimedia | 提供具有 Qt Multimedia 有用功能的全局对象 |
Playlist | 针对要播放的指定媒体列表 |
PlaylistItem | 定义 Playlist 播放列表项 |
Radio | Access radio functionality from a QML application |
RadioData | 从 QML 应用程序访问 RDS 数据 |
Torch | 简单控制手电筒功能 |
SoundEffect | 提供在 QML 中播放音效方式的类型 |
VideoOutput | 渲染视频或摄像头取景器 |