[Tutorial] How to create a music player [ZCMD]
#1

Creating a music system using 0.3d's PlayAudioStreamForPlayer

What are we going to learn at the end of this?
Your going to have learned how to create a dialog that shows a list of songs and how you can add more to that dialog if you want to at a later date.

What will I need?
All you need is ZCMD for the command that the players will type to display the music players dialog.

So Here We go!

Step One: Creating the dialog define

We need to make our definitions!
For the first part we will #define DIALOG_MUSICPLAYER

So do it like this at the top of your script anywhere you wish, if you already have dialogs, Great! do it there.
pawn Code:
//Anywhere at the top of the script, do not put it in a callback!

#define DIALOG_MUSICPLAYER  1
//This creates a dialog definition which we will use when we ShowPlayerDialog later.


Step Two: Creating the command

For this we will be using ZCMD so make sure you have ZCMD if you want this to work!

pawn Code:
CMD:radio(playerid, params[])
{
    if(!IsPlayerInAnyVehicle) return SendClientMessage(playerid, -1, "You need to be in a vehicle to use this command!"); // This is used if you only want people to play the song when their in a vehicle
    ShowPlayerDialog(playerid, DIALOG_MUSICPLAYER, DIALOG_STYLE_LIST, "Music Player", "Song 1\nSong 2\nSong 3", "Play", "Close"); //This shows the dialog, notice we used DIALOG_MUSICPLAYER for the dialogid!
    return 1;
}
Now let me explain this a little bit more, what the first line does ( if(!IsPlayerInAnyVehicle) ) checks if the player is in a vehicle, if he isn't they will be sent a message that tells them, they need to be in a vehicle to use this command.

If you would like to use the command without being in a vehicle, just remove that line.

ShowPlayerDialog, this is where we show the player the box with the list of songs. If you look at the 5th field ( The Message ) you will notice it says.
pawn Code:
Song 1\n Song2\n Song 3
What does \n mean? \n is used to go to the next line, as we are using a list box that shows a list of songs, each song goes onto a new line.
You do not need to name them like I did, Song 1 Song 2 etc. You can name them what ever you like! Just make sure to keep the '\n' there!



Step Three: What to do if the player picks a song

So. We have created the dialog, with the names of the songs but what do we do if a player picks a song?

Under OnDialogResponse we need to put this. Which I will explain after.

pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == DIALOG_MUSICPLAYER) // This checks to see if the dialog the players viewing is the music player one
    {
          if(response) // If the player clicked a song
          {
               if(listitem == 0) // Since this is a list, we check if they clicked the first song on the list.
               {
                       PlayAudioStreamForPlayer(playerid, "URL"); // Play the song
               }
               if(listitem == 1) // We now check if the player clicked the second song
               {
                       PlayAudioStreamForPlayer(playerid, "URL"); // Play the song
               }
               if(listitem == 2) // or the third song.
               {
                       PlayAudioStreamForPlayer(playerid, "URL"); // Play the song
               }
           }
      }
    return 1;
}
So what are we doing here? First of,
pawn Code:
if(dialogid == DIALOG_MUSICPLAYER)
This checks to see if the player is viewing the MusicPlayer dialog.

pawn Code:
if(response)
This is a vital part of the dialog, this checks if the player has clicked something on the dialog.

pawn Code:
if(listitem == 0)
Now here is where we find what song they picked, you may think that the list starts at 1 since its the first thing. That's wrong. List items always start at 0
So this checks if the player clicked the first song, and if they did it plays the song, same with the other listitems, it checks if they picked another song instead.

pawn Code:
PlayAudioStreamForPlayer(playerid, "URL");
This is where we play the song for the player, where it says "URL" you will need to change that to the song you wish to play, if you do not have a URL, but have the MP3 file. You can find sites all over the place that you can upload an MP3 file too, then copy the link and paste it in where it says "URL". Remembering to keep " " around the URL, or it won't work.

Here is an example of how to use PlayAudioStreamForPlayer with a valid URL.
pawn Code:
PlayAudioStreamForPlayer(playerid, "http://www.yourwebsite.com/playsong.mp3");

Step Four: Adding more songs if you want too.
Well all you have to do is go back to the radio command, and where it says for example I will use this tutorials songs.

pawn Code:
Song 1\nSong 2\nSong 3
If you wish to add another song, all you have to do is \n for a new line, never forget that! and the name of the song you wish to add, here's an example

pawn Code:
Song 1\nSong 2\nSong 3\nSong 4
Don't forget though! You need to go back to OnDialogResponse and add the new listitem or it won't play!

And your done!

I hope this tutorial was helpful and I'm always open for suggestions in the comments to anything I could improve in the tutorial, if I did manage to help ya. Please, please rep me, thanks!

I'd like to thank Doreto for providing this link, this link allows you to upload your MP3 files, then you can copy the URL into your PlayAudioStreamForPlayer

Kiwi 6

Reply
#2

At the "URL", show any example what to put. Anyone might not know what to put there. An example would make it clear.

Nice tutorial though.
Reply
#3

Quote:
Originally Posted by rinori
View Post
At the "URL", show any example what to put. Anyone might not know what to put there. An example would make it clear.

Nice tutorial though.
Thanks and good, suggestion. I've just added it
Reply
#4

nice tutorial good work
Reply
#5

Very Nice tutorial, Good Job!
I would like to know, how I am able to upload songs. Like the example you gave. I asked you because I don't know it and I want to learn it.
Reply
#6

Quote:
Originally Posted by Dwane
View Post
Very Nice tutorial, Good Job!
I would like to know, how I am able to upload songs. Like the example you gave. I asked you because I don't know it and I want to learn it.
you can use
PHP Code:
www.kiwi6.com (****** search
here example here or you can use online radio links
Reply
#7

Quote:
Originally Posted by doreto
View Post
you can use
PHP Code:
www.kiwi6.com (****** search
here example here or you can use online radio links
Wow! Thank you very much.
One more question about music player in samp. Is there a chance to lag with it or not?
Reply
#8

Quote:
Originally Posted by Dwane
View Post
Wow! Thank you very much.
One more question about music player in samp. Is there a chance to lag with it or not?
Well the SAMP development team have no doubt done all they can to reduce lagg when streaming something, but I've never came across any lag and thanks doreto for providing that link, i'll add it to the tutorial!
Reply
#9

Quote:
Originally Posted by LiamM
View Post
Well the SAMP development team have no doubt done all they can to reduce lagg when streaming something, but I've never came across any lag
Well, okay I will try create it based on this tutorial. Thank you! + Rep
Reply
#10

If theres anything I can improve, please post a comment and I'll edit the tut
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)