Debian Codegraph @

src/qml/player/AbstractPlayer.qml

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Ugo Riboni <[email protected]>
6 * MichaƂ Sawicz <[email protected]>
7 * Renato Araujo Oliveira Filho <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 3.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 import QtQuick 2.4
22 import QtMultimedia 5.0
23 import "../common"
24 import "../common/utils.js" as Utils
25
26 Item {
27 id: player
28
29 property bool playing: state == "playing"
30 property bool paused: state == "paused"
31 property real aspectRatio: width / height
32 property variant video: mediaPlayer
33 property alias videoOutput: _videoOutput
34
35 property alias source: mediaPlayer.source
36 property alias status: mediaPlayer.status
37 property alias volume: mediaPlayer.volume
38
39 readonly property alias duration: mediaPlayer.duration
40 readonly property alias position: mediaPlayer.position
41
42 property int forwardSeekStep: Math.min(60000, mediaPlayer.duration * 0.05)
43 property int backwardSeekStep: Math.min(30000, mediaPlayer.duration * 0.025)
44
45
46 signal error(int errorCode, string errorString)
47
48 objectName: "videoPlayer"
49 state: "stopped"
50
51 function stop() {
52 state = "stopped"
53 }
54
55 function play() {
56 state = "playing"
57 }
58
59 function pause() {
60 state = "paused"
61 }
62
63 function togglePause() {
64 if (playing) {
65 pause()
66 } else if (paused) {
67 play()
68 }
69 }
70
71 function seekForward() {
72 return seek(forwardSeekStep)
73 }
74
75 function seekBackward() {
76 return seek(-backwardSeekStep)
77 }
78
79 function seek(value) {
80 if (mediaPlayer.seekable) {
81 if (state != "playing" && state != "paused") {
82 state = "playing"
83 state = "paused"
84 }
85 mediaPlayer.seek(mediaPlayer.position + value)
86 } else {
87 return false
88 }
89 return true
90 }
91
92 function fastForward() {
93 if (mediaPlayer.seekable) {
94 if (state == "forwarding") {
95 scrubbingTimer.step = Math.min(5 * forwardSeekStep, scrubbingTimer.step * 1.5)
96 } else {
97 state = "forwarding"
98 }
99 return true
100 } else {
101 return false
102 }
103 }
104
105 function rewind() {
106 if (mediaPlayer.seekable) {
107 if (state == "rewinding") {
108 scrubbingTimer.step = Math.max(-5 * forwardSeekStep, scrubbingTimer.step * 1.5)
109 } else {
110 state = "rewinding"
111 }
112 return true
113 } else {
114 return false
115 }
116 }
117
118 VideoOutput {
119 id: _videoOutput
120
121 source: mediaPlayer
122 anchors.fill: parent
123 smooth: true
124
125 Behavior on opacity { NumberAnimation { duration: 200; easing.type: Easing.OutQuad } }
126 }
127
128 MediaPlayer {
129 id: mediaPlayer
130
131 property real aspectRatio: metaData.resolution !== undefined ? metaData.resolution.width / metaData.resolution.height : -1
132 property int realWidth: aspectRatio != -1 ? Math.min(width, (aspectRatio >= parent.aspectRatio ? width : aspectRatio * height)) : -1
133 property int realHeight: aspectRatio != -1 ? Math.min(height, (aspectRatio < parent.aspectRatio ? height : width / aspectRatio)) : -1
134 property int widthMargin: aspectRatio != -1 ? (aspectRatio >= parent.aspectRatio ? 0 : (width - realWidth) / 2) : -1
135 property int heightMargin: aspectRatio != -1 ? (aspectRatio < parent.aspectRatio ? 0 : (height - realHeight) / 2) : -1
136
137 onError: {
138 console.error("AbstractPlayer: " + error + ":" + errorString)
139 player.error(error, errorString)
140 }
141
142 onPlaybackStateChanged: {
143 // Make sure that the app toggles the play/pause button when playbackStatus
144 // changes from underneath it in media-hub/qtubuntu-media
145 if (mediaPlayer.playbackState == MediaPlayer.PausedState) {
146 player.pause()
147 } else if (mediaPlayer.playbackState == MediaPlayer.PlayingState) {
148 player.play()
149 }
150 }
151 }
152
153 Timer {
154 id: scrubbingTimer
155 interval: 500
156 repeat: true
157
158 property int step
159
160 onTriggered:
161 if (mediaPlayer.position + step < 0) {
162 mediaPlayer.seek(0)
163 player.state = "playing"
164 } else {
165 mediaPlayer.seek(mediaPlayer.position + step)
166 }
167 }
168
169 states: [
170 State {
171 name: "stopped"
172 StateChangeScript { script: mediaPlayer.stop() }
173 PropertyChanges { target: scrubbingTimer; running: false }
174 },
175
176 State {
177 name: "playing"
178 PropertyChanges { target: mediaPlayer; playbackRate: 1.0; muted: false }
179 StateChangeScript { script: mediaPlayer.play() }
180 PropertyChanges { target: scrubbingTimer; running: false }
181 },
182
183 State {
184 name: "paused"
185 StateChangeScript { script: mediaPlayer.pause() }
186 PropertyChanges { target: scrubbingTimer; running: false }
187 },
188
189 State {
190 name: "forwarding"
191 PropertyChanges { target: mediaPlayer; muted: true }
192 StateChangeScript { script: mediaPlayer.pause() }
193 PropertyChanges { target: scrubbingTimer; running: true }
194 },
195
196 State {
197 name: "rewinding"
198 PropertyChanges { target: mediaPlayer; muted: true }
199 StateChangeScript { script: mediaPlayer.pause() }
200 PropertyChanges { target: scrubbingTimer; running: true }
201 }
202 ]
203
204 transitions: [
205 Transition {
206 to: "forwarding"
207 PropertyAction { target: scrubbingTimer; property: "step"; value: forwardSeekStep }
208 },
209
210 Transition {
211 to: "rewinding"
212 PropertyAction { target: scrubbingTimer; property: "step"; value: -forwardSeekStep }
213 }
214 ]
215 }