• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[vJASS] Stop and replace game music, play game music for player only

Status
Not open for further replies.
Level 26
Joined
Feb 2, 2006
Messages
1,700
I use the following methods:

JASS:
/**
	 * \param musicList File paths should be separated by ; character.
	 */
	function SetMapMusicForPlayer takes player whichPlayer, string musicList, boolean random, integer index returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call SetMapMusic(musicList, random, index)
		endif
	endfunction

	/**
	 * <a href="http://www.wc3jass.com/viewtopic.php?t=141&sid=13329920bf4c51dc914b774abe5838de">Source</a>
	 */
	function SetMapMusicForForce takes force whichForce, string musicList, boolean random, integer index returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call SetMapMusic(musicList, random, index)
		endif
	endfunction

	function SetMapMusicIndexedForPlayer takes player whichPlayer, string musicList, integer index returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call SetMapMusicIndexedBJ(musicList, index)
		endif
	endfunction

	function SetMapMusicIndexedForForce takes force whichForce, string musicList, integer index returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call SetMapMusicIndexedBJ(musicList, index)
		endif
	endfunction

	function SetMapMusicRandomForPlayer takes player whichPlayer, string musicList returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call SetMapMusicRandomBJ(musicList)
		endif
	endfunction

	function SetMapMusicRandomForForce takes force whichForce, string musicList returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call SetMapMusicRandomBJ(musicList)
		endif
	endfunction

	function ClearMapMusicForPlayer takes player whichPlayer returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call ClearMapMusic()
		endif
	endfunction

	function ClearMapMusicForForce takes force whichForce returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call ClearMapMusic()
		endif
	endfunction

	function PlayMusicForPlayer takes player whichPlayer, string musicName returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call PlayMusic(musicName)
		endif
	endfunction

	function PlayMusicForForce takes force whichForce, string musicName returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call PlayMusic(musicName)
		endif
	endfunction

	function PlayMusicExForPlayer takes player whichPlayer, string musicName, integer fromMilliseconds, integer fadeInMilliseconds returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call PlayMusicEx(musicName, fromMilliseconds, fadeInMilliseconds)
		endif
	endfunction

	function PlayMusicExForForce takes force whichForce, string musicName, integer fromMilliseconds, integer fadeInMilliseconds returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call PlayMusicEx(musicName, fromMilliseconds, fadeInMilliseconds)
		endif
	endfunction

	function StopMusicForPlayer takes player whichPlayer, boolean fadeOut returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call StopMusic(fadeOut)
		endif
	endfunction

	function StopMusicForForce takes force whichForce, boolean fadeOut returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call StopMusic(fadeOut)
		endif
	endfunction

	function ResumeMusicForPlayer takes player whichPlayer returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call ResumeMusic()
		endif
	endfunction

	function ResumeMusicForForce takes force whichForce returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call ResumeMusic()
		endif
	endfunction

	function PlayThematicMusicForPlayer takes player whichPlayer, string musicFileName returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call PlayThematicMusic(musicFileName)
		endif
	endfunction

	function PlayThematicMusicForForce takes force whichForce, string musicFileName returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call PlayThematicMusic(musicFileName)
		endif
	endfunction

	function PlayThematicMusicExForPlayer takes player whichPlayer, string musicFileName, integer fromMilliseconds returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call PlayThematicMusicEx(musicFileName, fromMilliseconds)
		endif
	endfunction

	function PlayThematicMusicExForForce takes force whichForce, string musicFileName, integer fromMilliseconds returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call PlayThematicMusicEx(musicFileName, fromMilliseconds)
		endif
	endfunction

	function EndThematicMusicForPlayer takes player whichPlayer returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call EndThematicMusic()
		endif
	endfunction

	function EndThematicMusicForForce takes force whichForce returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call EndThematicMusic()
		endif
	endfunction

	/**
	 * \param volume 0-127
	 */
	function SetMusicVolumeForPlayer takes player whichPlayer, integer volume returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call SetMusicVolume(volume)
		endif
	endfunction

	function SetMusicVolumeForForce takes force whichForce, integer volume returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call SetMusicVolume(volume)
		endif
	endfunction

	function SetMusicPlayPositionForPlayer takes player whichPlayer, integer milliseconds returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call SetMusicPlayPosition(milliseconds)
		endif
	endfunction

	function SetMusicPlayPositionForForce takes force whichForce, integer milliseconds returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call SetMusicPlayPosition(milliseconds)
		endif
	endfunction

	function SetThematicMusicPlayPositionForPlayer takes player whichPlayer, integer milliseconds returns nothing
		if (whichPlayer == GetLocalPlayer()) then
			call SetThematicMusicPlayPosition(milliseconds)
		endif
	endfunction

	function SetThematicMusicPlayPositionForForce takes force whichForce, integer milliseconds returns nothing
		if (IsPlayerInForce(GetLocalPlayer(), whichForce)) then
			call SetThematicMusicPlayPosition(milliseconds)
		endif
	endfunction

and

JASS:
/**
		 * \param musicList File paths should be separated by ; character.
		 */
		public static method setMapMusic takes string musicList returns nothing
			call StopMusic(false)
			call ClearMapMusic()
			call SetMapMusic(musicList, true, 0)
			call ResumeMusic()
		endmethod

		/**
		 * Map data structure MapData should always have public static constant string member "mapMusic" which contains a list of music files.
		 * If that value is equal to null music won't be changed.
		 */
		public static method setDefaultMapMusic takes nothing returns nothing
			if (MapData.mapMusic != null) then
				call thistype.setMapMusic(MapData.mapMusic)
			endif
		endmethod

		public static method setMapMusicForPlayer takes player whichPlayer, string musicList returns nothing
			call StopMusicForPlayer(whichPlayer, false)
			call ClearMapMusicForPlayer(whichPlayer)
			call SetMapMusicForPlayer(whichPlayer, musicList, true, 0)
		endmethod

		public static method setDefaultMapMusicForPlayer takes player whichPlayer returns nothing
			if (MapData.mapMusic != null) then
				call thistype.setMapMusicForPlayer(whichPlayer, MapData.mapMusic)
			endif
		endmethod

First of all I want to ask if playing music of any kind for a player only works and if it does lead to desyncs. Since I've adapted the functions from somewhere I guess not.

The real problem I have all the time is that when calling the static method setMapMusic() which should stop and replace the map's music (for example the music pieces of the human race) it does sometimes continue to play the race's music and sometimes it plays my custom music. PlayMusic() on the other hand of course does always play the music but it ends the music afterwards.
How can I change the map's music in a reliable way? I've tried different variants with removing the ResumeMusic() call etc.
 
Level 26
Joined
Feb 2, 2006
Messages
1,700
Sorry for reviving this old thread but it still does not work. The human music just continues. I've added a print statement to setMapMusic() and tried different orders for stopping the music etc. In the beginning of the game a video sequence starts which uses cinematic mode. After disabling the cinematic mode again I always call setDefaultMapMusic (). The map music is not null and the path should be fine.
Map music looks like this:
JASS:
public static constant string mapMusic = "Sound\\Music\\mp3Music\\Pippin the Hunchback.mp3"
That is the same path Warcraft III generates when I use the sound as music in the sound editor and call the function SetMapMusic() via GUI.
 
Level 26
Joined
Feb 2, 2006
Messages
1,700
I use a custom MPQ. I've edited the UI/WorldEditData.txt file to list the music even in the sound editor. When I play it there it plays successfully.

Again I used this method:
JASS:
public static method setMapMusic takes string musicList returns nothing
            call StopMusic(false)
            call ClearMapMusic()
            call SetMapMusic(musicList, true, 0)
            call ResumeMusic()
        endmethod
 
Level 14
Joined
Jul 1, 2008
Messages
1,314
you can either use Arad MNK's External Music System on the hive or make something easy on your own.

-Store the strings of the paths of the music peaces in an array. You can use locally stored music or imported pieces.
-Set Map Music to ""
- Stop and Play your music locally for each player depending on an event of your choice. Thereby you can access your tracklist array easily by playing a track by its index.

JASS:
e.g. 
A unit enters region
if GetLocalPlayer() == GetOwningPlayer( GetTriggerUnit()) then
call StopMusic()
call PlayMusic(YOUR_ARRAY_OF_TRACK_PATHS[integer_of_choice])
 
Status
Not open for further replies.
Top