Results 1 to 4 of 4
  1. #1
    genzm is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Sequentially playing audio clips

    Hy, I want to play a string of numbers as an audio track in a java application. I got audio tracks for all numbers (0-9). The input is an array of Strings. Each string contains 1 number. I use a Hashmap to map each string to an audio clip. All input strings reference a valid audio file. The problem does not concern finding the right tracks. My problem lies with playing them one after the other.

    I use a recursive function for this (see the code below). The ClipEndListener has a very simple implementation of the update function and will start the next part once the previous part has finished.

    Java Code:
    import java.util.HashMap;
    import java.util.Map;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineEvent;
    import javax.sound.sampled.LineEvent.Type;
    import javax.sound.sampled.LineListener;
    
    public class AuditivePlayer {
        public static final Map<String, AudioInputStream> MULT_SAMPLES = new HashMap<String, AudioInputStream>();
        static {
            try {
                MULT_SAMPLES.put("0", AudioSystem.getAudioInputStream(Cloud.class.getResource("/sounds/0.wav")));
                MULT_SAMPLES.put("1", AudioSystem.getAudioInputStream(Cloud.class.getResource("/sounds/1.wav")));
                MULT_SAMPLES.put("2", AudioSystem.getAudioInputStream(Cloud.class.getResource("/sounds/2.wav")));
                /* ... */
            } catch(Exception e) {
                /* catching exception */
            }
        }
    
        private String[] parts;
        private int index;
        private Clip currentClip;
    
        public void playArray(String[] parts) {
            index = 0;
            this.parts = parts;
            playPart();
        }
    
        private void playPart() {
            if (index == parts.length)
                return;
    
            try {
                currentClip = AudioSystem.getClip();
                currentClip.open(MULT_SAMPLES.get(parts[index]));
                System.out.println("playing part: " + index + " ("+ parts[index] +").");
                index++;
                currentClip.addLineListener(new ClipEndListener());
                currentClip.start();
            } catch (Exception ex) {
                System.err.println(ex.getMessage());
                ex.printStackTrace();
            }
        }
    
        private class ClipEndListener implements LineListener {
            @Override
            public void update(LineEvent event) {
                if (event.getType() == Type.STOP)
                    playPart();
            }
        }
    }
    This seems ok to me, and it works for the first array of strings. Unfortunately, when getting new arrays of strings (playArray is called for the second time), it only plays one or two parts, instead of the entire array. Any suggestions why this could be?

    Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Sequentially playing audio clips

    From where are you calling playArray? Why are you using recursion for this? Re-invoking the method inside an event handler is probably not the best way to go about this. My recommendation would be to use the event handler to set a flag which can then be checked else where in the program. You can also use the same instance of the handler, just reset the flag elsewhere in your program.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    genzm is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Sequentially playing audio clips

    Quote Originally Posted by jim829 View Post
    From where are you calling playArray? Why are you using recursion for this? Re-invoking the method inside an event handler is probably not the best way to go about this. My recommendation would be to use the event handler to set a flag which can then be checked else where in the program. You can also use the same instance of the handler, just reset the flag elsewhere in your program.

    Regards,
    Jim
    Hy, thanks for your reply.
    But isn't the whole point of listeners that you don't have to do an active check? Because what you now are suggesting is that I write something like this:
    Java Code:
    while(condition) {
        if (flag) {
            index++;
            playPart();
        }
    }
    But this loop was exactly what I was trying to avoid.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Sequentially playing audio clips

    No. The whole point of listeners is to inform you about an event so you can take appropriate action. If it doesn't take too long, the action may done in the listener. But events are dispatched in the event dispatch thread (EDT). So while one event is being processed, the others have to wait. Sometimes a listener will kick off another thread in which a task can run. Your recursive call within the handler may be contributing to your problem.

    In your case above, you could sleep for say 100 milliseconds outside of the if block so that you aren't looping a lot. And you should probably make flag volatile since it is being set in another thread.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. JMF Playing an audio error
    By vivekme77 in forum New To Java
    Replies: 2
    Last Post: 02-11-2013, 04:28 AM
  2. How to clear Audio Clips from memory
    By JagFig in forum Advanced Java
    Replies: 0
    Last Post: 11-01-2012, 03:45 AM
  3. Playing audio clips
    By robs in forum Threads and Synchronization
    Replies: 24
    Last Post: 06-26-2012, 07:14 PM
  4. Video clips in side bar start playing automagically.
    By JosAH in forum Suggestions & Feedback
    Replies: 10
    Last Post: 05-26-2011, 09:01 AM
  5. playing audio in J-App??
    By ashton in forum New To Java
    Replies: 3
    Last Post: 01-30-2009, 08:50 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
  •