Commit d74a7202 authored by kempe's avatar kempe

Refactored Audio player

* Moved Audio element to its own file
* Made player controls its own component
* Refactored sleeptimer
parent 172e2f3a
......@@ -52,5 +52,9 @@ TRANSLATIONS += translations/harbour-received-sv.ts
HEADERS +=
DISTFILES += \
qml/components/js/Rad.js
qml/components/js/Rad.js \
qml/components/DockedAudioPlayer.qml
FORMS += \
qml/components/PlayerControls.qml.ui
This diff is collapsed.
import QtQuick 2.0
import Sailfish.Silica 1.0
import "./js/Storage.js" as DB
import "./js/Favorites.js" as FavoritesUtils
import "./js/Utils.js" as Utils
DockedPanel {
id: player
property var stationData: null;
property bool isFavorite: false
function play(station) {
if(!player.expanded)
player.open = true;
player.stationData = JSON.parse(JSON.stringify(station));
player.isFavorite = FavoritesUtils.isFavorite(stationData);
window.stationIcon = stationData.stationLogo;
stop();
audio.source = stationData.url;
audio.play();
}
function playNext() {
var nextStation = FavoritesUtils.getNextFavorite(stationData);
console.debug("Playing next favorite", JSON.stringify(nextStation));
play(nextStation);
}
function playPrev() {
var prevStation = FavoritesUtils.getPrevFavorite(stationData);
console.debug("Playing prev favorite", JSON.stringify(prevStation));
play(prevStation);
}
function stop() {
audio.stopAndReset();
}
function resume() {
audio.source = stationData.url;
audio.play();
}
function isPlaying() {
return audio.isPlaying();
}
function isLoading() {
audio.isLoading();
}
width: parent.width
height: Theme.itemSizeExtraLarge + Theme.paddingLarge
dock: Dock.Bottom
opacity: Qt.inputMethod.visible || !open ? 0.0 : 1.0
Behavior on opacity { FadeAnimation {duration: 300}}
SleepTimer {
id: sleepTimer
onSleepTriggered: player.stop();
}
PushUpMenu {
id: playerManu
MenuLabel {
text: qsTr("Sleep timer")
}
MenuLabelSmal {
id: sleepTimerLabel
visible: sleepTimer.running
text: Utils.secToTimeString(sleepTimer.progress);
}
MenuItem{
visible: sleepTimer.running
text: qsTr("Cancel")
onClicked: {
sleepTimer.stopTimer()
}
}
MenuItem {
visible: !sleepTimer.running
text: qsTr("Set")
onClicked: {
var sTime = DB.loadSleepTimer()
console.debug("DB HOUR: " + sTime.hour)
console.debug("DB MINUTE: " + sTime.minute)
var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", {
hour: sTime.hour,
minute: sTime.minute,
hourMode: DateTime.TwentyFourHours
})
dialog.accepted.connect(function() {
DB.updateSleepTimer(dialog.hour, dialog.minute)
var sec = (dialog.minute + (dialog.hour * 60)) * 60;
sleepTimer.startTimer(sec);
})
}
}
}
Item {
anchors.fill: parent
MouseArea {
id: opener
anchors.fill: parent
onClicked: console.debug("Show fullscreen player controls")
}
Row {
id: quickControlsItem
anchors.fill: parent
spacing: Theme.paddingLarge
Image {
id: stationIcon
sourceSize.height: parent.height
sourceSize.width: player.height
source: window.stationIcon
smooth: true
fillMode: Image.PreserveAspectFit
cache: true
}
Column {
id: trackInfo
width: parent.width - stationIcon.width - Theme.paddingLarge
height: parent.height
spacing: -Theme.paddingSmall
Label {
width: parent.width
truncationMode: TruncationMode.Fade
text: stationData ? stationData.name : ""
}
Label {
width: parent.width
font.pixelSize: Theme.fontSizeSmall
truncationMode: TruncationMode.Fade
color: Theme.secondaryColor
text: stationData ? stationData.genre : "";
}
ProgressBar {
id: progressBar
value: audio.bufferProgress
width: parent.width
visible: audio.isLoading()
Behavior on value {
NumberAnimation {}
}
}
PlayerControls {
id: controls
visible: !player.isLoading()
isPlaying: player.isPlaying()
isStared: player.isFavorite
buttonStar.onClicked: player.isFavorite = FavoritesUtils.toogleFavorite(stationData)
buttonPrevious.onClicked: playPrev()
buttonPlay.onClicked: player.isPlaying() ? stop() : resume()
buttonNext.onClicked: playNext()
}
}
}
}
AudioPlayer {
id: audio
}
}
import QtQuick 2.0
Row {
property bool isPlaying: false
property bool isStared: false
property alias buttonStar: buttonStar
property alias buttonPrevious: buttonPrevious
property alias buttonPlay: buttonPlay
property alias buttonNext: buttonNext
id: controls
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width
property real itemWidth: width / 4
IconButton {
id: buttonStar
width: itemWidth
anchors.verticalCenter: parent.verticalCenter
icon.source: isStared ? "image://theme/icon-m-favorite-selected" : "image://theme/icon-m-favorite"
}
IconButton {
id: buttonPrevious
width: itemWidth
anchors.verticalCenter: parent.verticalCenter
icon.source: "image://theme/icon-m-previous-song"
}
IconButton {
id: buttonPlay
width: itemWidth
anchors.verticalCenter: parent.verticalCenter
icon.source: isPlaying ? "image://theme/icon-m-pause" : "image://theme/icon-m-play"
}
IconButton {
id: buttonNext
width: itemWidth
anchors.verticalCenter: parent.verticalCenter
icon.source: "image://theme/icon-m-next-song"
}
}
......@@ -3,21 +3,23 @@ import QtQuick 2.0
Timer {
property int count: 0
property int sleepSec: 0
property int progress: sleepSec - count
signal sleepTriggered()
id: sleepTimer
running: false
repeat: true
onSleepTriggered: stopTimer()
onTriggered: {
count++;
if(count >= sleepSec) {
sleepTriggered()
stop()
}
}
function stopTimer() {
stop();
stop()
count = 0;
}
......
......@@ -19,6 +19,15 @@ function updateFavorites() {
}
}
function toogleFavorite(station) {
if(isFavorite(station))
removeFavorite(station)
else
addFavorite(station)
return isFavorite(station)
}
function removeFavorite(station) {
console.log("Removing favorite:", JSON.stringify(station))
DB.deleteStation(station)
......
......@@ -21,7 +21,7 @@ ApplicationWindow
id: favorites
}
AudioPlayer {
DockedAudioPlayer {
id: player
}
......
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1">
<context>
<name>AudioPlayer</name>
<message>
<source>Sleep timer</source>
<translation>insomningstimer</translation>
</message>
<message>
<source>Cancel</source>
<translation>Avbryt</translation>
</message>
<message>
<source>Set</source>
<translation>Ställ</translation>
</message>
</context>
<context>
<name>Browse</name>
<message>
......@@ -78,6 +63,21 @@
<translation>Visa spelare</translation>
</message>
</context>
<context>
<name>DockedAudioPlayer</name>
<message>
<source>Sleep timer</source>
<translation type="unfinished">insomningstimer</translation>
</message>
<message>
<source>Cancel</source>
<translation type="unfinished">Avbryt</translation>
</message>
<message>
<source>Set</source>
<translation type="unfinished">Ställ</translation>
</message>
</context>
<context>
<name>Favorites</name>
<message>
......
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1">
<context>
<name>AudioPlayer</name>
<message>
<source>Sleep timer</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cancel</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Set</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Browse</name>
<message>
......@@ -78,6 +63,21 @@
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>DockedAudioPlayer</name>
<message>
<source>Sleep timer</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cancel</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Set</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Favorites</name>
<message>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment