-
Notifications
You must be signed in to change notification settings - Fork 0
/
jamendo.html
119 lines (106 loc) · 3.48 KB
/
jamendo.html
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html>
<head>
<title>Jamendo Web API Example</title>
<script src="https://unpkg.com/tone"></script>
</head>
<body>
<h1>Jamendo Web API Example V11</h1>
<button onclick="authenticate()">Authenticate with Jamendo</button>
<button onclick="fetchRandomTrack()">Fetch Random Track</button>
<button id="playButton">Play Song A</button>
<script>
const clientId = '69073f3b'; // Replace with your Jamendo API client ID
const redirectUri = 'https://eliasds.github.io/Binaural-Learning/redirect.html'; // Replace with your redirect URI
const responseType = 'code';
const scope = 'basic';
const callbackFunctionName = 'myCallback';
const trackId = 2026253; // Replace with the ID of the track you want to retrieve
function authenticate() {
const authUrl = `https://api.jamendo.com/v3.0/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=${responseType}&scope=${scope}`;
window.location.href = authUrl;
}
// Function to fetch track information
function fetchTrackInfo(trackId) {
const apiUrl = `https://api.jamendo.com/v3.0/tracks/?client_id=${clientId}&id=${trackId}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.results.length > 0) {
const track = data.results[0];
const audioUrl = track.audio;
// Use the audio URL to download or stream the audio file
console.log('Audio URL:', audioUrl);
} else {
console.log('Track not found');
}
})
.catch(error => {
console.log('Error:', error);
});
}
function displayTrackInfo(track) {
if (track) {
console.log('Track:', track);
// Display the track information on your page
} else {
console.log('Track not found');
}
}
function playSong(audioUrl) {
if (audioUrl) {
const player = new Tone.Player(audioUrl).toDestination();
player.autostart = true;
} else {
console.log('Invalid audio URL');
}
}
// Fetch a random track and display its information
function fetchRandomTrack() {
const apiUrl = `https://api.jamendo.com/v3.0/tracks/?format=json&limit=1&order=random&client_id=${clientId}`;
return fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.results.length > 0) {
const track = data.results[0];
const trackId = track.id;
console.log('Track ID:', trackId);
displayTrackInfo(track);
return trackId;
} else {
console.log('No random tracks found');
return null;
}
})
.catch(error => {
console.log('Error:', error);
return null;
});
}
// Event listener for the play button
document.getElementById('playButton').addEventListener('click', function() {
fetchTrackInfo(trackId)
.then(audioUrl => {
playSong(audioUrl);
});
});
// Callback function to handle the response
function myCallback(response) {
console.log(response);
// Extract the song information from the response
const song = response.results[0];
console.log(song); // Log the song object to inspect its structure
const songUrl = song.audio;
// Play the song using Tone.js
const player = new Tone.Player(songUrl).toDestination();
player.autostart = true;
}
// Check if access token is present in the URL query parameters and save it in local storage
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
if (code) {
localStorage.setItem('code', code);
}
</script>
</body>
</html>