-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
84 lines (73 loc) · 2.67 KB
/
controller.js
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
window.onload = (function () {
var audio = new Audio();
function searchTracks(query) {
$.ajax({
url: 'https://api.spotify.com/v1/search',
data: {
q: query,
type: 'track'
},
success: function (response) {
if (response.tracks.items.length) {
var track = response.tracks.items[0];
audio.src = track.preview_url;
audio.play();
communicateAction('<h2>Playing ' + track.name + ' by ' + track.artists[0].name + '</h2><img width="200" src="' + track.album.images[1].url + '">');
}
}
});
}
function playSong(songName, artistName) {
var query = songName;
if (artistName) {
query += ' artist:' + artistName;
}
searchTracks(query);
}
function communicateAction(text) {
var rec = document.getElementById('conversation');
rec.innerHTML += '<div class="action">' + text + '</div>';
}
function recognized(text) {
var rec = document.getElementById('conversation');
rec.innerHTML = '<div class="recognized"><div>'+ '\"' + text + '\"' + '</div></div>';
}
//alert('before annyang');
if (annyang) {
//alert('after annyang');
// Let's define our first command. First the text we expect, and then the function it should call
var commands = {
'stop': function () {
audio.pause();
},
'play track *song': function (song) {
recognized('Play track ' + song);
playSong(song);
},
'play *song by *artist': function (song, artist) {
recognized('Play song ' + song + ' by ' + artist);
playSong(song, artist);
},
'play song *song': function (song) {
recognized('Play song ' + song);
playSong(song);
},
'play *song': function (song) {
recognized('Play ' + song);
playSong(song);
},
':nomatch': function (message) {
recognized(message);
communicateAction('Sorry, I don\'t understand this action');
}
};
// Add our commands to annyang
annyang.addCommands(commands);
// Start listening. You can call this here, or attach this call to an event, button, etc.
annyang.start();
}
annyang.addCallback('error', function () {
//communicateAction('error');
});
//playSong("Play Love Someone");
})();