Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Music in My Dimension


Thor597
 Share

Recommended Posts

Subscribe to the PlayBackgroundMusicEvent and then check if the player is in your dimension. If so, return a different SoundPoolEntry. That's how I'd do it.

To have the music in your Mod-Zip file, you can use a custom URLStreamHandler which makes the SoundPoolEntry accept the InputStreams from your zip. Ask if you wan't help with that.

Link to comment
Share on other sites

Subscribe to the PlayBackgroundMusicEvent and then check if the player is in your dimension. If so, return a different SoundPoolEntry. That's how I'd do it.

To have the music in your Mod-Zip file, you can use a custom URLStreamHandler which makes the SoundPoolEntry accept the InputStreams from your zip. Ask if you wan't help with that.

 

Yes I would want some more help with that stuff. Someone should make a page on the tutorials page on how to add background music to dimensions and just music in general and also in the same tutorial how to add sounds(I used the system with ISoundHandler before but now that its gone I need to do the events things)

 

so yeah, a tutorial called: How to add new Audio(Sounds and Music) should be in the tutorials section! :D

Link to comment
Share on other sites

Ok, here's a simple tutorial:

 

First, we need a Class that will handle the Sound Events: (I left out all the imports and stuff, I'm sure you can do that yourself)

private static SoundPoolEntry entry;

public class SoundHandler {
    @ForgeSubscribe
    public void onSound(PlaySoundEvent event) {
        if (event.name.equals("example.examplesound")) {
            event.result = entry;
        }
    }

    static {
        try {
            entry = new SoundPoolEntry("example/examplesound.ogg", new File("/path/to/your/sound.ogg").toURI().toURL());
        } catch (MalformedURLException e) {
            // oops
            e.printStackTrace();
        }
     }
}

Register the Event handler like this:

MinecraftForge.EVENT_BUS.register(new SoundHandler());

 

You could then just use (pseudo code):

someRandomWorldObject.playSoundEffect(x, y, z, "example.examplesound", 1, 1);

 

But that would require you to have your Sound installed into a normal folder in the .minecraft directory. It would be more convenient to have you sound in your mod-zip file.

Therefore you need a custom URLStreamHandler which should look about like this:

public class InputStreamURLHandler extends URLStreamHandler {
    private static HashMap<String,InputStream> inputStreamMap = new HashMap(); 
    private static final InputStreamURLHandler instance = new InputStreamURLHandler();
    protected URLConnection openConnection(URL u) throws IOException {
        return new InputStreamURLConnection(u);
    }

    public static InputStreamURLHandler instance() {
        return instance;
    }
    
    public static void addInputStream(String name, InputStream inputStream) {
        inputStreamMap.put(name, inputStream);
    }

    static class InputStreamURLConnection extends URLConnection {

        public InputStreamURLConnection(URL url) {
            super(url);
        }

        @Override
        public void connect() throws IOException { }

        @Override
        public InputStream getInputStream() throws IOException {
            return inputStreamMap.containsKey(getURL().getHost()) ? inputStreamMap.get(getURL().getHost()) : null;
        }
    }
}

Then you can do this in your SoundHandler class:

package com.example.exampleMod.client.sound;

private static SoundPoolEntry entry;

public class SoundHandler {
    @ForgeSubscribe
    public void onSound(PlaySoundEvent event) {
        if (event.name.equals("example.examplesound")) {
            event.result = entry;
        }
    }

    static {
        try {
            InputStreamURLHandler.addInputStream("examplesound", YourModFile.class.getResourceAsStream("/sampleSound.ogg"));
            entry = new SoundPoolEntry("example/examplesound.ogg", new URL(null, "inputstream://examplesound", InputStreamURLHandler.instance()));
        } catch (MalformedURLException e) {
            // oops
            e.printStackTrace();
        }
     }
}

All you need to do then is put "sampleSound.ogg" in your mod zip file.

If you want to add music, you do the same except that you subscribe to the PlayBackgroundMusicEvent.

Warning: All this has to happen only on the client side, so it's best to do it via a @SidedProxy.

 

Hope that helps.

Link to comment
Share on other sites

You define the path where it says getResourceAsStream. The path you provide there is relative to your Mod zip File (resp. the classpath which is also the minecraft.jar itself or the bin-Directory in the eclipse Folder when working with MCP.)

So if you use "/example.ogg" as the argument for getResourceAsStream you have to put the file "example.ogg" directly into your Zipfile or into the bin Directoy in eclipse.

Actually I found a better way to register sounds right now, which makes adding multiple sounds easier. Use this:

public class SoundHandler {
    @ForgeSubscribe
    public void onSound(SoundLoadEvent event) {
        try {
            InputStreamURLHandler.addInputStream("cameraclick", CameraCraft.class.getResourceAsStream("/CameraCraft/cameraclick.ogg"));
            event.manager.soundPoolSounds.addSound("your/sound.ogg", new URL(null, "inputstream://yoursound", InputStreamURLHandler.instance()));
        } catch (Exception e) {
            System.err.println("Failed to register one or more sounds.");
        }
    }
}

Everything else is the same. You can just repeat the 2 lines in the try-Block for every sound you want to add.

Link to comment
Share on other sites

You are aware of the fact that Class.getResource() (Not as stream) returns an URL you could pass to new SoundPoolEntry?

Like:

public class SoundHandler {
    @ForgeSubscribe
    public void onSound(SoundLoadEvent event) {
        try {
            event.manager.soundPoolSounds.addSound("your/sound.ogg", CameraCraft.class.getResource("/path/to/your/sound.ogg"));
        } catch (Exception e) {
            System.err.println("Failed to register one or more sounds.");
        }
    }
}

This avoids writing a hacked urlstreamhandler and will work no matter where in your classpath the sound file is.

Link to comment
Share on other sites

I have a problem.

 

I want my mob to do this sound but im not getting anything, heres my code

 

package net.minecraft.src;

import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
import net.minecraftforge.client.event.sound.SoundLoadEvent;
import net.minecraftforge.event.ForgeSubscribe;

public class ThorMod_SoundHandler
{
@ForgeSubscribe
 @SideOnly(Side.CLIENT)
public void onSound(SoundLoadEvent event)
{
	try
	{
		event.manager.soundPoolSounds.addSound("monkey1", mod_TheThorMod.class.getResource("/Sounds/monkey1"));
		event.manager.soundPoolSounds.addSound("monkey2", mod_TheThorMod.class.getResource("/Sounds/monkey2"));
		event.manager.soundPoolSounds.addSound("monkey3", mod_TheThorMod.class.getResource("/Sounds/monkey3"));
	} 

	catch (Exception e)
	{
		System.err.println("Failed to register one or more sounds.");
	}
}
}

 

And in my entity class I have this

protected String getLivingSound()
    {
        return "monkey";
    }

Link to comment
Share on other sites

Okay, I tried that now for myself and my sound won't register anywhere. In fact, the onSound method doesn't get even called (I've put a println in the method to test if it gets called). Did I do something wrong?

 

My SoundHandler:

 

public class PD_SoundHandler {	
@ForgeSubscribe
public void onSound(SoundLoadEvent event) {
	System.out.println("sound!");
        try {
            event.manager.soundPoolSounds.addSound("particledeco/static.ogg", PD_ModRegistry.class.getResource("/sound/static.wav"));
        } catch (Exception e) {
            System.err.println("Failed to register one or more sounds.");
        }
    }

 

 

The event bus will be registered in my ClientProxy like this:

MinecraftForge.EVENT_BUS.register(new PD_SoundHandler());

 

how I want to call my sound (in my entity):

PD_ModRegistry.proxy.getClientWorld().playSoundAtEntity(this, "particledeco.static", 1f, 1f);

 

 

PS: It seems that Thor597 above me has the same problem :P

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Okay, tyvm, but I have still the problem that the sound isn't played.

 

The sound is registered and the file will be found, but the entity won't play it :/

 

EDIT: Nvm, I'm using the worldObj field from my entity instead the ClientWorld from my proxy and it works now ^.^

EDIT2: It seems you have to use the server world for the entity, because it doesn't work with the client one...

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

So if i want music in my dimension i put this in my soundhandler

 

@ForgeSubscribe
@SideOnly(Side.CLIENT)
public void onSound(SoundLoadEvent event)
{
	try
	{
		if(this.world.provider.worldType == 23)
		{
			event.manager.soundPoolMusic.addSound("thormod/netromWood2.wav", mod_TheThorMod.class.getResource("/Sounds/netromWood2.wav"));
		}
	} 

Link to comment
Share on other sites

So if i want music in my dimension i put this in my soundhandler

 

@ForgeSubscribe
@SideOnly(Side.CLIENT)
public void onSound(SoundLoadEvent event)
{
	try
	{
		if(this.world.provider.worldType == 23)
		{
			event.manager.soundPoolMusic.addSound("thormod/netromWood2.wav", mod_TheThorMod.class.getResource("/Sounds/netromWood2.wav"));
		}
	} 

No. You have to add your Sound always. The Sound adding is just for registering your sound to the System. If you want to control when your music plays, use the onPlayBackgroundMusicEvent.

Don't you use eclipse? Because your code should produce errors. this.world is undefined.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.