Sorry for bumping but I don't get it is it rehlds fault or mp3 format because it works fine for me
I'll provide full plugin, maybe I am missing something.
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <hamsandwich>
#define PLUGIN "Music"
#define VERSION "1.0.1"
#define AUTHOR "TBagT"
#pragma semicolon 1
#define SONG_NAME_MAX 64
#define SONG_PATH_MAX 128
#define SONG_TASK 5955
#define TASK_SHOW_AD 6000
#define is_a_player(%1) (1<=%1<=32)
const OFFSET_CSMENUCODE = 205;
enum _:SongData { song_name[ SONG_NAME_MAX ], song_path[ SONG_PATH_MAX ], song_length, bool: is_wav };
enum _:PlayerData { current_song_id, bool: play_songs };
enum _:PluginCvars { PLAY_FOR_JUST_CONNECTED, SHOW_AD, SHOW_AD_TIMER }
new Array: Songs, bool: plugin_active = true, bool: round_started = false,
g_total_songs = 0, g_say_text, g_player[ 33 ][ PlayerData ],
g_cvars[ PluginCvars ], cfg_dir[ SONG_PATH_MAX ];
new const clcmd_commands[ ][ ] = {
"say /music", "clcmd_music",
"say_team /music", "clcmd_music",
"say /stop", "client_stop_music",
"say_team /stop", "client_stop_music"
};
public plugin_precache( ) {
register_dictionary( "music_plugin.txt" );
Songs = ArrayCreate( SongData );
static configDir[ SONG_PATH_MAX ];
get_configsdir( configDir, charsmax( configDir ) );
formatex( cfg_dir, charsmax( cfg_dir ), "%s/music_plugin.cfg", configDir );
if( !file_exists( cfg_dir ) )
plugin_active = false;
else
insert_songs( );
if( g_total_songs ) {
new index, SongItem[ SongData ];
for( index = 0; index < g_total_songs; index++ ) {
ArrayGetArray( Songs, index, SongItem );
precache_generic( SongItem[ song_path ] );
}
} else
plugin_active = false;
}
public plugin_init( ) {
register_plugin( PLUGIN, VERSION, AUTHOR );
register_cvar( "music_plugin", VERSION, FCVAR_SPONLY | FCVAR_SERVER );
set_cvar_string( "music_plugin", VERSION );
if( plugin_active ) {
g_cvars[ PLAY_FOR_JUST_CONNECTED ] = register_cvar( "music_play_just_connected", "1" );
g_cvars[ SHOW_AD ] = register_cvar( "music_show_ad", "1" );
g_cvars[ SHOW_AD_TIMER ] = register_cvar( "music_ad_timer", "30.0" );
register_logevent( "lev_round_start", 2, "1=Round_Start" );
register_logevent( "lev_round_end", 2, "1=Round_End" );
RegisterHam( Ham_Spawn, "player", "fw_player_spawn", 1 );
for( new index = 0; index < sizeof( clcmd_commands ); index += 2 )
register_clcmd( clcmd_commands[ index ], clcmd_commands[ index + 1 ] );
g_say_text = get_user_msgid( "SayText" );
} else
log_amx( "%L", LANG_SERVER, "MUSIC_PLUGIN_ERR" );
}
public plugin_end( ) {
ArrayDestroy( Songs );
}
public client_putinserver( id ) {
if( !plugin_active )
return;
g_player[ id ][ current_song_id ] = -1;
g_player[ id ][ play_songs ] = bool: get_pcvar_num( g_cvars[ PLAY_FOR_JUST_CONNECTED ] );
}
public client_disconnect( id ) {
if( !plugin_active )
return;
client_stop_music( id, false );
}
public fw_player_spawn( id ) {
if( !plugin_active )
return;
if( g_player[ id ][ current_song_id ] == -1 && round_started && g_player[ id ][ play_songs ] ) {
new random_song_id = generate_random_song( );
client_start_music( id, random_song_id );
}
}
public lev_round_start( ) {
if( !plugin_active )
return;
round_started = true;
if( bool: get_pcvar_num( g_cvars[ SHOW_AD ] ) )
set_task( get_pcvar_float( g_cvars[ SHOW_AD_TIMER ] ), "show_ad_task", TASK_SHOW_AD, _, _, "b" );
client_start_music_all( );
}
public lev_round_end( ) {
if( !plugin_active )
return;
round_started = false;
if( task_exists( TASK_SHOW_AD ) )
remove_task( TASK_SHOW_AD );
client_stop_music_all( );
}
public insert_songs( ) {
new filePointer = fopen( cfg_dir, "r" );
if( filePointer ) {
new readData[ SONG_PATH_MAX ];
new SongItem[ SongData ];
new SngName[ SONG_NAME_MAX ], SngPath[ SONG_PATH_MAX ], SngLen[ 4 ];
while( fgets( filePointer, readData, charsmax( readData ) ) ) {
if( equal( readData, "^n" ) || equal( readData, "^r^n" ) )
continue;
parse( readData, SngName, charsmax( SngName ) - 1, SngPath, charsmax( SngPath ) - 1, SngLen, 3 );
if( !file_exists( SngPath ) ) {
log_amx( "%L", LANG_SERVER, "SONG_NOT_FOUND", SngPath );
continue;
}
if( containi( SngPath, ".wav" ) == -1 && containi( SngPath, ".mp3" ) == -1 ) {
log_amx( "%L", LANG_SERVER, "SONG_INVALID_FORMAT", SngName );
continue;
}
SongItem[ song_name ] = SngName;
SongItem[ song_path ] = SngPath;
SongItem[ song_length ] = _:str_to_num( SngLen );
SongItem[ is_wav ] = ( containi( SngPath, ".wav" ) != -1 ) ? true : false;
ArrayPushArray( Songs, SongItem );
g_total_songs++;
}
fclose( filePointer );
} else
plugin_active = false;
}
client_start_music_all( ) {
if( !plugin_active )
return;
new random_song_id = generate_random_song( );
new players[ 32 ], pnum;
get_players( players, pnum );
for( new index = 0; index < pnum; index++ ) {
if( g_player[ players[ index ] ][ play_songs ] )
client_start_music( players[ index ], random_song_id );
}
}
client_start_music( id, song ) {
if( !plugin_active )
return PLUGIN_CONTINUE;
if( song > g_total_songs )
return PLUGIN_CONTINUE;
client_stop_music( id, false );
if( !g_player[ id ][ play_songs ] )
g_player[ id ][ play_songs ] = true;
g_player[ id ][ current_song_id ] = song;
new playSong[ SongData ];
ArrayGetArray( Songs, song, playSong );
log_amx( "It is: %s", ( playSong[ is_wav ] ) ? "spk" : "mp3 play" );
log_amx( "Song Path: %s", playSong[ song_path ] );
client_cmd( id, "%s ^"%s^"", ( playSong[ is_wav ] ) ? "spk" : "mp3 play", playSong[ song_path ] );
set_task( float( playSong[ song_length ] ), "client_song_finished", id + SONG_TASK );
return PLUGIN_CONTINUE;
}
public client_song_finished( id ) {
id -= SONG_TASK;
new random_song_id;
if( g_total_songs > 1 ) {
do {
random_song_id = generate_random_song( );
} while( g_player[ id ][ current_song_id ] == random_song_id );
} else
random_song_id = 0;
client_start_music( id, random_song_id );
}
client_stop_music_all( ) {
if( !plugin_active )
return;
new players[ 32 ], pnum;
get_players( players, pnum );
for( new index = 0; index < pnum; index++ )
client_stop_music( players[ index ], false );
}
public client_stop_music( id, bool: stop_every ) {
if( !plugin_active )
return PLUGIN_CONTINUE;
if( stop_every )
g_player[ id ][ play_songs ] = false;
g_player[ id ][ current_song_id ] = -1;
remove_task( id + SONG_TASK );
client_cmd( id, "mp3 stop" );
return PLUGIN_CONTINUE;
}
/* MENU */
public clcmd_music( id ) {
if( !plugin_active )
return PLUGIN_CONTINUE;
static menu[ SONG_PATH_MAX ];
new menu_id;
formatex( menu, charsmax( menu ), "%L", LANG_PLAYER, "MUSIC_MENU_TITLE" );
menu_id = menu_create( menu, "menu_items_random" );
formatex( menu, charsmax( menu ), "%L", LANG_PLAYER, "PLAY_RANDOM_SONG" );
menu_additem( menu_id, menu );
formatex( menu, charsmax( menu ), "%L", LANG_PLAYER, "MUSIC_LIST_ITEM", g_total_songs );
menu_additem( menu_id, menu );
formatex( menu, charsmax( menu ), "%s%L", ( g_player[ id ][ play_songs ] ) ? "\w" : "\d", LANG_PLAYER, "STOP_PLAYING" );
menu_additem( menu_id, menu );
formatex( menu, charsmax( menu ), "%L", LANG_PLAYER, "EXIT_MUSIC_MENU" );
menu_setprop( menu_id, MPROP_EXITNAME, menu );
set_pdata_int( id, OFFSET_CSMENUCODE, 0 );
menu_display( id, menu_id, 0 );
return PLUGIN_CONTINUE;
}
public menu_items_random( id, menu, item ) {
if( !plugin_active )
return PLUGIN_HANDLED;
switch( item ) {
case 0: {
new random_song_id = generate_random_song( );
client_start_music( id, random_song_id );
clcmd_music( id );
}
case 1: {
clcmd_music_list( id );
}
case 2: {
client_stop_music( id, true );
clcmd_music( id );
}
}
menu_destroy( menu );
return PLUGIN_HANDLED;
}
public clcmd_music_list( id ) {
if( !plugin_active )
return PLUGIN_CONTINUE;
static menu[ SONG_NAME_MAX ], SongItem[ SongData ];
new menu_id, item_data[ 2 ], index;
formatex( menu, charsmax( menu ), "%L", LANG_PLAYER, "MUSIC_LIST" );
menu_id = menu_create( menu, "music_list_handler" );
for( index = 0; index < g_total_songs; index++ ) {
ArrayGetArray( Songs, index, SongItem );
item_data[ 0 ] = index;
item_data[ 1 ] = 0;
formatex( menu, charsmax( menu ), "%s%s \r[ \y%d:%02d \r]", ( g_player[ id ][ current_song_id ] == index ) ? "\d" : "\w", SongItem[ song_name ], SongItem[ song_length ] / 60, SongItem[ song_length ] % 60 );
menu_additem( menu_id, menu, item_data );
}
formatex( menu, charsmax( menu ), "%L", LANG_PLAYER, "BACK_MUSIC_MENU" );
menu_setprop( menu_id, MPROP_BACKNAME, menu );
formatex( menu, charsmax( menu ), "%L", LANG_PLAYER, "NEXT_MUSIC_MENU" );
menu_setprop( menu_id, MPROP_NEXTNAME, menu );
formatex( menu, charsmax( menu ), "%L", LANG_PLAYER, "EXIT_MUSIC_MENU" );
menu_setprop( menu_id, MPROP_EXITNAME, menu );
set_pdata_int( id, OFFSET_CSMENUCODE, 0 );
menu_display( id, menu_id, 0 );
return PLUGIN_CONTINUE;
}
public music_list_handler( id, menu, item ) {
if( item == MENU_EXIT || !plugin_active ) {
menu_destroy( menu );
return PLUGIN_HANDLED;
}
new item_data[ 2 ], dummy, index;
menu_item_getinfo( menu, item, dummy, item_data, charsmax( item_data ), _, _, dummy );
index = item_data[ 0 ];
client_start_music( id, index );
clcmd_music_list( id );
return PLUGIN_HANDLED;
}
public show_ad_task( ) {
client_printcolor( 0, "%L", LANG_PLAYER, "AD_NOTIFICATION" );
}
generate_random_song( ) {
return random_num( 0, g_total_songs - 1 );
}
stock client_printcolor( const id, const input[], any:... ) {
new count = 1, players[ 32 ];
static msg[ 191 ];
vformat( msg, 190, input, 3 );
replace_all( msg, 190, "!g", "^4" );
replace_all( msg, 190, "!y", "^1" );
replace_all( msg, 190, "!t", "^3" );
if( id && is_user_connected( id ) )
players[ 0 ] = id;
else
get_players( players, count, "ch" );
for( new index = 0; index < count; index++ ) {
message_begin( MSG_ONE_UNRELIABLE, g_say_text, _, players[ index ] );
write_byte( players[ index ] );
write_string( msg );
message_end( );
}
}