How to play music via Plex API from a web page

other-dev

#1

// Constants
var username = "";
var password = "";
var searchdata = "";
var plexProd = "";

// Global variables
var plexUser;
var plexServer;
var plexAccess;
var plexAddress;
var plexPort;
var plexSongs;

// Get User Info from Plex.tv vor username and password
$.ajax({
url: 'https://plex.tv/users/sign_in.json',
type: 'POST',
headers: { "X-Plex-Client-Identifier": "1",
"X-Plex-Product": plexProd,
"X-Plex-Version": "1.1.1"
},
data: 'user[login]=' + username + '&user[password]=' + password,
dataType: 'json',
success: function(response) {
plexUser = response.user;
},
error: function(xhr, ajaxOptions, thrownError) {
console.log('boo!');
console.log('Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' Ready State: ' + xhr.readyState + 'thrownError: ' + thrownError);
}
});

// Get Server Info (only looking for the 1st server)
$.ajax({
url: "https://plex.tv/pms/servers.xml",
type: "GET",
headers: { "X-Plex-Client-Identifier": "1",
"X-Plex-Product": plexProd,
"X-Plex-Version": "1.1.1",

"X-Plex-Token": plexUser.authToken
},
dataType: "xml",
success: function(response) {
plexServer = response.childNodes[0].children[0].attributes;
plexAccess = plexServer["accessToken"].nodeValue;
plexAddress = plexServer["address"].nodeValue;
plexPort = plexServer["port"].nodeValue;
},
error: function(xhr, ajaxOptions, thrownError) {
console.log('boo!');
console.log('Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' Ready State: ' + xhr.readyState + 'thrownError: ' + thrownError);
}
})

// Search for songs, albums, artists with searchdata
$.ajax({
url: "https://" + plexAddress + ":" + plexPort + "/search",
type: "GET",
headers: { "Accept": "application/json",
"X-Plex-Client-Identifier": "1",
"X-Plex-Product": plexProd,
"X-Plex-Version": "1.1.1",
"X-Plex-Token": plexAccess
},
data: "type=10&query=" + searchdata,
dataType: "json",
success: function(response) {
plexSongs = response.MediaContainer.Metadata;
},
error: function(xhr, ajaxOptions, thrownError) {
console.log('boo!');
console.log('Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' Ready State: ' + xhr.readyState + 'thrownError: ' + thrownError);
}
})

// From plexSongs, I can get the all the information about the song Key, Artist, Album, Title, ... etc

// But how do I get the Song to play on my local machine?