Results 1 to 4 of 4
- 01-14-2013, 11:14 PM #1
Member
- Join Date
- Feb 2012
- Location
- Dublin
- Posts
- 29
- Rep Power
- 0
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(); } }
- 01-15-2013, 12:14 AM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
- 01-15-2013, 12:42 AM #3
Member
- Join Date
- Feb 2012
- Location
- Dublin
- Posts
- 29
- Rep Power
- 0
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.
- 01-15-2013, 12:44 AM #4
Member
- Join Date
- Feb 2012
- Location
- Dublin
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
Why will this not build properly?
By Jamcob in forum New To JavaReplies: 11Last Post: 01-06-2012, 09:15 PM -
Playing an audiostream in an applet.
By adrianb in forum Java AppletsReplies: 16Last Post: 03-06-2011, 02:29 AM -
forwarding, rewinding , stopping an audiostream using client server
By Olla89 in forum Advanced JavaReplies: 0Last Post: 04-24-2010, 08:27 PM -
how to getText() properly?
By javamula in forum New To JavaReplies: 12Last Post: 09-16-2009, 05:45 AM -
Using accessors properly
By LifeWithJava in forum New To JavaReplies: 2Last Post: 12-23-2008, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks