binding: mediaplayer: switch to mediaplayer backend binding
[apps/mediaplayer.git] / app / MediaPlayer.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import QtQuick 2.6
18 import QtQuick.Layouts 1.1
19 import QtQuick.Controls 2.0
20 import QtMultimedia 5.6
21 import AGL.Demo.Controls 1.0
22 import MediaPlayer 1.0
23 import 'api' as API
24
25 ApplicationWindow {
26     id: root
27
28     API.MediaPlayer {
29         id: player
30         url: bindingAddress
31     }
32
33     API.BluetoothManager {
34         id: bluetooth
35         url: bindingAddress
36     }
37
38     Timer {
39         id: timer
40         interval: 250
41         running: (bluetooth.av_connected && bluetooth.state == "playing")
42         repeat: true
43         onTriggered: {
44             bluetooth.position = bluetooth.position + 250
45             slider.value = bluetooth.position
46         }
47     }
48
49     ListModel {
50         id: playlist
51     }
52
53     ColumnLayout {
54         anchors.fill: parent
55         Item {
56             Layout.fillWidth: true
57             Layout.fillHeight: true
58             Layout.preferredHeight: 1080
59             clip: true
60             Image {
61                 id: albumart
62                 anchors.left: parent.left
63                 anchors.right: parent.right
64                 anchors.bottom: parent.bottom
65                 height: sourceSize.height * width / sourceSize.width
66                 fillMode: Image.PreserveAspectCrop
67                 source: player.cover_art ? player.cover_art : ''
68                 visible: bluetooth.av_connected == false
69             }
70
71             Item {
72                 anchors.left: parent.left
73                 anchors.right: parent.right
74                 anchors.bottom: parent.bottom
75                 height :307
76                 Rectangle {
77                     anchors.fill: parent
78                     color: 'black'
79                     opacity: 0.75
80                 }
81
82                 ColumnLayout {
83                     anchors.fill: parent
84                     anchors.margins: root.width * 0.02
85                     Item {
86                         Layout.fillWidth: true
87                         Layout.fillHeight: true
88                         Row {
89                             spacing: 20
90                             ToggleButton {
91                                 id: random
92                                 visible: bluetooth.connected == false
93                                 offImage: './images/AGL_MediaPlayer_Shuffle_Inactive.svg'
94                                 onImage: './images/AGL_MediaPlayer_Shuffle_Active.svg'
95                             }
96                             ToggleButton {
97                                 id: loop
98                                 visible: bluetooth.connected == false
99                                 checked: player.loop_state
100                                 offImage: './images/AGL_MediaPlayer_Loop_Inactive.svg'
101                                 onImage: './images/AGL_MediaPlayer_Loop_Active.svg'
102                                 onClicked: { player.loop(checked) }
103                             }
104                         }
105                         ColumnLayout {
106                             anchors.fill: parent
107                             Label {
108                                 id: title
109                                 Layout.alignment: Layout.Center
110                                 text: bluetooth.av_connected ? bluetooth.title : (player.title ? player.title : '')
111                                 horizontalAlignment: Label.AlignHCenter
112                                 verticalAlignment: Label.AlignVCenter
113                             }
114                             Label {
115                                 Layout.alignment: Layout.Center
116                                 text: bluetooth.av_connected ? bluetooth.artist : (player.artist ? player.artist : '')
117                                 horizontalAlignment: Label.AlignHCenter
118                                 verticalAlignment: Label.AlignVCenter
119                                 font.pixelSize: title.font.pixelSize * 0.6
120                             }
121                         }
122                     }
123                     Slider {
124                         id: slider
125                         Layout.fillWidth: true
126                         to: bluetooth.av_connected ? bluetooth.duration : player.duration
127                         enabled: bluetooth.av_connected == false
128                         value: bluetooth.av_connected ? bluetooth.position : player.position
129                         function getPosition() {
130                             if (bluetooth.av_connected) {
131                                 return player.time2str(bluetooth.position)
132                             }
133                             return player.time2str(player.position)
134                         }
135                         Label {
136                             id: position
137                             anchors.left: parent.left
138                             anchors.bottom: parent.top
139                             font.pixelSize: 32
140                             text: slider.getPosition()
141                         }
142                         Label {
143                             id: duration
144                             anchors.right: parent.right
145                             anchors.bottom: parent.top
146                             font.pixelSize: 32
147                             text: bluetooth.av_connected ? player.time2str(bluetooth.duration) : player.time2str(player.duration)
148                         }
149                         onPressedChanged: player.seek(value)
150                     }
151                     RowLayout {
152                         Layout.fillHeight: true
153 //                        Image {
154 //                            source: './images/AGL_MediaPlayer_Playlist_Inactive.svg'
155 //                        }
156 //                        Image {
157 //                            source: './images/AGL_MediaPlayer_CD_Inactive.svg'
158 //                        }
159                         Item { Layout.fillWidth: true }
160                         ImageButton {
161                             id: previous
162                             offImage: './images/AGL_MediaPlayer_BackArrow.svg'
163                             onClicked: {
164                                 if (bluetooth.av_connected) {
165                                     bluetooth.sendMediaCommand("Previous")
166                                     bluetooth.position = 0
167                                 } else {
168                                     player.previous()
169                                 }
170                             }
171                         }
172                         ImageButton {
173                             id: play
174                             offImage: './images/AGL_MediaPlayer_Player_Play.svg'
175                             onClicked: {
176                                 if (bluetooth.av_connected) {
177                                     bluetooth.sendMediaCommand("Play")
178                                 } else {
179                                     player.play()
180                                 }
181                             }
182                             states: [
183                                 State {
184                                     when: player.running === true
185                                     PropertyChanges {
186                                         target: play
187                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
188                                         onClicked: player.pause()
189                                     }
190                                 },
191                                 State {
192                                     when: bluetooth.av_connected && bluetooth.state == "playing"
193                                     PropertyChanges {
194                                         target: play
195                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
196                                         onClicked: bluetooth.sendMediaCommand("Pause")
197                                     }
198                                 }
199
200                             ]
201                         }
202                         ImageButton {
203                             id: forward
204                             offImage: './images/AGL_MediaPlayer_ForwardArrow.svg'
205                             onClicked: {
206                                 if (bluetooth.av_connected) {
207                                     bluetooth.sendMediaCommand("Next")
208                                 } else {
209                                     player.next()
210                                 }
211                             }
212                         }
213
214                         Item { Layout.fillWidth: true }
215  
216                         ToggleButton {
217                               visible: bluetooth.connected
218                               checked: bluetooth.av_connected
219                               offImage: './images/AGL_MediaPlayer_Bluetooth_Inactive.svg'
220                               onImage: './images/AGL_MediaPlayer_Bluetooth_Active.svg'
221
222                               onClicked: {
223                                   if (bluetooth.av_connected) {
224                                       bluetooth.disconnect_profiles()
225                                   } else {
226                                       bluetooth.connect_profiles()
227                                   }
228                               }
229                         }
230                     }
231                 }
232             }
233         }
234         Item {
235             Layout.fillWidth: true
236             Layout.fillHeight: true
237             Layout.preferredHeight: 407
238
239             ListView {
240                 anchors.fill: parent
241                 id: playlistview
242                 visible: bluetooth.av_connected == false
243                 clip: true
244                 header: Label {
245                     x: 50
246                     text: 'PLAYLIST'
247                     opacity: 0.5
248                 }
249                 model: playlist
250                 currentIndex: -1
251
252                 delegate: MouseArea {
253                     id: delegate
254                     width: ListView.view.width
255                     height: ListView.view.height / 4
256                     RowLayout {
257                         anchors.fill: parent
258                         anchors.leftMargin: 50
259                         anchors.rightMargin: 50
260                         ColumnLayout {
261                             Layout.fillWidth: true
262                             Label {
263                                 Layout.fillWidth: true
264                                 text: model.title
265                             }
266                             Label {
267                                 Layout.fillWidth: true
268                                 text: model.artist
269                                 color: '#66FF99'
270                                 font.pixelSize: 32
271                             }
272                         }
273                         //Label {
274                         //    text: player.time2str(model.duration)
275                         //    color: '#66FF99'
276                         //    font.pixelSize: 32
277                         //}
278                     }
279                     onClicked: {
280                         player.pick_track(playlistview.model.get(index).index)
281                         player.play()
282                     }
283                 }
284
285                 highlight: Rectangle {
286                     color: 'white'
287                     opacity: 0.25
288                 }
289             }
290         }
291     }
292 }