Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

 

  • Author
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);
}

 

  • Author

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

  • Author
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.

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.