Jump to content

So I have this code in my main class

public class ProjetFinal {static boolean music = true;    public static void main(String[] args) {new frmMainMenu().setVisible(true);      }    }

And this is my Frame code

                while (Music == true)                 {                try                 {                    Music = false;                InputStream test = getClass().getClassLoader().getResourceAsStream("musics/menu.wav");                AudioStream Audio = new AudioStream(test);                   AudioPlayer.player.start(Audio);                                 } catch (final Exception e) {    System.err.println("Exception occurred: " + e.getMessage());   JOptionPane.showMessageDialog(null, e.getMessage()); }finally { Music = false; }                                                }
Link to comment
https://linustechtips.com/topic/379529-java-my-loop-is-not-functionning-properly/
Share on other sites

Link to post
Share on other sites

You seem to have 1 too less } in your Frame code, am I right?

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

You seem to have 1 too less } in your Frame code, am I right?

 

Yeah but I didn't put em all. Don't have any red lines and program runs fine. Just that when I exit my main frame and come back second music stats playing over first one.

Link to post
Share on other sites

Yeah but I didn't put em all. Don't have any red lines and program runs fine. Just that when I exit my main frame and come back second music stats playing over first one.

You should try to eliminate some stuff.

First of all, you should check if your code ever reaches the "while (Music == true)" state.

After that, see if the variable 'test' is not null.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

You should try to eliminate some stuff.

First of all, you should check if your code ever reaches the "while (Music == true)" state.

After that, see if the variable 'test' is not null.

 

What do you mean eliminate?

 

And how can I check if it reaches or not?

Link to post
Share on other sites

What do you mean eliminate?

 

And how can I check if it reaches or not?

Eliminate all possible faults in your code, by seeing what is causing the error or whatever.

Place a trace or debug.log (or whatever you would use in Java) to see if your code actually gets in side of the while loop.

Check what the test variable is filled with, after you filled it with a song.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

So...is your program exiting before the audio has a chance to play? Is that the problem? I see you request audioplay start, but that (I'm guessing) doesn't block the program from executing other actions, and if your line right after the 'finally' statement is 'return 0' then you are exiting before your sound file has a chance to play more than a millisecond or two of audio.

Link to post
Share on other sites

So...is your program exiting before the audio has a chance to play? Is that the problem? I see you request audioplay start, but that (I'm guessing) doesn't block the program from executing other actions, and if your line right after the 'finally' statement is 'return 0' then you are exiting before your sound file has a chance to play more than a millisecond or two of audio.

 

No everything works fine. It's just when I go back to the MainMenu frame is restarts to play. Thus my loop or something is not working well.

Link to post
Share on other sites

Eliminate all possible faults in your code, by seeing what is causing the error or whatever.

Place a trace or debug.log (or whatever you would use in Java) to see if your code actually gets in side of the while loop.

Check what the test variable is filled with, after you filled it with a song.

 

Not sure about logs or debugging, we didn't see that yet in class... lol.

Link to post
Share on other sites

Not sure about logs or debugging, we didn't see that yet in class... lol.

 

I find it upsetting when programming "classes" don't cover problem solving as one of the first topics.

 

In the meantime, you'll just have to pepper your code with print statements so you know if that part of the code was reached; print the value of variables so you know what is going on and can detect erroneous output.

Link to post
Share on other sites

 

 

In the meantime, you'll just have to pepper your code with print statements so you know if that part of the code was reached; print the value of variables so you know what is going on and can detect erroneous output.

Oh yeah, how did I not think about that.... ofcourse. I can easily do labels or Options panes when it reaches the place. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×