summaryrefslogtreecommitdiffstats
path: root/src/js/player.js
blob: 5efdc2cc16b0181119ec08c74c8a3651e7f2299b (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
import { load as load_template } from './templates';
import Mustache from 'mustache';

import { mediaplayer } from 'agl-js-api';

var template;
var root;
var page = {
    metadata: {},
    isLooping: false,
    getPosition: function() {
        if ( this.metadata && this.metadata.position ) {
            return formatTime(this.metadata.position);
        } else {
            return '--:--';
        }
    },
    getDuration: function(){
        if ( this.metadata && this.metadata.track && this.metadata.track.duration ) {
            return formatTime(this.metadata.track.duration);
        } else {
            return '--:--';
        }
    },
    getPercentage: function() {
        if ( this.metadata && this.metadata.position && this.metadata.track && this.metadata.track.duration) {
            return Math.floor((this.metadata.position/this.metadata.track.duration)*100);
        }
    },
    isPlaying: function() {
        return this.metadata && this.metadata.status === 'playing';
    }
};

function twoDigit(number) {
    if( number < 10 ) {
        return '0'+number;
    } else {
        return number;
    }
}

function formatTime(time) {
    return twoDigit(Math.floor(time/60000))+':'+twoDigit(Math.floor((time/1000)%60));
}

export function show() {
    root.innerHTML = Mustache.render(template, page);
}

export function init(node) {

    mediaplayer.on_metadata_changes(function(metadata){
        console.log(metadata);
        page.metadata = metadata;
        show();
    });
    load_template('player.template.html').then(function(result) {
        template = result;
        root = node;
        Mustache.parse(template);
        show();
    }, function(error) {
        console.error('ERRROR loading main template', error);
    });
}

export function loop() {
    if( !page.isLooping ) {
        mediaplayer.loop('track').then(function() {
            page.isLooping = true;
            show();
        });
    } else {
        mediaplayer.loop('playlist').then(function() {
            page.isLooping = false;
            show();
        });
    }
}

export function play(trackIndex) {
    if( trackIndex ) {
        mediaplayer.pickTrack(trackIndex);
    } else {
        mediaplayer.play();
    }
}

export function pause() {
    mediaplayer.pause();
}

export function previous() {
    mediaplayer.previous();
}

export function next() {
    mediaplayer.next();
}