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
--------------------------------------
- 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.
--------------------------------------*/
/*
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;
}
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;
}
What if the objects are attached to a car, do they move with the car?
|
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;
}