Jump to content

Recommended Posts

Posted

I'm trying to play a custom piece of music, but would like to not have the background music from the game play, whilst my track is playing.

I would like for other sounds, such as mob sounds, doors, and all other sounds than music to still play.

As seen below, I tried using the soundhandler, but this doesn't seem to pause the sounds, whilst I play my own track.

Any ideas for how I can pause only the background music, only while my own track is playing?

 

import angercraft.postcreditsmusic.PostCreditsMusic;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;

public class ClientHandler {

     static Minecraft mc = Minecraft.getInstance();
     static SoundEvent sound = new SoundEvent(new ResourceLocation(PostCreditsMusic.MOD_ID, "track"));

    public static void playTrack() {
        SoundHandler soundHandler = mc.getSoundHandler();
        soundHandler.pause();
        mc.player.playSound(sound, 1.0F, 1.0F);
    }
}

 

Posted
58 minutes ago, diesieben07 said:
  • Don't store a static reference to Minecraft.
  • SoundEvent is a registry entry, it must be registered like all other registry entries. It must also not be created in a static initializer.
  • Look at MusicTicker. You'll have to hack around it's internals to make it stop playing music.

Thank you for your help.

Regarding the SoundEvent, will the following be good manners, or should it be registered differently?

public static SoundEvent sound;

@SubscribeEvent
public void registerMusic(RegistryEvent.Register<SoundEvent> event) {
      sound = new SoundEvent(new ResourceLocation(PostCreditsMusic.MOD_ID, "track"));
      event.getRegistry().register(sound);
}

 

Posted (edited)

Alright, got it all to work, and so far I've got a hacky solution to stop the normal music from playing.

But how would I go about detecting when the song is done playing? As I need to return the timeUntilNextMusic to its normal value.

 

private static Field timeUntilNextMusic = null;

    public static void playTrack() {
        Minecraft mc = Minecraft.getInstance();
        MusicTicker musicTicker = mc.getMusicTicker();
        if(timeUntilNextMusic == null) {
            timeUntilNextMusic = ObfuscationReflectionHelper.findField(MusicTicker.class, "field_147676_d");
        }
        musicTicker.stop();
        try {
            timeUntilNextMusic.setInt(musicTicker, 10000);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        mc.player.playSound(PostCreditsMusic.sound, 1.0F, 1.0F);
    }

 

Edited by Angercraft
Posted
31 minutes ago, diesieben07 said:
  • You should make timeUntilNextMusic final.
  • That's not how you handle exceptions. Ever.
  • The proper way to stop the vanilla music from playing would be PlaySoundEvent.

Yeah, sorry about the exception, I know about that one, but when just testing I don't worry too much about them.

Thank you for the help, I still need to figure out how to check if my custom track is done playing.

Posted

Alright, I got it to work. You can detect if a sound is done playing, when it is removed from the Map<ISound, Integer> playingSoundsStopTime located in the SoundEngine.

Needed to use reflection to get it working, but for now it seems like a good workaround.

 

Thank you for your time.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.