MinecraftForge icon indicating copy to clipboard operation
MinecraftForge copied to clipboard

Can't Stop a Sound from Playing in a PlaySoundEvent

Open masl123 opened this issue 11 years ago • 8 comments

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?

masl123 avatar Apr 09 '15 01:04 masl123

Logs.

LexManos avatar Apr 09 '15 03:04 LexManos

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.

masl123 avatar Apr 09 '15 11:04 masl123

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.

GotoLink avatar Apr 09 '15 12:04 GotoLink

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 ;)

ShetiPhian avatar Apr 09 '15 12:04 ShetiPhian

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.

masl123 avatar Apr 09 '15 13:04 masl123

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.

masl123 avatar Apr 09 '15 14:04 masl123

@diesieben07 added labels [Bug]

Actuarius avatar May 25 '16 01:05 Actuarius

setResultSound(null) will prevent the sound from playing.

LexManos avatar May 29 '16 02:05 LexManos