FluMediaPlayer.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtMultimedia
  4. import FluentUI
  5. Rectangle {
  6. property url source
  7. property bool showControl: false
  8. property real volume: 30
  9. id:control
  10. width: 480
  11. height: 270
  12. color: FluColors.Black
  13. clip: true
  14. MouseArea{
  15. anchors.fill: parent
  16. preventStealing: true
  17. onClicked: {
  18. showControl = !showControl
  19. }
  20. }
  21. MediaPlayer {
  22. id: mediaplayer
  23. property bool autoSeek:true
  24. source: control.source
  25. videoOutput: video_output
  26. audioOutput: AudioOutput{
  27. id:audio_output
  28. }
  29. onErrorChanged:
  30. (error)=> {
  31. console.debug(error)
  32. }
  33. onPositionChanged: {
  34. if(autoSeek){
  35. slider.seek(mediaplayer.position*slider.maxValue/mediaplayer.duration)
  36. }
  37. }
  38. onMediaStatusChanged:
  39. (status)=> {
  40. if(status===2){
  41. slider.maxValue = mediaplayer.duration
  42. showControl = true
  43. mediaplayer.play()
  44. }
  45. }
  46. }
  47. onSourceChanged: {
  48. slider.seek(0)
  49. }
  50. VideoOutput {
  51. id:video_output
  52. anchors.fill: parent
  53. }
  54. Item{
  55. height: 100
  56. y:showControl ? control.height - 110 : control.height
  57. anchors{
  58. horizontalCenter: parent.horizontalCenter
  59. }
  60. width: 460
  61. opacity: showControl
  62. MouseArea{
  63. anchors.fill: parent
  64. }
  65. Behavior on opacity{
  66. NumberAnimation{
  67. duration: 150
  68. }
  69. }
  70. Behavior on y{
  71. NumberAnimation{
  72. duration: 150
  73. }
  74. }
  75. Rectangle{
  76. anchors.fill: parent
  77. color:FluTheme.dark ? Qt.rgba(45/255,45/255,45/255,0.97) : Qt.rgba(237/255,237/255,237/255,0.97)
  78. radius: 5
  79. }
  80. FluSlider{
  81. id:slider
  82. size:parent.width-20
  83. y:20
  84. anchors.horizontalCenter: parent.horizontalCenter
  85. tipEnabled:false
  86. onPressed: {
  87. mediaplayer.autoSeek = false
  88. mediaplayer.pause()
  89. }
  90. value:mediaplayer.position
  91. onReleased: {
  92. mediaplayer.autoSeek = true
  93. mediaplayer.play()
  94. }
  95. onValueChanged: {
  96. if(mediaplayer.autoSeek == false){
  97. mediaplayer.position = value*mediaplayer.duration/slider.maxValue
  98. }
  99. }
  100. onLineClickFunc:function(val){
  101. mediaplayer.position = val*mediaplayer.duration/slider.maxValue
  102. }
  103. }
  104. FluText{
  105. id:start_time
  106. anchors{
  107. top: slider.bottom
  108. topMargin: 10
  109. left: slider.left
  110. }
  111. text: formatDuration(slider.value*mediaplayer.duration/slider.maxValue)
  112. }
  113. FluText{
  114. id:end_time
  115. anchors{
  116. top: slider.bottom
  117. right: slider.right
  118. topMargin: 10
  119. }
  120. text: formatDuration(mediaplayer.duration)
  121. }
  122. Row{
  123. spacing: 10
  124. anchors{
  125. horizontalCenter: parent.horizontalCenter
  126. bottom: parent.bottom
  127. bottomMargin: 10
  128. }
  129. FluIconButton{
  130. iconSize: 17
  131. iconSource: FluentIcons.SkipBack10
  132. onClicked: {
  133. mediaplayer.position = Math.max(mediaplayer.position-10*1000,0)
  134. }
  135. }
  136. FluIconButton{
  137. iconSize: 15
  138. iconSource: mediaplayer.playbackState === MediaPlayer.PlayingState ? FluentIcons.Pause : FluentIcons.Play
  139. onClicked: {
  140. if(mediaplayer.playbackState === MediaPlayer.PlayingState){
  141. mediaplayer.pause()
  142. }else{
  143. mediaplayer.play()
  144. }
  145. }
  146. }
  147. FluIconButton{
  148. iconSize: 17
  149. iconSource: FluentIcons.SkipForward30
  150. onClicked: {
  151. mediaplayer.position = Math.min(mediaplayer.position+30*1000,mediaplayer.duration)
  152. }
  153. }
  154. }
  155. FluIconButton{
  156. id:btn_volume
  157. iconSize: 17
  158. iconSource: audio_output.muted ? FluentIcons.Mute : FluentIcons.Volume
  159. anchors{
  160. left: parent.left
  161. leftMargin: 5
  162. bottom: parent.bottom
  163. bottomMargin: 10
  164. }
  165. onClicked: {
  166. audio_output.muted = !audio_output.muted
  167. }
  168. }
  169. FluSlider{
  170. id:slider_volume
  171. size: 80
  172. dotSize: 20
  173. value:30
  174. anchors{
  175. left:btn_volume.right
  176. verticalCenter: btn_volume.verticalCenter
  177. leftMargin: 10
  178. }
  179. onValueChanged:{
  180. audio_output.volume = value/100
  181. }
  182. }
  183. }
  184. function formatDuration(duration) {
  185. const seconds = Math.floor(duration / 1000);
  186. const hours = Math.floor(seconds / 3600);
  187. const minutes = Math.floor((seconds % 3600) / 60);
  188. const remainingSeconds = seconds % 60;
  189. return `${pad(hours)}:${pad(minutes)}:${pad(remainingSeconds)}`;
  190. }
  191. function pad(value) {
  192. return value.toString().padStart(2, '0');
  193. }
  194. function pause(){
  195. mediaplayer.pause()
  196. }
  197. function play(){
  198. mediaplayer.play()
  199. }
  200. }