Results 1 to 10 of 10

Thread: Stop Music Loop

  1. #1
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Stop Music Loop

    Hello everyone, I have added a music .wav files that loops.. But I can't manage to stop the loop when I want.
    I call Sound.stop() but it does not work. Here is my sound class:
    Java Code:
    import java.io.File;
    
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    
    public class Sound {
    	
        public static synchronized void play(final String fileName)
        {
       
            // Note: use .wav files            
            new Thread(new Runnable() {
                public void run() {
                    try {
                        Clip clip = AudioSystem.getClip();
                        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("res/" + fileName));
                        clip.open(inputStream);
                        clip.start();
                   
                    } catch (Exception e) {
                        System.out.println("play sound error: " + e.getMessage() + " for " + fileName);
                    }
                }
            }).start();
        
        }
        public static synchronized void loop(final String fileName)
        {
        	
            new Thread(new Runnable() {
                public void run() {
                    try {
                    	Clip clip = AudioSystem.getClip();
                        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("res/" + fileName));
                        clip.open(inputStream);
                        clip.loop(99);
                   
                    } catch (Exception e) {
                        System.out.println("play sound error: " + e.getMessage() + " for " + fileName);
                    }
                }
            }).start();
        	
        	
        }
        public static synchronized void stop() throws LineUnavailableException{
        	Clip clip = AudioSystem.getClip();
    		clip.stop();
    		clip.flush();
    	}
    }
    I have a dream, that one day, I will not rage quit every time I encounter an error I can't fix!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Stop Music Loop

    I don't have any experience with the Audio API.

    And I am not an expert on thread programming but it seems to me that if you start a thread in
    a synchronized method, then that thread will continue to hold the lock. Since your stop method
    and loop method are synchronized on the same lock, I wonder if the thread is capable of being
    interrupted?

    Have you verified that the stop() method is being entered (hint: try some print statements)?
    Have you verified that the clip you are trying to stop is the clip you are playing (hint: try some print statements)?
    Have you fully read and understood the Audio API documentation that you are using?
    Do you understand the intricacies of thread programming? Thread programming problems can be
    subtle and difficult to diagnose for even the experienced thread programmer.

    And I believe I recommended to you the other day you should not be using static everywhere. That
    recommendation still stands.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Stop Music Loop

    Can you post the code with a main() method that you use to test the Sound class?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Stop Music Loop

    Quote Originally Posted by jim829 View Post
    I don't have any experience with the Audio API.

    And I am not an expert on thread programming but it seems to me that if you start a thread in
    a synchronized method, then that thread will continue to hold the lock. Since your stop method
    and loop method are synchronized on the same lock, I wonder if the thread is capable of being
    interrupted?
    That isn't a problem: the Thread.start() method starts a new thread and it doesn't 'inherit' a synchronized property from its caller. The op should save the Clip object in the Sound class and stop that particular instance (not create a new one and try to stop that one).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Stop Music Loop

    Quote Originally Posted by jim829 View Post
    I don't have any experience with the Audio API.

    And I am not an expert on thread programming but it seems to me that if you start a thread in
    a synchronized method, then that thread will continue to hold the lock. Since your stop method
    and loop method are synchronized on the same lock, I wonder if the thread is capable of being
    interrupted?

    Have you verified that the stop() method is being entered (hint: try some print statements)?
    Have you verified that the clip you are trying to stop is the clip you are playing (hint: try some print statements)?
    Have you fully read and understood the Audio API documentation that you are using?
    Do you understand the intricacies of thread programming? Thread programming problems can be
    subtle and difficult to diagnose for even the experienced thread programmer.

    And I believe I recommended to you the other day you should not be using static everywhere. That
    recommendation still stands.

    Regards,
    Jim
    Yes, I have tried printing in console. But the sound does not stop. And for the static thing - this code is not mine, I found it on the internet, I don't think I am ready for researching audio api but I wanted to add sound.
    I have a dream, that one day, I will not rage quit every time I encounter an error I can't fix!

  6. #6
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Stop Music Loop

    Sound.loop("loop.wav");
    This is how start the sound.
    try {
    Sound.stop();
    } catch (LineUnavailableException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    This is how i try to stop it.
    I have a dream, that one day, I will not rage quit every time I encounter an error I can't fix!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Stop Music Loop

    Read my previous reply: the code is trying to stop the wrong Clip.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Stop Music Loop

    Quote Originally Posted by JosAH View Post
    a new thread and it doesn't 'inherit' a synchronized property from its caller.
    OK. That's the part I wasn't certain about.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Stop Music Loop

    Quote Originally Posted by JosAH View Post
    Read my previous reply: the code is trying to stop the wrong Clip.

    kind regards,

    Jos
    As I said, I have no previous experience with threads and audio api so I have no idea how to stop the correct clip. I've tried searching on google, with no success. Can you give me a hint or a headstart so I can do it?
    I have a dream, that one day, I will not rage quit every time I encounter an error I can't fix!

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Stop Music Loop

    A single clip object (the one you are playing) needs to be accessible from any thread.
    So try making it an instance field and then access it in your thread. Don't create a new instance.

    There may be other issues.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Can you have two options to stop a do...while loop?
    By untamed.ornithoid in forum New To Java
    Replies: 3
    Last Post: 10-11-2014, 10:58 AM
  2. Trying to stop a 'for' loop
    By monster10888 in forum New To Java
    Replies: 3
    Last Post: 10-11-2012, 08:41 PM
  3. break or stop for loop
    By mitra in forum New To Java
    Replies: 7
    Last Post: 09-12-2011, 11:39 PM
  4. how to stop for loop
    By rjagan in forum New To Java
    Replies: 3
    Last Post: 04-01-2011, 04:15 PM
  5. help!, while loop wont stop!
    By crank6015 in forum New To Java
    Replies: 4
    Last Post: 03-24-2011, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •