summaryrefslogtreecommitdiffstats
path: root/tests/manual/qml-multiple-players/qml-multiple-players.qml
blob: cca3c6172f52380d7bf344f70e08c1e1a2d58ec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtMultimedia

Window {
    id: root
    height: 1000
    width: 960
    color: "#000000"
    visible: true

    property var sources: ["qrc:/testvideo1.mp4", "qrc:/testvideo2.mp4"]
    // property var sources: ["file:///path/to/file1.mp4", "file:///path/to/file2.mp4"]

    property int videoOutputHeight: 40
    property int videoOutputWidth: 300

    property int symbolPointSize: 22
    property int textPointSize: 16
    property int buttonHeight: 40

    property bool playing: true
    property bool looping: true
    property bool audioMuted: false
    property bool videoVisible: true

    ListView {
        id: list
        width: parent.width
        height: parent.height - globalControls.height
        clip: true
        spacing: 5
        ScrollBar.vertical: ScrollBar { active: true }

        model: playerModel
        delegate: playerDelegate
    }

    ListModel {
        id: playerModel

        ListElement {
            index: 0
        }
    }

    Component {
        id: playerDelegate

        RowLayout {
            id: listItem
            required property int index

            Component.onCompleted: {
                playerModel.set(listItem.index, { "player": player })
                player.play()
                audioOutput.muted = root.audioMuted
            }

            Label {
                Layout.preferredHeight: 40
                Layout.preferredWidth: 80
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: textPointSize
                color: "#FFFFFF"

                text: "Player " + (listItem.index + 1)
            }

            Button {
                implicitHeight: buttonHeight
                font.pointSize: textPointSize
                implicitWidth: 90

                text: audioOutput.muted ? "Unmute" : "Mute"
                onClicked: {
                    audioOutput.muted = !audioOutput.muted
                }
            }

            Button {
                implicitHeight: buttonHeight
                font.pointSize: textPointSize
                implicitWidth: 80

                text: videoOutput.visible ? "Hide" : "Show"
                onClicked: videoOutput.visible = videoOutput.visible ? false : true
            }

            Button {
                implicitHeight: buttonHeight
                font.pointSize: symbolPointSize
                implicitWidth: 60

                text: player.playing ? "\u23f8" : "\u23f5" // pause / play
                onClicked: player.playing ? player.pause() : player.play()
            }

            Button {
                implicitHeight: buttonHeight
                font.pointSize: symbolPointSize

                text: "\u23f9" // stop
                onClicked: player.stop()
            }

            Button {
                implicitHeight: buttonHeight
                font.pointSize: symbolPointSize

                text: "\u23ed" // skip to next source
                onClicked: {
                    player.sourceIndex += 1
                    player.play()
                }
            }

            VideoOutput {
                id: videoOutput
                Layout.preferredWidth: videoOutputWidth
                Layout.preferredHeight: videoOutputHeight
                visible: true
            }

            AudioOutput {
                id: audioOutput
            }

            MediaPlayer {
                id: player
                videoOutput: videoOutput
                audioOutput: audioOutput
                loops: root.looping ? MediaPlayer.Infinite : 1
                property int sourceIndex: 0
                source: root.sources[sourceIndex % root.sources.length]

                onErrorOccurred: {
                    console.log("Error occurred, status:", player.errorString);
                }
                onSourceChanged: {
                    console.log("Source changed to " + source)
                }
            }
        }
    }

    RowLayout {
        id: globalControls
        height: 60
        anchors.bottom: parent.bottom

        Label {
            Layout.preferredHeight: 40
            Layout.preferredWidth: 80
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            font.pointSize: textPointSize
            color: "#FFFFFF"

            text: "Global"
        }

        Button {
            implicitHeight: buttonHeight
            font.pointSize: textPointSize
            implicitWidth: 90

            text: root.audioMuted ? "Unmute" : "Mute"
            onClicked: {
                root.audioMuted = !root.audioMuted
                for (var i = 0; i < playerModel.count; i++) {
                    playerModel.get(i).player.audioOutput.muted = root.audioMuted
                 }

            }
        }

        Button {
            implicitHeight: buttonHeight
            font.pointSize: textPointSize
            implicitWidth: 80

            text: root.videoVisible ? "Hide" : "Show"
            onClicked: {
                root.videoVisible = !root.videoVisible
                for (var i = 0; i < playerModel.count; i++) {
                    playerModel.get(i).player.videoOutput.visible = root.videoVisible
                }
            }
        }

        Button {
            implicitHeight: buttonHeight
            font.pointSize: root.symbolPointSize
            implicitWidth: 60

            text: root.playing ? "\u23f8" : "\u23f5" // pause / play
            onClicked: {
                root.playing = !root.playing
                for (var i = 0; i < playerModel.count; i++) {
                    if (root.playing) {
                        playerModel.get(i).player.play()
                    } else {
                        playerModel.get(i).player.pause()
                    }

                }
            }
        }

        Button {
            implicitHeight: buttonHeight
            font.pointSize: root.symbolPointSize

            text: "\u23f9" // stop
            onClicked: {
                for (var i = 0; i < playerModel.count; i++) {
                    playerModel.get(i).player.stop()
                }
            }
        }

        Button {
            implicitHeight: buttonHeight
            font.pointSize: root.symbolPointSize

            text: "\u23ed" // skip to next source
            onClicked: {
                for (var i = 0; i < playerModel.count; i++) {
                    var p = playerModel.get(i).player
                    p.sourceIndex += 1
                    p.play()
                }
            }
        }

        Button {
            implicitHeight: buttonHeight
            implicitWidth: 70
            font.pointSize: root.symbolPointSize

            text: root.looping ? "!\u221E" : "\u221E" // looping: !infinite / infinite
            onClicked: {
                root.looping = !root.looping
            }
        }

        Button {
            implicitHeight: buttonHeight
            font.pointSize: textPointSize

            text: "Add player " + (playerModel.count + 1)
            onClicked: {
                playerModel.append({"index": playerModel.count})
            }
        }

        Button {
            implicitHeight: buttonHeight
            font.pointSize: textPointSize

            text: "Remove player"
            onClicked: {
                if (playerModel.count > 0) {
                    playerModel.remove(playerModel.count - 1)
                }
            }
        }
    }
}