Web Hosting Talk







View Full Version : Sending info to Media Player through php


lexington
06-15-2008, 12:57 PM
I did a search and found a few topics discussing this but nothing worked. I know how to call up the song from a php file and have it play. However I would like to have the media player display info such as song title, author, etc. I read about using ASX and when I copied the way another script handles this by echoing the info in a php file it didn't work. I basically used this:


$band_id = intval($_GET['band_id']);
$song_id = intval($_GET['song_id']);
$type = htmlspecialchars(trim($_GET['type']));

$result = mysql_query("SELECT song_title FROM " .SONGS_TABLE. " WHERE song_id = '$song_id'");
$row = mysql_fetch_array($result);

$mp3_path = $user_dir_full_path . $band_id . '/songs/' . 'song_'.$type.'_' . $song_id . '.mp3';

if ( mysql_num_rows($result) && file_exists($mp3_path) )
{

header("Accept-Range: bytes");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0, false");
header("Cache-control: private");
header("Pragma: no-cache");
header('Content-type: audio/mpeg');
header("Content-Disposition: inline; filename=".$row['song_title']);

/*echo "<ASX version = \"3.0\">\n";
echo "<TITLE>".$row['song_title']."</TITLE>\n";
echo "<ENTRY>\n";
echo "<AUTHOR>Test</AUTHOR>\n";
echo "<COPYRIGHT>Copyright by test</COPYRIGHT>\n";
echo "<TITLE>Hello - ".$row['song_title']."</TITLE>\n";
echo "<REF HREF = \"".$site_url."play.php?band_id=$band_id&song_id=$song_id\"/>\n";
echo "</ENTRY>\n";
echo "</ASX>\n";*/

/*echo '<ASX version="3.0">
<Entry>
<ref href="'.$site_url.'play.php?band_id='.$band_id.'&song_id='.$song_id.'&type=hifi" />
</Entry>
</ASX>';*/


readfile($mp3_path);


}
else
{
echo "song not found";
}

I commented out my two examples of ASX because if either of them are in the windows media player displays an error. How can I make this work? Thanks.

Tree NC
06-15-2008, 04:07 PM
Your first ASX example looks fine. In order to display a song's artist, title, album, etc, use a <PARAM> tag. Below is an example of ASX that includes that info.

<ASX Version="3.0">
<AUTHOR>Name of content creator goes here</AUTHOR>
<TITLE>Album Title goes here</TITLE>
<PARAM name="Album" value="Album Title "/>
<PARAM name="Artist" value="Artist Name"/>
<PARAM name="Genre" value="Genre"/>
<ENTRY>
<REF HREF="song1.wma"/>
<AUTHOR>Creator's name</AUTHOR>
<COPYRIGHT>Copyright information</COPYRIGHT>
<TITLE>Song #1 title</TITLE>
</ENTRY>
<ENTRY>
<REF HREF="song2.wma"/>
<AUTHOR>Creator's name</AUTHOR>
<COPYRIGHT>Copyright information</COPYRIGHT>
<TITLE>Song #2 name</TITLE>
</ENTRY>
</ASX>

MSDN has a good reference (http://msdn.microsoft.com/en-us/library/aa914047.aspx) for ASX elements.

lexington
06-15-2008, 04:20 PM
Thanks :) I am only using one song file so I removed one of your entry links. The good news is that there are no more errors. The bad news is that the author, copyright, etc. and other links are empty in the player. Here is what I replaced the commented out code with:

echo '<ASX Version="3.0">
<AUTHOR>Name of content creator goes here</AUTHOR>
<TITLE>Album Title goes here</TITLE>
<PARAM name="Album" value="Album Title "/>
<PARAM name="Artist" value="Artist Name"/>
<PARAM name="Genre" value="Genre"/>
<ENTRY>
<REF HREF='.$site_url.'play.php?band_id='.$band_id.'&song_id='.$song_id.'&type=hifi"/>
<AUTHOR>Creators name</AUTHOR>
<COPYRIGHT>Copyright information</COPYRIGHT>
<TITLE>Song #1 title</TITLE>
</ENTRY>
</ASX>
';

lexington
06-15-2008, 04:36 PM
Update, I also want to add that the error still appears when playing in Internet Explorer.

vibrokatana
06-15-2008, 06:20 PM
Eh, ignore this post :\

foobic
06-15-2008, 07:26 PM
Thanks :) I am only using one song file so I removed one of your entry links. The good news is that there are no more errors. The bad news is that the author, copyright, etc. and other links are empty in the player. Here is what I replaced the commented out code with:

echo '<ASX Version="3.0">
<AUTHOR>Name of content creator goes here</AUTHOR>
<TITLE>Album Title goes here</TITLE>
<PARAM name="Album" value="Album Title "/>
<PARAM name="Artist" value="Artist Name"/>
<PARAM name="Genre" value="Genre"/>
<ENTRY>
<REF HREF='.$site_url.'play.php?band_id='.$band_id.'&song_id='.$song_id.'&type=hifi"/>
<AUTHOR>Creators name</AUTHOR>
<COPYRIGHT>Copyright information</COPYRIGHT>
<TITLE>Song #1 title</TITLE>
</ENTRY>
</ASX>
';
Looks like you may have missed a quote around the HREF value. Try downloading the entire file and checking it carefully with a text editor (or you could use the Firefox Live HTTP headers plugin, which will also let you check the headers you're sending out).

As a general rule, for multi-line strings containing quotes like this you'd find it easier to use a heredoc:
echo <<<ENDASX
<ASX Version="3.0">
<AUTHOR>Name of content creator goes here</AUTHOR>
<TITLE>Album Title goes here</TITLE>
<PARAM name="Album" value="Album Title "/>
<PARAM name="Artist" value="Artist Name"/>
<PARAM name="Genre" value="Genre"/>
<ENTRY>
<REF HREF="$site_url/play.php?band_id=$band_id&song_id=$song_id&type=hifi"/>
<AUTHOR>Creators name</AUTHOR>
<COPYRIGHT>Copyright information</COPYRIGHT>
<TITLE>Song #1 title</TITLE>
</ENTRY>
</ASX>
ENDASX;

lexington
06-16-2008, 12:23 AM
Thanks, when I used your code I got the error:

Parse error: syntax error, unexpected $end

Since I am not used to using ENDASX I am not sure how to fix it so I will see about fixing the missing quote (don't know how I missed that before) Thanks :)

EDIT

Still the same Media Player error with:


echo '<ASX Version="3.0">
<AUTHOR>Name of content creator goes here</AUTHOR>
<TITLE>Album Title goes here</TITLE>
<PARAM name="Album" value="Album Title "/>
<PARAM name="Artist" value="Artist Name"/>
<PARAM name="Genre" value="Genre"/>
<ENTRY>
<REF HREF="'.$site_url.'play.php?band_id='.$band_id.'&song_id='.$song_id.'&type=hifi"/>
<AUTHOR>Creators name</AUTHOR>
<COPYRIGHT>Copyright information</COPYRIGHT>
<TITLE>Song #1 title</TITLE>
</ENTRY>
</ASX>
';

It says:

Windows Media Player cannot play this file because the associated Windows Media metafile playlist is not valid

I wonder if perhaps the headers or the fact I am using readfile() function has something to do with it?

foobic
06-16-2008, 12:45 AM
Parse error: syntax error, unexpected $end
That usually means you have some extra characters after the "ENDASX;" - maybe just a space at the end of the line.

Windows Media Player cannot play this file because the associated Windows Media metafile playlist is not valid
You need to compare what you're sending with a valid file. Use the Firefox HTTP headers plugin to check your headers and download the file to compare its content.

lexington
06-16-2008, 12:48 AM
Thanks I installed the Firefox HTTP headers plugin but not sure how to use it? I just see an empty field where I am supposed to paste something in it?

foobic
06-16-2008, 12:59 AM
Start browsing, Grasshopper, and many things will be revealed. ;)

lexington
06-16-2008, 01:04 AM
Ok yeah I figured it out however it shows no info the for php file that pulls the MP3 file which is what I am having problems with.

lexington
06-16-2008, 04:21 AM
Does anyone know this cannot be that hard.

foobic
06-16-2008, 04:52 AM
Are you putting the url of the php file directly into your Firefox url bar? Remember you don't want to pass this file over to any media player - you want to download and view it.

lexington
06-16-2008, 05:21 AM
Are you putting the url of the php file directly into your Firefox url bar? Remember you don't want to pass this file over to any media player - you want to download and view it.

Yes I am.

"Remember you don't want to pass this file over to any media player"

I really do not understand what you mean. I used to use a crappy script named Jamroom and it simply used a php file and when you clicked on the link it directed to the mp3 file and displayed the song author and title in the media player. That is all I want to do.

lexington
06-16-2008, 06:41 PM
You know what I bet the other script author used getid3 to rewrite the contents of the MP3 file itself to make this work since I noticed when I uploaded a track that already contained the song author and album info it displayed on the player through the php file. So perhaps getid3 can rewrite the mp3 with info from the database. I just don't know how to integrate getid3 into the site since their info is vague.

foobic
06-16-2008, 07:18 PM
One last time: It makes no difference whether the file is static or created on the fly by php. The web server simply delivers headers and content to the client (Firefox, Media Player or anything else). The client doesn't (cannot) know what's happening inside the webserver.

So if you deliver the same file and headers via php as you do with a direct link to a real .mp3 file you will get the same result. Since you're not getting the same result, you're delivering something different. So look at what you're producing by php (headers and content) and compare it against a real mp3 file served by Apache directly - you'll probably see where you're going wrong.