Add VIS vehicle signal support
[apps/mediaplayer.git] / app / MediaPlayer.qml
index 570af0f..95bc22b 100644 (file)
  * limitations under the License.
  */
 
-import QtQuick 2.6
-import QtQuick.Layouts 1.1
-import QtQuick.Controls 2.0
-import QtMultimedia 5.6
+import QtQuick 2.11
+import QtQuick.Layouts 1.11
+import QtQuick.Controls 2.4
+import QtQuick.Window 2.13
+
 import AGL.Demo.Controls 1.0
-import MediaPlayer 1.0
 
 ApplicationWindow {
     id: root
 
-    MediaPlayer {
+    width: container.width * container.scale
+    height: container.height * container.scale
+
+    Item {
         id: player
-        audioRole: MediaPlayer.MusicRole
-        autoLoad: true
-        playlist: playlist
+
+        property string title: ""
+        property string album: ""
+        property string artist: ""
+
+        property bool av_connected: false
+
+        property int duration: 0
+        property int position: 0
+
+        property string status: "stopped"
+
         function time2str(value) {
-            return Qt.formatTime(new Date(value), 'mm:ss')
+            return Qt.formatTime(new Date(value), (value > 3600000) ? 'hh:mm:ss' : 'mm:ss')
         }
-        onPositionChanged: slider.value = player.position
     }
 
-    Playlist {
-        id: playlist
-        playbackMode: random.checked ? Playlist.Random : loop.checked ? Playlist.Loop : Playlist.Sequential
+    Component.onCompleted : {
+        // Let the mediaplayer backend know we're ready for metadata events
+        mediaplayer.start()
 
-        Component.onCompleted: {
-            playlist.addItems(mediaFiles)
+        VehicleSignals.connect()
+    }
+
+    Connections {
+        target: mediaplayer
+
+        onMetadataChanged: {
+            var track = metadata.track
+
+            if ('status' in metadata) {
+                player.status = metadata.status
+            }
+
+            if ('connected' in metadata) {
+                player.av_connected = metadata.connected
+            }
+
+            if (track) {
+                if ('image' in track)
+                     return
+                player.title = track.title
+                player.album = track.album
+                player.artist = track.artist
+
+                if ('duration' in track)
+                     player.duration = track.duration
+
+                if ('index' in track) {
+                     playlistview.currentIndex = track.index
+                }
+            }
+
+            if ('position' in metadata) {
+                player.position = metadata.position
+            }
         }
     }
 
+    Connections {
+        target: VehicleSignals
+
+        onConnected: {
+           VehicleSignals.authorize()
+        }
+
+        onAuthorized: {
+           VehicleSignals.subscribe("Vehicle.Cabin.Infotainment.Media.Action")
+       }
+
+        onSignalNotification: {
+            if (path === "Vehicle.Cabin.Infotainment.Media.Action") {
+                if (value == "SkipForward") {
+                    mediaplayer.next()
+                } else if (value == "SkipBackward") {
+                    mediaplayer.previous()
+                } else if (value == "NextSource") {
+                    if (player.av_connected)
+                        mediaplayer.connect()
+                    else
+                        mediaplayer.disconnect()
+                }
+            }
+        }
+    }
+
+    Timer {
+        id: timer
+        interval: 250
+        running: player.av_connected && player.status == "playing"
+        repeat: true
+        onTriggered: {
+            player.position = player.position + 250
+        }
+    }
+
+    Item {
+        id: container
+        anchors.centerIn: parent
+        width: Window.width
+       height: Window.height
+        //scale: screenInfo.scale_factor()
+        scale: 1
 
     ColumnLayout {
         anchors.fill: parent
@@ -58,7 +146,8 @@ ApplicationWindow {
                 anchors.bottom: parent.bottom
                 height: sourceSize.height * width / sourceSize.width
                 fillMode: Image.PreserveAspectCrop
-                source: player.metaData.coverArtImage ? player.metaData.coverArtImage : ''
+                source: AlbumArt
+                visible: player.av_connected === false
             }
 
             Item {
@@ -80,15 +169,18 @@ ApplicationWindow {
                         Layout.fillHeight: true
                         Row {
                             spacing: 20
-                            ToggleButton {
-                                id: random
-                                offImage: './images/AGL_MediaPlayer_Shuffle_Inactive.svg'
-                                onImage: './images/AGL_MediaPlayer_Shuffle_Active.svg'
-                            }
+                            //ToggleButton {
+                            //    id: random
+                            //    visible: bluetooth.connected == false
+                            //    offImage: './images/AGL_MediaPlayer_Shuffle_Inactive.svg'
+                            //    onImage: './images/AGL_MediaPlayer_Shuffle_Active.svg'
+                            //}
                             ToggleButton {
                                 id: loop
+                                visible: player.av_connected === false
                                 offImage: './images/AGL_MediaPlayer_Loop_Inactive.svg'
                                 onImage: './images/AGL_MediaPlayer_Loop_Active.svg'
+                                onClicked: { mediaplayer.loop(checked ? "playlist" : "off") }
                             }
                         }
                         ColumnLayout {
@@ -96,14 +188,13 @@ ApplicationWindow {
                             Label {
                                 id: title
                                 Layout.alignment: Layout.Center
-                                text: player.metaData.title ? player.metaData.title : ''
+                                text: player.title
                                 horizontalAlignment: Label.AlignHCenter
                                 verticalAlignment: Label.AlignVCenter
                             }
                             Label {
-                                id: artist
                                 Layout.alignment: Layout.Center
-                                text: player.metaData.author ? player.metaData.author : ''
+                                text: player.artist
                                 horizontalAlignment: Label.AlignHCenter
                                 verticalAlignment: Label.AlignVCenter
                                 font.pixelSize: title.font.pixelSize * 0.6
@@ -114,6 +205,8 @@ ApplicationWindow {
                         id: slider
                         Layout.fillWidth: true
                         to: player.duration
+                        enabled: player.av_connected === false
+                        value: player.position
                         Label {
                             id: position
                             anchors.left: parent.left
@@ -128,7 +221,7 @@ ApplicationWindow {
                             font.pixelSize: 32
                             text: player.time2str(player.duration)
                         }
-                        onPressedChanged: player.seek(value)
+                        onPressedChanged: mediaplayer.seek(value)
                     }
                     RowLayout {
                         Layout.fillHeight: true
@@ -140,36 +233,58 @@ ApplicationWindow {
 //                        }
                         Item { Layout.fillWidth: true }
                         ImageButton {
+                            id: previous
                             offImage: './images/AGL_MediaPlayer_BackArrow.svg'
-                            onClicked: playlist.previous()
+                            onClicked: {
+                                mediaplayer.previous()
+                            }
                         }
                         ImageButton {
                             id: play
-                            offImage: './images/AGL_MediaPlayer_Player_Play.svg'
-                            onClicked: player.play()
                             states: [
                                 State {
-                                    when: player.playbackState === MediaPlayer.PlayingState
+                                    when: player.status == "playing"
                                     PropertyChanges {
                                         target: play
                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
-                                        onClicked: player.pause()
+                                        onClicked: {
+                                            mediaplayer.pause()
+                                        }
+                                    }
+                                },
+                                State {
+                                    when: player.status != "playing"
+                                    PropertyChanges {
+                                        target: play
+                                        offImage: './images/AGL_MediaPlayer_Player_Play.svg'
+                                        onClicked: mediaplayer.play()
                                     }
                                 }
                             ]
                         }
                         ImageButton {
+                            id: forward
                             offImage: './images/AGL_MediaPlayer_ForwardArrow.svg'
-                            onClicked: playlist.next()
+                            onClicked: {
+                                mediaplayer.next()
+                            }
                         }
 
                         Item { Layout.fillWidth: true }
-//                        Image {
-//                            source: './images/AGL_MediaPlayer_Bluetooth_Inactive.svg'
-//                        }
-//                        Image {
-//                            source: './images/AGL_MediaPlayer_Radio_Inactive.svg'
-//                        }
+                        ToggleButton {
+                              visible: true
+                              checked: player.av_connected
+                              onClicked: {
+                                if (checked)
+                                        mediaplayer.connect()
+                                else
+                                        mediaplayer.disconnect()
+                              }
+                              contentItem: Image {
+                                source: player.av_connected ? './images/AGL_MediaPlayer_Bluetooth_Active.svg' : './images/AGL_MediaPlayer_Bluetooth_Inactive.svg'
+                              }
+                        }
                     }
                 }
             }
@@ -178,18 +293,19 @@ ApplicationWindow {
             Layout.fillWidth: true
             Layout.fillHeight: true
             Layout.preferredHeight: 407
+
             ListView {
                 anchors.fill: parent
+                id: playlistview
+                visible: player.av_connected === false
                 clip: true
                 header: Label {
                     x: 50
                     text: 'PLAYLIST'
                     opacity: 0.5
                 }
-                model: PlaylistWithMetadata {
-                    source: playlist
-                }
-                currentIndex: playlist.currentIndex
+                model: MediaplayerModel
+                currentIndex: -1
 
                 delegate: MouseArea {
                     id: delegate
@@ -199,12 +315,6 @@ ApplicationWindow {
                         anchors.fill: parent
                         anchors.leftMargin: 50
                         anchors.rightMargin: 50
-                        Image {
-                            source: model.coverArt
-                            fillMode: Image.PreserveAspectFit
-                            Layout.preferredWidth: delegate.height
-                            Layout.preferredHeight: delegate.height
-                        }
                         ColumnLayout {
                             Layout.fillWidth: true
                             Label {
@@ -214,19 +324,18 @@ ApplicationWindow {
                             Label {
                                 Layout.fillWidth: true
                                 text: model.artist
-                                color: '#66FF99'
+                                color: '#00ADDC'
                                 font.pixelSize: 32
                             }
                         }
-                        Label {
-                            text: player.time2str(model.duration)
-                            color: '#66FF99'
-                            font.pixelSize: 32
-                        }
+                        //Label {
+                        //    text: player.time2str(model.duration)
+                        //    color: '#00ADDC'
+                        //    font.pixelSize: 32
+                        //}
                     }
                     onClicked: {
-                        playlist.currentIndex = model.index
-                        player.play()
+                        mediaplayer.picktrack(playlistview.model[index].index)
                     }
                 }
 
@@ -238,3 +347,4 @@ ApplicationWindow {
         }
     }
 }
+}