X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=app%2FMediaPlayer.qml;h=6f8d0db20f51f51b9f5e742afbde3fda19684ca3;hb=009400d49ccb54144e1311931a769fcb325b1b0d;hp=a937bc616aafcdd3abdcbcc4eb1be5fe166804d8;hpb=3bf175c871cac7d6b60d8263196262ea5ffcda74;p=apps%2Fmediaplayer.git diff --git a/app/MediaPlayer.qml b/app/MediaPlayer.qml index a937bc6..6f8d0db 100644 --- a/app/MediaPlayer.qml +++ b/app/MediaPlayer.qml @@ -19,111 +19,169 @@ import QtQuick.Layouts 1.1 import QtQuick.Controls 2.0 import QtMultimedia 5.6 import AGL.Demo.Controls 1.0 -import MediaPlayer 1.0 ApplicationWindow { id: root Item { - id: bluetooth - property bool connected: false - property string state + id: player + + property string title: "" + property string album: "" + property string artist: "" - property string artist - property string title property int duration: 0 property int position: 0 - function disableBluetooth() { - bluetooth.artist = '' - bluetooth.title = '' - bluetooth.duration = 0 - bluetooth.position = 0 - bluetooth.connected = false + property string cover_art: "" + property string status: "" + + function time2str(value) { + return Qt.formatTime(new Date(value), 'mm:ss') } } - Connections { - target: dbus + Item { + id: bluetooth - onStopPlayback: { - player.stop() - playlist.clear() - playlistmodel.setSource(playlist) - playlistview.visible = false - } + property string deviceAddress: "" + property bool connected: false + property bool av_connected: false + + property int position: 0 + property int duration: 0 - onProcessPlaylistUpdate: { - playlist.clear() - playlist.addItems(mediaFiles) + property string artist: "" + property string title: "" + property string state: "stopped" - playlistmodel.setSource(playlist) - playlistview.visible = bluetooth.connected == false - } + // AVRCP Target UUID + property string avrcp_uuid: "0000110e-0000-1000-8000-00805f9b34fb" - onProcessPlaylistHide: { - playlistview.visible = false - player.stop() + function connect_profiles() { + var address = bluetooth.deviceAddress; + bluetooth_connection.connect(address, "a2dp") + bluetooth_connection.connect(address, "avrcp") } - onProcessPlaylistShow: { - playlistview.visible = true - bluetooth.disableBluetooth() + function disconnect_profiles() { + var address = bluetooth.deviceAddress; + bluetooth_connection.disconnect(address, "a2dp") + bluetooth_connection.disconnect(address, "avrcp") } - onDisplayBluetoothMetadata: { - if (avrcp_artist) - bluetooth.artist = avrcp_artist - if (avrcp_title) - bluetooth.title = avrcp_title - bluetooth.duration = avrcp_duration + function set_avrcp_controls(cmd) { + bluetooth_connection.set_avrcp_controls(bluetooth.deviceAddress, cmd) } + } - onUpdatePlayerStatus: { - bluetooth.connected = true - bluetooth.state = status + Connections { + target: bluetooth_connection + + onDeviceListEvent: { + var address = "" + for (var i = 0; i < data.list.length; i++) { + var item = data.list[i] + if (item.Connected == "True" && item.UUIDs.indexOf(bluetooth.avrcp_uuid) >= 0) { + address = item.Address + + bluetooth.connected = true + mediaplayer.pause() + + //NOTE: This hack is here for when MediaPlayer is started + // with an existing connection. + bluetooth.av_connected = item.AVPConnected == "True" + } + } + if (!address) + bluetooth.connected = false + else + bluetooth.deviceAddress = address } - onUpdatePosition: { - slider.value = current_position - bluetooth.position = current_position + onDeviceUpdatedEvent: { + var metadata = data.Metadata + + if (data.Connected == "False") + return + + bluetooth.connected = data.Connected == "True" + bluetooth.av_connected = data.AVPConnected == "True" + bluetooth.deviceAddress = data.Address + + if ('Position' in metadata) + bluetooth.position = metadata.Position + + if ('Duration' in metadata) + bluetooth.duration = metadata.Duration + + if ('Status' in metadata) + bluetooth.state = metadata.Status + + if ('Artist' in metadata) + bluetooth.artist = metadata.Artist + + if ('Title' in metadata) + bluetooth.title = metadata.Title } } - MediaPlayer { - id: player - audioRole: MediaPlayer.MusicRole - autoLoad: true - playlist: playlist + Connections { + target: mediaplayer - property bool is_bluetooth: false - function time2str(value) { - return Qt.formatTime(new Date(value), 'mm:ss') + onPlaylistChanged: { + playlist_model.clear(); + + for (var i = 0; i < playlist.list.length; i++) { + var item = playlist.list[i] + + playlist_model.append({ "index": item.index, "artist": item.artist ? item.artist : '', "title": item.title ? item.title : '' }) + + if (item.selected) { + playlistview.currentIndex = i + } + } + } + + onMetadataChanged: { + player.title = metadata.title + player.album = metadata.album + player.artist = metadata.artist + + if (metadata.duration) { + player.duration = metadata.duration + } + + if (metadata.position) { + player.position = metadata.position + } + + if (metadata.status) { + player.status = metadata.status + } + + if (metadata.image) { + player.cover_art = metadata.image + } + + playlistview.currentIndex = metadata.index } - onPositionChanged: slider.value = player.position } Timer { id: timer interval: 250 - running: (bluetooth.connected && bluetooth.state == "playing") + running: (bluetooth.av_connected && bluetooth.state == "playing") repeat: true onTriggered: { - bluetooth.position = dbus.getCurrentPosition() - slider.value = bluetooth.position + bluetooth.position = bluetooth.position + 250 } } - Playlist { - id: playlist - playbackMode: random.checked ? Playlist.Random : loop.checked ? Playlist.Loop : Playlist.Sequential - - Component.onCompleted: { - playlist.addItems(mediaFiles) - } + ListModel { + id: playlist_model } - ColumnLayout { anchors.fill: parent Item { @@ -138,8 +196,8 @@ ApplicationWindow { anchors.bottom: parent.bottom height: sourceSize.height * width / sourceSize.width fillMode: Image.PreserveAspectCrop - source: player.metaData.coverArtImage ? player.metaData.coverArtImage : '' - visible: bluetooth.connected == false + source: player.cover_art ? player.cover_art : '' + visible: bluetooth.av_connected == false } Item { @@ -161,17 +219,19 @@ ApplicationWindow { Layout.fillHeight: true Row { spacing: 20 - ToggleButton { - id: random - visible: bluetooth.connected == false - 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: bluetooth.connected == false + //checked: player.loop_state offImage: './images/AGL_MediaPlayer_Loop_Inactive.svg' onImage: './images/AGL_MediaPlayer_Loop_Active.svg' + onClicked: { mediaplayer.loop(checked) } } } ColumnLayout { @@ -179,13 +239,13 @@ ApplicationWindow { Label { id: title Layout.alignment: Layout.Center - text: bluetooth.title ? bluetooth.title : (player.metaData.title ? player.metaData.title : '') + text: bluetooth.av_connected ? bluetooth.title : (player.title ? player.title : '') horizontalAlignment: Label.AlignHCenter verticalAlignment: Label.AlignVCenter } Label { Layout.alignment: Layout.Center - text: bluetooth.artist ? bluetooth.artist : (player.metaData.contributingArtist ? player.metaData.contributingArtist : '') + text: bluetooth.av_connected ? bluetooth.artist : (player.artist ? player.artist : '') horizontalAlignment: Label.AlignHCenter verticalAlignment: Label.AlignVCenter font.pixelSize: title.font.pixelSize * 0.6 @@ -195,10 +255,11 @@ ApplicationWindow { Slider { id: slider Layout.fillWidth: true - to: bluetooth.connected ? bluetooth.duration : player.duration - enabled: bluetooth.connected == false + to: bluetooth.av_connected ? bluetooth.duration : player.duration + enabled: bluetooth.av_connected == false + value: bluetooth.av_connected ? bluetooth.position : player.position function getPosition() { - if (bluetooth.connected && bluetooth.position) { + if (bluetooth.av_connected) { return player.time2str(bluetooth.position) } return player.time2str(player.position) @@ -215,9 +276,9 @@ ApplicationWindow { anchors.right: parent.right anchors.bottom: parent.top font.pixelSize: 32 - text: bluetooth.connected ? player.time2str(bluetooth.duration) : player.time2str(player.duration) + text: bluetooth.av_connected ? player.time2str(bluetooth.duration) : player.time2str(player.duration) } - onPressedChanged: player.seek(value) + onPressedChanged: mediaplayer.seek(value) } RowLayout { Layout.fillHeight: true @@ -232,10 +293,11 @@ ApplicationWindow { id: previous offImage: './images/AGL_MediaPlayer_BackArrow.svg' onClicked: { - if (bluetooth.connected) { - dbus.processQMLEvent("Previous") + if (bluetooth.av_connected) { + bluetooth.set_avrcp_controls("Previous") + bluetooth.position = 0 } else { - playlist.previous() + mediaplayer.previous() } } } @@ -243,27 +305,30 @@ ApplicationWindow { id: play offImage: './images/AGL_MediaPlayer_Player_Play.svg' onClicked: { - if (bluetooth.connected) { - dbus.processQMLEvent("Play") + if (bluetooth.av_connected) { + bluetooth.set_avrcp_controls("Play") } else { - player.play() + mediaplayer.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: { + player.status = "" + mediaplayer.pause() + } } }, State { - when: bluetooth.connected && bluetooth.state == "playing" + when: bluetooth.av_connected && bluetooth.state == "playing" PropertyChanges { target: play offImage: './images/AGL_MediaPlayer_Player_Pause.svg' - onClicked: dbus.processQMLEvent("Pause") + onClicked: bluetooth.set_avrcp_controls("Pause") } } @@ -273,21 +338,30 @@ ApplicationWindow { id: forward offImage: './images/AGL_MediaPlayer_ForwardArrow.svg' onClicked: { - if (bluetooth.connected) { - dbus.processQMLEvent("Next") + if (bluetooth.av_connected) { + bluetooth.set_avrcp_controls("Next") } else { - playlist.next() + mediaplayer.next() } } } Item { Layout.fillWidth: true } -// Image { -// source: './images/AGL_MediaPlayer_Bluetooth_Inactive.svg' -// } -// Image { -// source: './images/AGL_MediaPlayer_Radio_Inactive.svg' -// } + + ToggleButton { + visible: bluetooth.connected + checked: bluetooth.av_connected + offImage: './images/AGL_MediaPlayer_Bluetooth_Inactive.svg' + onImage: './images/AGL_MediaPlayer_Bluetooth_Active.svg' + + onClicked: { + if (bluetooth.av_connected) { + bluetooth.disconnect_profiles() + } else { + bluetooth.connect_profiles() + } + } + } } } } @@ -297,22 +371,18 @@ ApplicationWindow { Layout.fillHeight: true Layout.preferredHeight: 407 - PlaylistWithMetadata { - id: playlistmodel - source: playlist - } - ListView { anchors.fill: parent id: playlistview + visible: bluetooth.av_connected == false clip: true header: Label { x: 50 text: 'PLAYLIST' opacity: 0.5 } - model: playlistmodel - currentIndex: playlist.currentIndex + model: playlist_model + currentIndex: -1 delegate: MouseArea { id: delegate @@ -322,12 +392,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 { @@ -337,19 +401,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.get(index).index) } }