If you have not already, we suggest setting your Plex username to something else rather than email which is displayed on your posts in forum. You can change the username at https://app.plex.tv/desktop#!/account
Welcome to our forums! Please take a few moments to read through our Community Guidelines (also conveniently linked in the header at the top of each page). There, you'll find guidelines on conduct, tips on getting the help you may be searching for, and more!

How to play music via Plex API from a web page

Bob_the_SpacemanBob_the_Spaceman Posts: 1Members, Plex Pass Plex Pass

// 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?

Tagged:
Sign In or Register to comment.