[Include] AttachStreamToObject [attach easily audio streams to SA-MP objects]
#1

Introduction
I've seen many requests in the suggestions board this suggestion, I mean like alot of people suggested / requested it. it wasn't too hard to script, and I made it as an experiment, a simple experiment, however I thought someone would find it useful for their servers as long they edit it so it works better.

For example, if you are using scripted sirens for vehicles, you would know that they have no sound, but using the audio streams introduced in 0.3d, you can make that feature, you can make a jukebox object that has a radio stream attached, and other things you can come upon with.

Usage
Thanks to the (required) y_hooks include by ****** this is really easy to use. There are three functions:

pawn Code:
AttachStreamToObject( objectid, Float:range, url[] ); // create an attached audio stream for an object
StopObjectStream( objectid ); // destroys the attached audio stream
IsStreamAttachedToObject( objectid ); // returns 1 if an audio stream is attached to the objectid
Known bugs
They are listed in the include:
pawn Code:
/* * Known bugs
    --------------------------------------
        - There can be problems (or not) if you destroy the object before
          stopping the audio stream, use StopObjectStream prior destroying
          an object with attached stream.
         
        - The data is not reset if the stream is over. I recommend using this
          for streams that are very long, or not ending, like radio stations, or a timer to destroy the attached stream with the stream duration.

        - There is no support for the PlayAudioStreamForPlayer extra parameters
          like how the sound fades smoothly when you approach it.
    --------------------------------------*/
Download
pawn Code:
/*
    ATTACH SOUND TO OBJECT WRITTEN BY ADMANTIS
        CREDITS TO ****** FOR HIS FANTASTIC YSI LIBRARY AND FOREACH LIBRARY

    * Functions
    --------------------------------------
    AttachStreamToObject( objectid, range, url )
    INFO: Attach a stream to a object, so when a player approaches the object,
    it can be heard. For example, using the function in scripted sirens using
    0.3e objects, you can get sounds for the siren using the 0.3d
    PlayAudioStreamForPlayer function.

        * objectid: the object ID the stream will be attached.
        * range: meters you need to approach to the object to hear the stream.
        * url: valid .mp3, .ogg, icecast, shoutcast link for the audio stream.

    --------------------------------------
    StopObjectStream( objectid )
    INFO: Destroy the attached sound stream for that object.
        * objectid: the object ID the stream will be destroyed.
    --------------------------------------

    * Known bugs
    --------------------------------------
        - There can be problems (or not) if you destroy the object before
          stopping the audio stream, use StopObjectStream prior destroying
          an object with attached stream.
         
        - The data is not reset if the stream is over. I recommend using this
          for streams that are very long, or not ending, like radio stations, or a timer to destroy the attached stream with the stream duration.

        - There is no support for the PlayAudioStreamForPlayer extra parameters
          like how the sound fades smoothly when you approach it.
    --------------------------------------

    Do whatever you want with this, seriously, I did it because it was
    requested by many people in the suggestions board and it would be
    interesting for me to write it.
*/


#include <a_samp>
#include <foreach>
#include <YSI\y_hooks>

enum g_stream
{
    gStreamEnabled,
    gStreamURL[128],
    Float:gStreamRange
}

new gObjectStream[MAX_OBJECTS][g_stream];
new gActiveStreamObjectID[MAX_PLAYERS];

forward StreamCheckTimer( );

stock StopObjectStream( objectid )
{
    gObjectStream[objectid][gStreamURL] = "";
    gObjectStream[objectid][gStreamEnabled] = 0;
    gObjectStream[objectid][gStreamRange] = 0;
}

stock AttachStreamToObject( objectid, Float:range, url[] )
{
    if( !IsValidObject( objectid ) )
        return 0;

    format( gObjectStream[objectid][gStreamURL], 128, url );
    gObjectStream[objectid][gStreamEnabled] = 1;
    gObjectStream[objectid][gStreamRange] = range;
    return 1;
}

stock IsStreamAttachedToObject( objectid ) {
    return ( gObjectStream[objectid][gStreamEnabled] == 1 ) ? 1 : 0;
}

hook OnGameModeInit()
{
    SetTimer( "StreamCheckTimer", 1000, true );
    return 1;
}

public StreamCheckTimer( )
{
    new Float:objpos[3];
    foreach(Player, x)
    {
        for( new y = 0; y != MAX_OBJECTS; ++y )
        {
            if( 1 != gObjectStream[y][gStreamEnabled] )
                continue;
            else
            {
                GetObjectPos( y, objpos[0], objpos[1], objpos[2] );
                if( IsPlayerInRangeOfPoint( x, gObjectStream[y][gStreamRange], objpos[0], objpos[1], objpos[2] ) )
                {
                    if( gActiveStreamObjectID[x] != y )
                    {
                        gActiveStreamObjectID[x] = y;
                        PlayAudioStreamForPlayer( x, gObjectStream[y][gStreamURL] );
                    }
                   
                    if( gActiveStreamObjectID[x] == y && gStreamObject[y][gStreamEnabled] == 0 )
                    {
                        gActiveStreamObjectID[x] = 0;
                        StopAudioStreamForPlayer( x );
                    }
                }

                else
                {
                    if( gActiveStreamObjectID[x] == y ) // if is listening to a stream
                    {
                        gActiveStreamObjectID[x] = 0;
                        StopAudioStreamForPlayer( x );
                    }
                }
            }
        }
    }

    return 1;
}
You got it.
Reply
#2

sounds good gona test it +rep
Reply
#3

Sounds cool!
Reply
#4

Yup, looks cool, good job Admantis!
Reply
#5

What if the objects are attached to a car, do they move with the car?
Reply
#6

God idea!

You can make...
pawn Code:
stock PlayAudioInObject(objectid, url[], Float:range)
{
    static
        Float:iPosX,
        Float:iPosY,
        Float:iPosZ
    ;
   
    GetObjectPos(objectid, iPosX, iPosY, iPosZ); // get object position.
    PlayAudioStreamForPlayer(playerid, url,  iPosX, iPosY, iPosZ, range, true); // Player Audio in object position(iPosX, iPosY, iPosZ).
    return true;
}

Good Work!
Reply
#7

Quote:
Originally Posted by KyleSmith
View Post
What if the objects are attached to a car, do they move with the car?
Yes, the position of the sound isn't stored in an array, instead, it's checked using GetObjectPos and IsPlayerInRangeOfPoint, but you still need to be aware of the things I said in the main thread.q
Reply
#8

HELP! I try this:
pawn Code:
if(strcmp(cmdtext, "/vmusic", true) == 0)
{
    Object[playerid] = CreateObject(1239,0,0,0,0,0,0);
    new Float:VPos[3];
    GetVehiclePos(playerid,VPos[0],VPos[1],VPos[2]);
    AttachObjectToVehicle(Object[playerid], GetPlayerVehicleID(playerid), 0.409729004, 0.526367188, 0.206963539, 0, 0, 0);
    AttachStreamToObject(Object[playerid], 20.0, "url-what-ever");
    return 1;
}
But it doesn't works.
Reply
#9

C:\Users\Gharrett\Desktop\Rebel Gaming\pawno\include\AttachStreamToObject.inc(39) : error 010: invalid function or declaration
C:\Users\Gharrett\Desktop\Rebel Gaming\pawno\include\AttachStreamToObject.inc(42) : error 010: invalid function or declaration
C:\Users\Gharrett\Desktop\Rebel Gaming\pawno\include\AttachStreamToObject.inc(65) : error 001: expected token: ")", but found "["
C:\Users\Gharrett\Desktop\Rebel Gaming\pawno\include\AttachStreamToObject.inc(65) : error 029: invalid expression, assumed zero
C:\Users\Gharrett\Desktop\Rebel Gaming\pawno\include\AttachStreamToObject.inc(65) : warning 215: expression has no effect
C:\Users\Gharrett\Desktop\Rebel Gaming\pawno\include\AttachStreamToObject.inc(65) : error 001: expected token: ";", but found "]"
C:\Users\Gharrett\Desktop\Rebel Gaming\pawno\include\AttachStreamToObject.inc(65) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


6 Errors.

this is what i get when i add your include
Reply
#10

seems nice ill try it some time I like it mate Good job
Reply
#11

It will only work for radio stations(i guess). For other music links everytime it will start music over and over again. Correct?
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)