Can one please help me in this java code. how can i let while loop work efficient it eats my CPU

import java.io.File;
import java.util.Scanner;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class SimpleAudioPlayer {

public static void main(String[] args) {

	
	File Song = null;
	boolean flag = true;
	
	while(flag) {
		System.out.println("Enter number to play song: ");
		System.out.println("Enter 0 to close: ");
		
		Scanner input = new Scanner(System.in);
		int c = input.nextInt();
		switch(c) {
			
			case 1:
				Song = new File("C:\\Users\\Majd\\eclipse-workspace\\Audio\\s1.wav");
				PlaySound(Song);
				break;
			case 2:
				Song = new File("C:\\Users\\Majd\\eclipse-workspace\\Audio\\s2.wav");
				PlaySound(Song);
				break;
			case 3:
				Song = new File("C:\\Users\\Majd\\eclipse-workspace\\Audio\\s3.wav");
				PlaySound(Song);
				break;
			case 4:
				Song = new File("C:\\Users\\Majd\\eclipse-workspace\\Audio\\Guitar.wav");
				PlaySound(Song);
				break;
			case 0:
				flag = false; break;
		}
		

	}
	

}

public static void PlaySound(File Sound) {
	
	try {
		Clip clip = AudioSystem.getClip();
		clip.open(AudioSystem.getAudioInputStream(Sound));
		clip.start();
		
		Thread.sleep(clip.getMicrosecondLength()/100);
	}catch(Exception e) {
		System.err.println(e.getMessage());
	}
}

}

Of course it does. You’ve got an infinite loop there that’s always executing. You need a way to react to input when you get it, otherwise the loop will run at the maximum available speed. (Or you could just do a sleep() call, if that works for you).

2 Likes

i tried sleep but i dont know where i should place it so id didnt work .can you please modify the code then maybe it works

Two things: This isn’t a general Java support forum.
And I won’t write your code for you. If you don’t even know where to place that sleep call, then you might want to learn a bit more Java. There’s plenty of good resources on the net.

6 Likes

yes. i posted in java forum but i didnt get answer and i find this forum very helpful and i learned alot and i am happy to be here. Thanks

Do not use sleep, use wait if you must.

1 Like

thanks sleep didnt work in this Code i will try wait

it’s not the loop’s fault the loop is fine, what you are doing wrong is that you are initialising the song every time you need it, init all the songs first then play the one you need.

1 Like

this is a way to do it you have many others

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Main {
    private static Clip s1Clip, s2Clip, s3Clip, guitarClip;

    public static void main(String[] args) {

        File s1Song = new File("C:\\Users\\Majd\\eclipse-workspace\\Audio\\s1.wav");
        File s2Song = new File("C:\\Users\\Majd\\eclipse-workspace\\Audio\\s2.wav");
        File s3Song = new File("C:\\Users\\Majd\\eclipse-workspace\\Audio\\s3.wav");
        File guitarSong = new File("C:\\Users\\Majd\\eclipse-workspace\\Audio\\Guitar.wav");

       try {
            s1Clip = initClip(s1Song);
            s2Clip = initClip(s2Song);
            s3Clip = initClip(s3Song);
            guitarClip = initClip(guitarSong);

        } catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
        Scanner input = new Scanner(System.in);
        boolean flag = true;

        while (flag) {
            System.out.println("Enter number to play song: ");
            System.out.println("Enter 0 to close: ");

            int c = input.nextInt();
            switch (c) {

                case 1:
                    s1Clip.start();
                    System.out.println("song 1 played");
                    break;
                case 2:
                    s2Clip.start();
                    System.out.println("song 2 played");

                    break;
                case 3:
                    s3Clip.start();
                    System.out.println("song 3 played");

                    break;
                case 4:
                    guitarClip.start();
                    System.out.println("song 4 played");

                    break;
                case 0:
                    return;
            }

        }

    }

    private static Clip initClip(File song) throws LineUnavailableException, IOException, UnsupportedAudioFileException {
        Clip clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(song));
        return clip;
    }

}
1 Like

Thanks thanks very much it works very good.:heart:

Give a man a fish…

4 Likes

You should try first to catch a fish i think so better:rose::rose::rose: