Can't Stop a Sound from Playing in a PlaySoundEvent
HI, I'm currently working on a mp3 Player Mod, and so I have to Stop any Background Music from playing when the Player is playing music. I have tried to Subscribe to the PlaySoundEvent to stop the Music. But I cant stop the Music from Playing. I have tried these three Methods:
@SubscribeEvent
@SideOnly(Side.CLIENT)
public void PlaySoundEvent (PlaySoundEvent event){
System.out.println("Played Streamed Sound: "+event.category);
if(event.category == SoundCategory.MUSIC){
//1.
event.manager.stopSound(event.sound); //no sound has been played yet
//2.
event.setResult(null); //this should Work
//3.
event.setCanceled(true); //Crashes the Game
}
}
I'm using Forge 11.14.1299 (I know that this isn't the newest Version of Forge, but I have had a look in the Changelog and there were no issues regarding that).
In Minecraft 1.7 (Forge V1.7.10-10.13.0.1180) this Worked:
@SubscribeEvent
@SideOnly(Side.CLIENT)
public void PlaySoundEvent(PlaySoundEvent17 event){
event.manager.sndHandler.stopSound(event.sound);
event.result = null;
}
BTW: Is there a way of playing a File from a specified Path and not use the SoundSystem directly, or get the SoundSystem Object without reflection?
Logs.
What do you mean, by saying Logs? Do you want any Logs from me? There are no important Logs, because its a semantic Bug, a Bug, which does not throw any Exceptions, except for the last one, becuase the Event is not cancelable.
PlaySoundEvent does not have HasResult or Cancellable annotations. Thus neither Event#setCancelled(boolean) not Event#setResult(Result) would work. You should edit the PlaySoundEvent#result field, exactly like in 1.7.
Why each doesn't work. event.manager.stopSound(event.sound); Just as you pointed out, the sound hasn't started. event.setCanceled(true); the event is not flagged Cancelable event.setResult(null); this is expecting the enum Result (DENY, DEFAULT, ALLOW)
The old way you used in 1.7 should be what your after. event.result = null;
EDIT: @GotoLink you ninja, sneaking in there a few seconds before me ;)
Thanks, for your Help. Although, its confusing, naming a Function getResult and setResult?? You could rename them to setResultState and getResultState, just to clarify things. Because right now, it looks like you would set the event.result.
OK, but if I try now to stop the music by using soundHandler.stopSound() (I want to Stop the music if the user clicks on the Play Button of the GUi), if a GUI is Open, and I Close the GUI again, the Music starts to Play again from the beginning on, without generating an Event. (I Think its the same situation when you open and close the Esc Menu). Only Calling the Method again and the soundHandler.update() Method, after the GUI has been Closed helps.
This did not happen in MC 1.7.
@diesieben07 added labels [Bug]
setResultSound(null) will prevent the sound from playing.