Video.qml 12 KB

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