Results 1 to 4 of 4
  1. #1
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default using audiostream and audioplayer properly

    Hi,

    When I use audiostream and audioplayer to play a sound effect, my program creates two problematic threads that I dont know how to end. I know this becasue I can see all the running threads on my debugger.
    One is a mixer thread (pulse audio)
    and another is the audioplayer thread.
    All the other threads are behaving properly, however these two threads, relate to audio stream and hog the cpu (keeping it around 6%) even after I close all my input streams and explicitly call stop on the audio player.
    I am using Lubuntu linux, with my mixer controlled by pulse audio.

    So I would like to know how to properly use the audiostream and audioplayer classes to playback sound effect wav's.


    Here is an short example to try in the Netbeans Debugger:

    Java Code:
    /*
     * Proper AudioStream Player Behaviour?
     */
    package player;
    
    import java.io.FileInputStream;
    import java.io.InputStream;
    import javax.swing.JOptionPane;
    import sun.audio.AudioPlayer;
    import sun.audio.AudioStream;
    
    /**
     *
     * @author aubrey
     */
    public class Player {
    
        boolean isJet=true;
        InputStream in;
        AudioStream as;
        Thread t;
        
       public Player(){
        
    		t = new Thread(){
                    
                    public void run(){
                     
                        
                    	while(isJet){
    			
    			in = Player.class.getResourceAsStream("/sounds/jetstart2.wav");  
                            try {
                                // Create an AudioStream object from the input stream.
                                //@SuppressWarnings("restriction")
                                as = new AudioStream(in);
                            
    
    		// Use the static class member "player" from class AudioPlayer to play
    		// clip.
                  
    		AudioPlayer.player.start(as);
                            
                                while(as.available()!=0)
                                {
                                System.out.print("Playing... ");
                                }
                           
                    
                            
                                //close the AudioStream
                                   in.close();
                                   System.out.println("closed input stream");
                        
                            
                        AudioPlayer.player.stop(as);
                        System.out.println("stopped audio stream");
                           
                                as.close();
                                System.out.println("closed audioStream");
                           
                                //end this current thread
                                isJet=false;
                            }catch(Exception e){System.out.println(e);}
                    
                        
                             
                        
                        
                   
                 
                    }
              
                    }
                    };
                      t.start(); 
                      
                      JOptionPane.showMessageDialog(null, new String("Troublesome Threads:\n 'Audio Player' running\n 'PulseAudio Eventloop Thread' running"));
                      
                    }        
                            
        public static void main(String[] args) {
           new Player();
        }
    }

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: using audiostream and audioplayer properly

    I can see and reproduce what you mean - I do not think there is an easy way to completely stop the other thread. It seems to me it is the mixing thread (Daemon thread) that would go on mixing as long as the session is running.
    I like likes!

  3. #3
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: using audiostream and audioplayer properly

    You may be right about the mixing thread.

    I think its possible the mixing thread which is handled by the OS could be related the the audio stream player.

    So maybe we should focus on a way to stop the player thread called 'Audio Player' from running well after the wav has completed playback.

  4. #4
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: using audiostream and audioplayer properly

    Does anyone know of a way to stop the AudioStream Player Thread?
    I have closed my inputStream and audioStream and also called AudioPlayer.player.stop(audioStream)
    But, obviously its not enough surely...

Similar Threads

  1. Why will this not build properly?
    By Jamcob in forum New To Java
    Replies: 11
    Last Post: 01-06-2012, 09:15 PM
  2. Playing an audiostream in an applet.
    By adrianb in forum Java Applets
    Replies: 16
    Last Post: 03-06-2011, 02:29 AM
  3. Replies: 0
    Last Post: 04-24-2010, 08:27 PM
  4. how to getText() properly?
    By javamula in forum New To Java
    Replies: 12
    Last Post: 09-16-2009, 05:45 AM
  5. Using accessors properly
    By LifeWithJava in forum New To Java
    Replies: 2
    Last Post: 12-23-2008, 02:49 PM

Tags for this Thread

Posting Permissions

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