[Tutorial] Making the most out of audio streaming! - Tips and tricks
#1

Basically this covers some cool things you can do with the "PlayAudioStreamForPlayer" feature.

pawn Code:
native PlayAudioStreamForPlayer(playerid, url[], Float:posX = 0.0, Float:posY = 0.0, Float:posZ = 0.0, Float:distance = 50.0, usepos = 0)
Tip 1: Using ****** translate/text to speech.

This may sound hard but it actually isn't. All you really need is the right URL addresses. Now let's break down ****** translate.

It's main URL is translate.******.com. This takes you to the general main page. The first "parameter" is the language code. This is basically two letters that correlate to a language.

For example, english would be "en". It is most likely the first letters of the language's name. After this comes the actual message being translated.

A URL saying "hello" in english would look like this,

http://translate.******.com/translate_tts?tl=en&q=hello

Now let's make a function to generate a URL. We'll make a stock function for this tutorial.

pawn Code:
stock GetTranslateURL(msg[], language[])
Now basically msg, and language are both strings. So using the stock would look like,

pawn Code:
new msg[64], language[3];
format(msg, sizeof(msg), "hello");
format(language, sizeof(language), "en");
GetTranslateURL(msg, language);
pawn Code:
stock GetTranslateURL(msg[], language[]="en")
If we do this the language will default to english and the parameter becomes optional.

pawn Code:
stock GetTranslateURL(msg[], language[]="en")
{
    new string[200]; // Declare a new string variable. We'll put 200 cells just to be safe incase the message is long.
    if(strlen(language) > 2) return -1; // The language should never be longer than 2 characters.
    format(string, sizeof(string), "http://translate.******.com/translate_tts?tl=%s&q=%s", language, msg);
    return string;
}
This would return the URL. Now to actually use it we have to get the URL and then play it as a stream. We'll make another stock to accomplish this.
pawn Code:
stock TranslateTextForPlayer(playerid, msg[], language[]="en")
{
     if(IsPlayerConnected(playerid)) // Always check if the player is connected before accessing any player functions.
     {
            if(strlen(GetTranslateURL(msg, language) > 1)
            {
                 new url[75];
                 format(url, sizeof(url), "%s", GetTranslateURL(msg, language); // Format the string using our stock.
                 PlayAudioStreamForPlayer(playerid, url);
                 return true;
             }
             else return 0;
     }
    else return INVALID_PLAYER_ID;
}
Our stock will return 1 if successful/valid input, false if GetTranslateURL is invalid and INVALID_PLAYER_ID if an invalid player id has been passed.

A list of language codes can be found below.

en - english
turkish - tr
crotian - hr
african - af
Danish - da
German - ga
Spanish - es
More can be used, how-ever I can't be bothered to list them all.

I plan on adding some more soon.
Reply
#2

You don't have to use format to copy a string, use strcat or strcpy instead. Secondly, there's no need to check if player is connected when you're using foreach. It adds in players to it's array when player connects.

Quote:
Originally Posted by Abagail
View Post
For this example, we'll make a function to stream a URL for online rcon administrators.

pawn Code:
stock PlayStreamForRCONAdmins(url[])
{
    foreach(Player, i)
    {

         if(IsPlayerConnected(i)) continue;
         if(IsPlayerAdmin(i)) continue;
         PlayAudioStreamForPlayer(i, url);
          return 1;
     }
}
Your loop is incorrect, it checks if player is connected and then skips if player is connected. Also in case if the player is logged in as RCON, it will also skip that player since they're logged in. Mistakes do happen though. Also, I don't see the variable "i" declared under foreach. I'm not sure how the latest foreach works, so please correct me if I'm wrong. Oh and returning 1 inside a loop will not get your function work the way you want, it will return 1 as soon after one single index is done.

EDIT : I just noted this:
Quote:
Originally Posted by Abagail
View Post
Basically, - this runs a loop. Then, it checks if the player is connected. If so, - the loop continues. Same thing with checking if the player is logged into RCON.
It's not how continue statement work. It skips and continues to the next loop if that's used. You can take a look at this topic, it has explained looping structures very well and I suggest you to read that.

https://sampforum.blast.hk/showthread.php?tid=305770
Reply
#3

I guess mistakes do happen. I've removed that part for now until I have time to re-write it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)