Video.qml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. import QtQuick
  4. import QtMultimedia
  5. /*!
  6. \qmltype Video
  7. \inherits Item
  8. \ingroup multimedia_qml
  9. \ingroup multimedia_video_qml
  10. \inqmlmodule QtMultimedia
  11. \brief A convenience type for showing a specified video.
  12. \c Video is a convenience type combining the functionality
  13. of a \l MediaPlayer and a \l VideoOutput into one. It provides
  14. simple video playback functionality without having to declare multiple
  15. types.
  16. The following is sample code to implement video playback in a scene.
  17. \qml
  18. Video {
  19. id: video
  20. width : 800
  21. height : 600
  22. source: "video.avi"
  23. MouseArea {
  24. anchors.fill: parent
  25. onClicked: {
  26. video.play()
  27. }
  28. }
  29. focus: true
  30. Keys.onSpacePressed: video.playbackState == MediaPlayer.PlayingState ? video.pause() : video.play()
  31. Keys.onLeftPressed: video.position = video.position - 5000
  32. Keys.onRightPressed: video.position = video.position + 5000
  33. }
  34. \endqml
  35. The source file, \c video.avi, plays when you click the parent
  36. of MouseArea. The video plays in an area of 800 by 600 pixels, and its \c id
  37. property has the value \b{video}.
  38. Notice that because signals for the \l Keys have been defined pressing the:
  39. \list
  40. \li \uicontrol Spacebar toggles the pause button.
  41. \li \uicontrol{Left Arrow} moves the current position in the video to 5 seconds
  42. previously.
  43. \li \uicontrol{Right Arrow} advances the current position in the video by 5 seconds.
  44. \endlist
  45. Video supports un-transformed, stretched, and uniformly scaled
  46. video presentation. For a description of stretched uniformly scaled
  47. presentation, see the \l fillMode property description.
  48. \sa MediaPlayer, VideoOutput
  49. \omit
  50. \section1 Screen Saver
  51. If it is likely that an application will be playing video for an extended
  52. period of time without user interaction, it may be necessary to disable
  53. the platform's screen saver. The \l ScreenSaver (from \l QtSystemInfo)
  54. may be used to disable the screensaver in this fashion:
  55. \qml
  56. import QtSystemInfo 5.0
  57. ScreenSaver { screenSaverEnabled: false }
  58. \endqml
  59. \endomit
  60. */
  61. // TODO: Restore Qt System Info docs when the module is released
  62. Item {
  63. id: video
  64. implicitWidth: videoOut.implicitWidth
  65. implicitHeight: videoOut.implicitHeight
  66. /*** Properties of VideoOutput ***/
  67. /*!
  68. \qmlproperty enumeration Video::fillMode
  69. Set this property to define how the video is scaled to fit the target
  70. area.
  71. \list
  72. \li VideoOutput.Stretch - the video is scaled to fit
  73. \li VideoOutput.PreserveAspectFit - the video is scaled uniformly to fit without
  74. cropping
  75. \li VideoOutput.PreserveAspectCrop - the video is scaled uniformly to fill, cropping
  76. if necessary
  77. \endlist
  78. Because this type is for convenience in QML, it does not
  79. support enumerations directly, so enumerations from \c VideoOutput are
  80. used to access the available fill modes.
  81. The default fill mode is preserveAspectFit.
  82. */
  83. property alias fillMode: videoOut.fillMode
  84. /*!
  85. \qmlproperty enumeration Video::endOfStreamPolicy
  86. \since 6.9
  87. This property specifies the policy to apply when the video stream ends.
  88. The \c endOfStreamPolicy can be one of:
  89. \value ClearOutput The video output is cleared.
  90. \value KeepLastFrame The video output continues displaying the last
  91. frame. Use the method \l clearOutput() to
  92. clear the output manually.
  93. The default value is \c VideoOutput.ClearOutput.
  94. */
  95. property alias endOfStreamPolicy: videoOut.endOfStreamPolicy
  96. /*!
  97. \qmlproperty int Video::orientation
  98. \since 6.9
  99. This property determines the angle, in degrees, at which the displayed video
  100. is rotated clockwise in video coordinates, where the Y-axis points
  101. downwards on the display.
  102. The orientation transformation is applied before \l mirrored.
  103. Only multiples of \c 90 degrees are supported, that is 0, 90, -90, 180, 270, etc.,
  104. otherwise, the specified value is ignored.
  105. The default value is \c 0.
  106. */
  107. property alias orientation: videoOut.orientation
  108. /*!
  109. \qmlproperty int Video::mirrored
  110. Determines whether the displayed video is mirrored around its vertical axis.
  111. The mirroring is applied after \l orientation.
  112. The default value is \c false.
  113. */
  114. property alias mirrored: videoOut.mirrored
  115. /*** Properties of MediaPlayer ***/
  116. /*!
  117. \qmlproperty enumeration Video::playbackState
  118. This read only property indicates the playback state of the media.
  119. \list
  120. \li MediaPlayer.PlayingState - the media is playing
  121. \li MediaPlayer.PausedState - the media is paused
  122. \li MediaPlayer.StoppedState - the media is stopped
  123. \endlist
  124. The default state is MediaPlayer.StoppedState.
  125. */
  126. property alias playbackState: player.playbackState
  127. /*!
  128. \qmlproperty real Video::bufferProgress
  129. This property holds how much of the data buffer is currently filled,
  130. from 0.0 (empty) to 1.0
  131. (full).
  132. */
  133. property alias bufferProgress: player.bufferProgress
  134. /*!
  135. \qmlproperty int Video::duration
  136. This property holds the duration of the media in milliseconds.
  137. If the media doesn't have a fixed duration (a live stream for example)
  138. this will be 0.
  139. */
  140. property alias duration: player.duration
  141. /*!
  142. \qmlproperty int Video::loops
  143. Determines how often the media is played before stopping.
  144. Set to MediaPlayer.Infinite to loop the current media file forever.
  145. The default value is \c 1. Setting this property to \c 0 has no effect.
  146. */
  147. property alias loops: player.loops
  148. /*!
  149. \qmlproperty enumeration Video::error
  150. This property holds the error state of the video. It can be one of:
  151. \list
  152. \li MediaPlayer.NoError - there is no current error.
  153. \li MediaPlayer.ResourceError - the video cannot be played due to a problem
  154. allocating resources.
  155. \li MediaPlayer.FormatError - the video format is not supported.
  156. \li MediaPlayer.NetworkError - the video cannot be played due to network issues.
  157. \li MediaPlayer.AccessDenied - the video cannot be played due to insufficient
  158. permissions.
  159. \li MediaPlayer.ServiceMissing - the video cannot be played because the media
  160. service could not be
  161. instantiated.
  162. \endlist
  163. */
  164. property alias error: player.error
  165. /*!
  166. \qmlproperty string Video::errorString
  167. This property holds a string describing the current error condition in more detail.
  168. */
  169. property alias errorString: player.errorString
  170. /*!
  171. \qmlproperty bool Video::hasAudio
  172. This property holds whether the current media has audio content.
  173. */
  174. property alias hasAudio: player.hasAudio
  175. /*!
  176. \qmlproperty bool Video::hasVideo
  177. This property holds whether the current media has video content.
  178. */
  179. property alias hasVideo: player.hasVideo
  180. /*!
  181. \qmlproperty mediaMetaData Video::metaData
  182. This property holds the meta data for the current media.
  183. See \l{MediaPlayer::metaData}{MediaPlayer.metaData} for details about each meta data key.
  184. \sa {mediaMetaData}
  185. */
  186. property alias metaData: player.metaData
  187. /*!
  188. \qmlproperty bool Video::muted
  189. This property holds whether the audio output is muted.
  190. */
  191. property alias muted: audioOutput.muted
  192. /*!
  193. \qmlproperty real Video::playbackRate
  194. This property holds the rate at which video is played at as a multiple
  195. of the normal rate.
  196. */
  197. property alias playbackRate: player.playbackRate
  198. /*!
  199. \qmlproperty int Video::position
  200. This property holds the current playback position in milliseconds.
  201. */
  202. property alias position: player.position
  203. /*!
  204. \qmlproperty bool Video::seekable
  205. This property holds whether the playback position of the video can be
  206. changed.
  207. If true, calling the \l seek() method or changing the \l position property
  208. will cause playback to seek to the new position.
  209. */
  210. property alias seekable: player.seekable
  211. /*!
  212. \qmlproperty url Video::source
  213. This property holds the source URL of the media.
  214. */
  215. property alias source: player.source
  216. /*!
  217. \since 6.7
  218. \qmlproperty bool Video::autoPlay
  219. This property controls whether the media begins to play automatically after it gets loaded.
  220. Defaults to \c false.
  221. */
  222. property alias autoPlay: player.autoPlay
  223. /*!
  224. \qmlproperty real Video::volume
  225. This property holds the audio volume.
  226. The volume is scaled linearly from \c 0.0 (silence) to \c 1.0
  227. (full volume). Values outside this range will be clamped.
  228. The default volume is \c 1.0.
  229. UI volume controls should usually be scaled nonlinearly. For example,
  230. using a logarithmic scale will produce linear changes in perceived
  231. loudness, which is what a user would normally expect from a volume
  232. control. See \l {QtAudio::convertVolume()} for more details.
  233. */
  234. property alias volume: audioOutput.volume
  235. /*!
  236. \qmlsignal Video::paused()
  237. This signal is emitted when playback is paused.
  238. */
  239. signal paused
  240. /*!
  241. \qmlsignal Video::stopped()
  242. This signal is emitted when playback is stopped.
  243. */
  244. signal stopped
  245. /*!
  246. \qmlsignal Video::playing()
  247. This signal is emitted when playback is started or continued.
  248. */
  249. signal playing
  250. /*!
  251. \qmlsignal Video::errorOccurred(error, errorString)
  252. This signal is emitted when an \a error has occurred. The \a errorString
  253. parameter may contain more detailed information about the error.
  254. */
  255. signal errorOccurred(int error, string errorString)
  256. VideoOutput {
  257. id: videoOut
  258. anchors.fill: video
  259. }
  260. MediaPlayer {
  261. id: player
  262. onPlaybackStateChanged: function(newState) {
  263. if (newState === MediaPlayer.PausedState)
  264. video.paused();
  265. else if (newState === MediaPlayer.StoppedState)
  266. video.stopped();
  267. else
  268. video.playing();
  269. }
  270. onErrorOccurred: function(error, errorString) {
  271. video.errorOccurred(error, errorString);
  272. }
  273. videoOutput: videoOut
  274. audioOutput: AudioOutput {
  275. id: audioOutput
  276. }
  277. }
  278. /*!
  279. \qmlmethod Video::play()
  280. Starts playback of the media.
  281. */
  282. function play() {
  283. player.play();
  284. }
  285. /*!
  286. \qmlmethod Video::pause()
  287. Pauses playback of the media.
  288. */
  289. function pause() {
  290. player.pause();
  291. }
  292. /*!
  293. \qmlmethod Video::stop()
  294. Stops playback of the media.
  295. */
  296. function stop() {
  297. player.stop();
  298. }
  299. /*!
  300. \qmlmethod Video::seek(offset)
  301. If the \l seekable property is true, seeks the current
  302. playback position to \a offset.
  303. \sa seekable, position
  304. */
  305. function seek(offset) {
  306. player.position = offset;
  307. }
  308. /*!
  309. \qmlmethod Video::clearOutput()
  310. \since 6.9
  311. Clears the video output by removing the current video frame.
  312. This method is recommended when you need to remove the last video frame after
  313. the video stream ends with the \l endOfStreamPolicy Video property
  314. set to \c KeepLastFrame.
  315. */
  316. function clearOutput() {
  317. videoOut.clearOutput();
  318. }
  319. }