Jump to content

I want to add task to play clip sound


Jongco0331

Recommended Posts

I made new Thread to play sound using Clip class. But, when I give Thread#sleep to get fade In, Minecraft Client stops. How can I add new Runnable Thread in forge modding 1.19.2?

 


public class AudioPlayUtil {

    public static boolean isPlaying = false;
    public static Clip clip;

    public static float volume = 1;

    public static void stopWav()
    {
        if(isPlaying)
        {
            isPlaying = false;
            clip.stop();
            FloatControl control = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            control.setValue(control.getMinimum());
        }
    }
    public static void playWav(float fadeInTime, File path)
    {
        if(isPlaying)
        {
            isPlaying = false;
            clip.stop();
            FloatControl control = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            control.setValue(control.getMinimum());
        } else {
            isPlaying = true;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
                            path);

                    clip = AudioSystem.getClip();
                    clip.open(audioInputStream);

                    FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);

                    float increasing_value = Math.abs(getCurrentVolume(gainControl, (float) ((int)AudioPlayUtil.volume * 100.0D)) / fadeInTime);

                    clip.loop(Clip.LOOP_CONTINUOUSLY);

                    gainControl.setValue(gainControl.getMinimum());

                    clip.start();

                    isPlaying = true;

                    for(int i = 0; i < (int)fadeInTime; i++)
                    {
                        FloatControl control = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN) ;
                        control.setValue(control.getValue() + increasing_value);
                        Thread.sleep(50);
                    }
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }).run();

    }

    public static float getCurrentVolume(FloatControl floatControl, float volume)
    {
        float a = floatControl.getMaximum() - floatControl.getMinimum();
        return Math.abs(a * (volume/100));
    }


}

 

Link to comment
Share on other sites

On 11/29/2022 at 10:19 AM, Jongco0331 said:

I made new Thread to play sound using Clip class. But, when I give Thread#sleep to get fade In, Minecraft Client stops. How can I add new Runnable Thread in forge modding 1.19.2?

Don't do this. If you are going to play sound in Minecraft, use the SoundEngine and build sounds around that. What are you trying to do?

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