Re: Adding sound to program.
Can you explain what happens or does not happen?
Re: Adding sound to program.
An error comes under saying cannot find symbol.
Re: Adding sound to program.
Please post the full text of the error message.
Re: Adding sound to program.
Cannot find symbol
symbol: method run()
location Bluemoon.PlaySound
P.s I'm using netbeans because it has the JFrame editor.
Re: Adding sound to program.
Where is the run() method defined?
I copied, compiled and executed your code with no problem.
There is no s.run() in the code you posted???
You should post the code you are having problems with if you want help with it.
The posted code works fine. If you have changed it, you need to post the new code!!!
Re: Adding sound to program.
I'm trying to run the 'run' method in the PlaySound class in my main class called Bluemoon. The code you're asking me to paste is near 300 lines long.
Re: Adding sound to program.
Right, don't post all of that. Make a very short test program to post.
Re: Adding sound to program.
Code :
PlaySound s = new PlaySound();
private void BeginActionPerformed(java.awt.event.ActionEvent evt) {
text1.setText("")
Begin.setVisible(false);
choice1.setVisible(true);
health.setVisible(true);
health.setText("Health = 10");
s.run();
}
Re: Adding sound to program.
That code will not compile. If I can't compile it I can't test it.
Re: Adding sound to program.
Re: Adding sound to program.
Where is the run() method defined?
What class is it in?
Re: Adding sound to program.
Re: Adding sound to program.
Are you sure run() is a method in the PlaySound class?
If you have changed the class from your earlier posting, please post a new copy of the PlaySound class.
Re: Adding sound to program.
Code :
package Bluemoon;
import java.io.*;
import javax.sound.sampled.*;
public class PlaySound
{
public static void main(String[] args)
{
sound = new File("Bluemoon\\Song.wav"); // Write you own file location here and be aware that it need to be an .wav file
new Thread(play).start();
}
static File sound;
static boolean muted = false; // This should explain itself
static float volume = 100.0f; // This is the volume that goes from 0 to 100
static float pan = 0.0f; // The balance between the speakers 0 is both sides and it goes from -1 to 1
static double seconds = 0.0d; // The amount of seconds to wait before the sound starts playing
static boolean looped_forever = false; // It will keep looping forever if this is true
static int loop_times = 0; // Set the amount of extra times you want the sound to loop (you don't need to have looped_forever set to true)
static int loops_done = 0; // When the program is running this is counting the times the sound has looped so it knows when to stop
final static Runnable play = new Runnable() // This Thread/Runnabe is for playing the sound
{
public void run()
{
try
{
// Check if the audio file is a .wav file
if (sound.getName().toLowerCase().contains(".wav"))
{
AudioInputStream stream = AudioSystem.getAudioInputStream(sound);
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED)
{
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits() * 2,
format.getChannels(),
format.getFrameSize() * 2,
format.getFrameRate(),
true);
stream = AudioSystem.getAudioInputStream(format, stream);
}
SourceDataLine.Info info = new DataLine.Info(
SourceDataLine.class,
stream.getFormat(),
(int) (stream.getFrameLength() * format.getFrameSize()));
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(stream.getFormat());
line.start();
// Set Volume
FloatControl volume_control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
volume_control.setValue((float) (Math.log(volume / 100.0f) / Math.log(10.0f) * 20.0f));
// Mute
BooleanControl mute_control = (BooleanControl) line.getControl(BooleanControl.Type.MUTE);
mute_control.setValue(muted);
FloatControl pan_control = (FloatControl) line.getControl(FloatControl.Type.PAN);
pan_control.setValue(pan);
long last_update = System.currentTimeMillis();
double since_last_update = (System.currentTimeMillis() - last_update) / 1000.0d;
// Wait the amount of seconds set before continuing
while (since_last_update < seconds)
{
since_last_update = (System.currentTimeMillis() - last_update) / 1000.0d;
}
System.out.println("Playing!");
int num_read = 0;
byte[] buf = new byte[line.getBufferSize()];
while ((num_read = stream.read(buf, 0, buf.length)) >= 0)
{
int offset = 0;
while (offset < num_read)
{
offset += line.write(buf, offset, num_read - offset);
}
}
line.drain();
line.stop();
if (looped_forever)
{
new Thread(play).start();
}
else if (loops_done < loop_times)
{
loops_done++;
new Thread(play).start();
}
}
}
catch (Exception ex) { ex.printStackTrace(); }
}
};
}
About half way down. The constructor.
Re: Adding sound to program.
that is not a constructor. It's a method.
Add this line first thing in the run method:
System.out.println("run this="+ this);
And look what is printed out when it executes.
Now look in the folder containing the class files and see if there is a class file with the same name as was printed by the above.
Re: Adding sound to program.
Error Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Bluemoon.PlaySound.run
at Bluemoon.Bluemoon.BeginActionPerformed(Bluemoon.ja va:173)
at Bluemoon.Bluemoon.access$200(Bluemoon.java:24)
at Bluemoon.Bluemoon$3.actionPerformed(Bluemoon.java: 84)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEven tMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.jav a:6504)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
at java.awt.Component.processEvent(Component.java:626 9)
at java.awt.Container.processEvent(Container.java:222 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4860)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Component.dispatchEvent(Component.java:46 86)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713 )
at java.awt.Component.dispatchEvent(Component.java:46 86)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
at java.awt.EventQueue.access$000(EventQueue.java:101 )
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)
-------------------------------------------------------------------------------------------------
I checked in the src folder for my project and there was no class file with that name.
Re: Adding sound to program.
The code you posted compiles and executes on my system.
Did you try to compile and execute what was posted in post #1?
Compile and execute the class: PlaySound that was posted in post#1.
Re: Adding sound to program.
So it plays sound on your system??
Re: Adding sound to program.
The code that you posted for the class: PlaySound compiles and executes on my system and plays a sound.
Re: Adding sound to program.
Yes. It works as a single class. But what i want to do is run the method public void run() in another class when you press a button.
Re: Adding sound to program.
Did you add the println and look at the output?
The print out will show you what class run() is in.
Re: Adding sound to program.
Yes. I pasted it, man. But it told me run is in PlaySound, which it is.
Re: Adding sound to program.
I don't think so. Post the print out here and I'll show you.
Re: Adding sound to program.
Yes. I pasted it, man. It told me the run() method was in PlaySound, which it is..