Improved playlist with random and normal play..

Posted By: Nidhogg

Improved playlist with random and normal play.. - 02/11/07 11:03

I hope I am allowed to post the code here. Anyway, here is my contribution to the GS community...

//playlist.wdl
/* Improved version of my first PLAYLIST.M3U reading template.
I have tested this template with the 3DGS A65.06 Pro version so I don't know
if it will work with earlier version hmmm OK..
I also cannot take full credit for bits of this code as I
did have some help from people who where kind enough to give me
ideas and even bits of working code to use, Also a afew bits from other
tutorial code. So credit to where credit is due and a big thankyou....

This template should work with most playlists maybe change
the code in the Check_For_Music function part.

I am aslo working on a DLL version but it will have more features...
Will take a while though as I am only still a youngun when it comes
to C++ lol...

The only thing I ask for in return is a small credit in any 3DGS release thanks...

Kindest regards Andrew Sengstock..
Alias Nidhogg....
*/


string rfile_str[256]; //For Random Play
string sfile_str[256]; //For Normal Play

var slot; //Used in Random function
var splay = 0; //Used in Normal Play
var rplay = 1; //Used in Random Play
var filehandle; //A file handle for loading and reading
var muhandle; //Handle used to detect if media is playing
var randlist; //Another Random Play var
var DontPlayMusic = 0; //Don't play media if not correct playlist format
/////////////////////////////////////////////////////////////////

FONT song_font , <Arial10.bmp>, 10, 16;

text song_txt // text object for displaying our strings
{
font = song_font; // standard font
pos_x = 10; // text begins at (10, 10) screen position (in pixels)
pos_y = 26;
}
/////////////////////////////////////////////////////////////////

text stay_txt // text object for displaying our strings
{
font = song_font; // standard font
pos_x = 10; // text begins at (10, 10) screen position (in pixels)
pos_y = 12;
}
/////////////////////////////////////////////////////////////////

function songprint(str) // display a string parameter on the screen
{
song_txt.string = str;
song_txt.visible = on;
Wait(100); //Stay on screen this long...
song_txt.visible = off;
}

function status_print(str) // display a string parameter on the screen
{
stay_txt.string = str;
stay_txt.visible = on;
Wait(100); //Stay on screen this long...
stay_txt.visible = off;
}
/////////////////////////////////////////////////////////////////

function song_show()
{
//screen_color.blue = 128; // set a dark blue background
if (splay == 0)
{
songprint(sfile_str); // display the string
}
else
{
songprint(rfile_str); // display the string
}
}

/////////////////////////////////////////////////////////////////

function Open_Playlist()
{
//Opens playlist the playlist file for reading...
filehandle = file_open_game("playlist.m3u");
}
/////////////////////////////////////////////////////////////////

function Close_Playlist()
{
//Closes the playlist file...
file_close(filehandle);
}
/////////////////////////////////////////////////////////////////

Function Check_For_Music()
{
Open_Playlist();
file_str_read(filehandle,sfile_str);
if str_cmp(sfile_str,"#EXTM3U") == 0
{
DontPlayMusic = 1;
status_print(" Invalid M3U playlist, Exiting program!");
sleep(5);
sys_exit(" ");
};
Close_Playlist();
}
/////////////////////////////////////////////////////////////////

function change_mode() //Changes between Random and Normal play via assigned ON_F2 Key
{ //eg: ON_F2 change_mode();
If (media_playing(muhandle) == 1)//If media is playing
{
media_stop(muhandle);//Stop media
muhandle = 0;//Reset var back to 0
}

if (splay == 0) //If we are in Normal Play mode
{
splay = 1; //Change var to 1
rplay = 0;//Ok now we go to Random Play
}
else
{
splay = 0; //If we are Random
rplay = 1; //Go back to Normal
}
}
/////////////////////////////////////////////////////////////////

function Random_play()
{
rplay = 0;
splay = 1;

status_print("Random Play");

If (media_playing(muhandle) == 1)
{
media_stop(muhandle);
muhandle = 0;
}

If (media_playing(muhandle) == 0)//If no media is playing
{
randomize(); //Start random
slot = 0; //Set slot to 0
randlist = 0;//Set the list to zero
Open_Playlist();
randlist = int(random(243));//Set this to how many actual MP3's in your playlist file. Not sure what the MAX would be..
while (slot != randlist) //Start the Loop
{
freeze_mode = 1; //Stop graphics, You don't have to have this..
file_str_read(filehandle,rfile_str);//Start reading individual lines in the playlist..
if (str_cmpni(rfile_str,"#") == 0)//Check if it's an actual file or path to file. If Not
{
file_str_read(filehandle,rfile_str);//Read the next line..
slot += 1;//Ok add it to the counter.
}
}//Keep looping until the slot matches the random randlist number..
// Close_Playlist(); Leave commented out..
wait(1);//Wait 1 frame or else we can get an error..
media_play(rfile_str,null,100);// Start playing the chosen Random file..
muhandle = media_handle;//Assign our error handle for media_play
songprint(rfile_str);//Horrible version of what file is playing, Until I finish the DLL..
freeze_mode = 0;//Ok now start the graphics again..
}
}
/////////////////////////////////////////////////////////////////
function Single_Play()// Pretty much the same as random play, But does Line for Line play of files..
{
splay = 0;
rplay = 1;

status_print("Normal Play");

If (media_playing(muhandle) == 1)
{
media_stop(muhandle);
muhandle = 0;
}

If (media_playing(muhandle) == 0)
{
file_str_read(filehandle,sfile_str);
while (str_cmpni(sfile_str,"#") == 0)
{
freeze_mode = 1;
file_str_read(filehandle,sfile_str);
wait(1);
if (str_len(sfile_str) == 0) //At the end of the list so we
{ //Skip back to beginning by closing and reopening the playlist file.
Close_Playlist();
wait(1);
Open_Playlist();
wait(1);
}
else
{
media_play(sfile_str,null,100);
muhandle = media_handle;
songprint(sfile_str);
freeze_mode = 0;
break; //Need to put a break out of loop here, Otherwise it wouldn't stop looping.
}
}
}

}
/////////////////////////////////////////////////////////////////

function Get_songs()
{
Open_Playlist();

while(1)
{
if (splay == 0) //Are we Normal Playing
{
splay = 0;
rplay = 1;
If (media_playing(muhandle) == 0)
{
Single_Play();
}
}
if (rplay == 0) //Or Random Playing
{
splay = 1;
rplay = 0;
If (media_playing(muhandle) == 0)
{
// Close_Playlist();
Random_Play();
}
}
wait(1);
}
}
////////////////////////////////////////////////////////////////////

function paused() //Pause the program via an ON_F? assigned key.
{
media_pause(muhandle);
freeze_mode = 1;
wait(1);
}
////////////////////////////////////////////////////////////////////

function restart() //As above but restart from where it paused..
{
media_start(muhandle);
freeze_mode = 0;
wait(1);
}
////////////////////////////////////////////////////////////////////

function Skip_song()//Don't like the song playing, Lets skip it, via an ON_K? assigned key
If (media_playing(muhandle) == 1)
{
media_stop(muhandle);
muhandle = 0;
wait(1);
}
////////////////////////////////////////////////////////////////////

Enjoy, Oh and if you wouldn't mind, A comment wouldn't go astray.
Posted By: alphaindigo

Re: Improved playlist with random and normal play. - 02/11/07 14:47

yeah food stuff, i would have posted it in user contributions, this forum is for discussing the templates that come with gamestudio. still a good contrib though!
Posted By: Nidhogg

Re: Improved playlist with random and normal play. - 02/14/07 06:53

Thankyou for moving it.. I'll remember in the future..
Posted By: sheefo

Re: Improved playlist with random and normal play. - 02/14/07 14:11

I could use this for background music in my RTS game . Thanks for contributing. I will credit you.
Posted By: xeno2k4ever

Re: Improved playlist with random and normal play. - 08/01/08 01:25

can this be used in a7?
Posted By: Nidhogg

Re: Improved playlist with random and normal play. - 08/01/08 07:50


I did a rebuild with A7.10.1 and it worked for like 3 to maybe 4 hours then it crashed. So a few very minor change in a couple of places should solve this.

I haven't the time at the moment to do it as I am working on a bigger project.
Posted By: frazzle

Re: Improved playlist with random and normal play. - 08/01/08 15:47

This looks like a pretty handy UC Nidhogg smile smile
Btw, it would have been handy/more attractive if you had made a demo level of it and zipped it wink wink

Cheers

Frazzle
Posted By: Nidhogg

Re: Improved playlist with random and normal play. - 08/01/08 16:42

I did wink And thanks for the comment.
© 2024 lite-C Forums