Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: How to manipulate a thread?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default How to manipulate a thread?

    I googled a tutorial on playing sounds, and therefore I have developed this class, and when I use it I create a new Thread . This is how I do it:
    PlaySound.sound = new File("Music/Gman_end.wav");
    						new Thread(PlaySound.play).start();
    Now is there a way to manipulate this thread? Because I have functions like mute and volume that I would like to apply to the playing sound file in the thread. Thank you for your time.

    package Game;
     
    import java.io.*;
    import javax.sound.sampled.*;
     
    public class PlaySound
    {	
    	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_BGM = 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_BGM)
    					{
    						new Thread(play).start();
    					}
    					else if (loops_done < loop_times)
    					{
    						loops_done++;
    						new Thread(play).start();
    					}
    				}
    			}
    			catch (Exception ex) { ex.printStackTrace(); }
    		}
    	};
     
    	public void stop(){
    		muted = true;
    		//new Thread(play).start();
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to manipulate a thread?

    Have you read the API doc for the Thread class? It shows the constructors and methods the class has that be used to "manipulate" the thread.

    All code executes on some thread. There are lots of thread being executed when a java program executes.
    The jvm uses one to start a program's execution. The jvm uses another thread to control the updating of the GUI. A program can create its own threads that can be used to execute methods in some class.

    is there a way to manipulate this thread
    Please explain what "manipulate" means? One thread can change the value of a variable that can be seen by other threads while they are executing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How to manipulate a thread?

    Quote Originally Posted by Norm View Post
    Please explain what "manipulate" means? One thread can change the value of a variable that can be seen by other threads while they are executing.
    I want to set the variable muted to true in the class PlaySound so the music file playing would stop.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to manipulate a thread?

    The code should be able to do that.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How to manipulate a thread?

    Quote Originally Posted by Norm View Post
    The code should be able to do that.
    but how do i do it? I tried doing PlaySound.mute = true; but it does not work? how would you do it?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to manipulate a thread?

    it does not work?
    Please explain. Post some code that can be executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How to manipulate a thread?

    I believe the code is not executed or perhaps not applied to the specific thread because the music keeps on playing!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to manipulate a thread?

    Add a println() statement that prints out the value of the variable to see what each thread sees when it executes.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How to manipulate a thread?

    Here is the main.java
    import java.io.File;
     
    public class main {
     
    	public static void main(String[] args) {
     
    		PlaySound.sound = new File("res/music.wav");
    		new Thread(PlaySound.play).start();
    		System.out.println(PlaySound.muted);
    		try{
    		Thread.sleep(5000);
    		}catch(Exception e){}
     
    		PlaySound.muted = true;
    		System.out.println(PlaySound.muted);
    	}
     
    }

    The PlaySound class
    import java.io.*;
    import javax.sound.sampled.*;
     
    public class PlaySound
    {
     
    	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(); }
    		}
    	};
    }


    However, after 5 seconds, the music keeps on playing even though I muted it. The value of muted does change, but music keeps on playing.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to manipulate a thread?

    Is the value of the variable changed? How does changing the value of a variable cause the music to be muted?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How to manipulate a thread?

    ..I don't think it affects the code until the same Thread is recalled where now muted = falsed .

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to manipulate a thread?

    When is the variable's value changed? When is that new value used to change the behavior of the executing thread?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: September 24th, 2013, 04:18 PM
  2. How to manipulate a value
    By joe132 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 22nd, 2013, 06:19 PM
  3. How to open and manipulate another program?
    By TKL in forum Object Oriented Programming
    Replies: 6
    Last Post: December 31st, 2012, 06:38 AM
  4. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM
  5. How Manipulate HTTP response?
    By nikos in forum Java Networking
    Replies: 1
    Last Post: October 7th, 2010, 12:22 PM