-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplejamendo.html
84 lines (72 loc) · 2.77 KB
/
simplejamendo.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
<!DOCTYPE html>
<html>
<head>
<title>Jamendo Web API Example</title>
<script src="https://unpkg.com/tone"></script>
</head>
<body>
<h1>Jamendo Web API Example V10</h1>
<button onclick="authenticate()">Authenticate with Jamendo</button>
<button onclick="fetchRandomSong()">Fetch Random Song</button>
<button onclick="fetchTrackInfo()">Fetch track info & Play</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() {
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);
// Play the track using Tone.js
const player = new Tone.Player(audioUrl).toDestination();
player.autostart = true;
} else {
console.log('Track not found');
}
})
.catch(error => {
console.log('Error:', error);
});
}
// Fetch a random song from Jamendo using JSONP
function fetchRandomSong() {
const apiUrl = `https://api.jamendo.com/v3.0/tracks/?format=json&limit=1&order=random&client_id=${clientId}&callback=${callbackFunctionName}`;
// Create a new script element to make the JSONP request
const script = document.createElement('script');
script.src = apiUrl;
document.head.appendChild(script);
}
// 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>