Playing big AudioInputStream whit javax.sound.sampled

Hello!.
I've a problem, I've a big AudioInputStream (retrieved from DataBase), and when I try play It with Clip throws this Exception:
javax.sound.sampled.LineUnavailableException: Failed to allocate clip data: Requested buffer too large.
     at com.sun.media.sound.MixerClip.implOpen(MixerClip.java:536)
     at com.sun.media.sound.MixerClip.open(MixerClip.java:161)
     at com.sun.media.sound.MixerClip.open(MixerClip.java:249)
     at ClipPlayer.<init>(ClipPlayer.java:138)
     at ClipPlayer.main(ClipPlayer.java:192)
Help Me please, friends.
Thank's.

Is funny, but I now know what's the problem.
For play big AudioInputStream exists a interface in sampled package for play this files. This interface is SourceDataLine. The next code solve the problem:
AudioInputStream audio=DataBase.retrieveAudioInputStream();
AudioFormat af=DataBase.retrieveAudioFormat(); // This need's for
// reconstruct the audiofile
DataLine.Info datalineinfo=new DataLine.Info(SourceDataLine.class,af);
try
if (AudioSystem.ifLineSupported(datalineinfo))
SourceDataLine source=(SourceDataLine) AudioSystem.getLine (datalineinfo);
int total=audio.available();
source.open(af);
int buffersize=source.getBufferSize();
byte [] data=new byte[buffersize];
source.start();
int read=buffersize;
//Well, now play!!!
while (read<total)
read=audio.read(data,0,data.length);
if (read==-1) break; //End of audiostream.
source.write(data,0,read);
else
System.err.println("DataLine not supported");
catch (.....)
Handle exceptions....
That's all rigth.!!

Similar Messages

  • Best practices for using javax.sound.sampled.Clip?

    I've got a very simple Swing-based puzzle game. When the user performs certain actions, I want to play a trivial little sound. So far, I'm using the Java Sound API (javax.sound.sampled). When the actionPerformed method is called, my model is updated, which triggers an event in the same thread, and one of the listeners, plays a sound like this:
    this.clip.setFramePosition(0);
    this.clip.start()The clip had been previously initialized like this:
              InputStream in = null;
              AudioInputStream ain = null;
              Clip tempClip = null;
              try {
                   in = BubblePopSound.class.getResourceAsStream("pop2.wav");
                   ain = AudioSystem.getAudioInputStream(in);
                   try {
                        DataLine.Info info = new DataLine.Info(Clip.class, ain
                                  .getFormat());
                        tempClip = (Clip) AudioSystem.getLine(info);
                        tempClip.open(ain);
                   finally {
                        ain.close();
              catch(UnsupportedAudioFileException uafe) {
                   // TODO Log that sound own't be played
              catch(LineUnavailableException lue) {
                   // TODO Log that sound own't be played
              catch(IOException ioe) {
                   throw new RuntimeException("IOException reading sound clip", ioe);
              finally {
                   if(ain != null) try {
                        ain.close();
                   catch(IOException ioe) {}
                   if(in != null) try {
                        in.close();
                   catch(IOException ioe) {}
              this.clip = tempClip;So my goal was to load the clip once, and be able to trigger the playing of it repeatedly. Hoever, it only works intermittently. Reading through the http://www.jsresources.org site, the Java Sound homepage, forums, etc. I feel like I don't truly understand the best way to play simple audio clips like this. These questions are still lingering:
    - When I call clip.start(), does it play in the current thread, or am I triggering something to start the playing independent of the thread?
    - Do I need to call stop explicitly?
    - What if the user triggers the sound again before it has finished playing?
    - Do I need to close the clip with the playing has stopped, or is it ok to keep it "open" so its ready to play again and again?
    - If I can keep it open, do I need to worry about cleaning it up later?

    If your sound file is < 1 second long and you are using the JDK/JRE 1.5.0 (pre _02 update I think) then the sound code is bugged and is likely the cause of the sound problem. Try updating your JRE/JDK to 1.5.0_03 and try the sound again. If that's not it I can't help.
    - When I call clip.start(), does it play in the current thread, or am I triggering something to start the playing independent of the thread?Not quite sure, but it doesn't seem to be interrupted when I start playing a sound and sleep the thread. It might buffer the sound data in the sound card or it might use a different thread. (Interesting to know if someone has that knowledge...)
    - Do I need to call stop explicitly?No.
    - What if the user triggers the sound again before it has finished playing?It should play the new sound as well (but when many are going some might be cut off. It functions adequately for my game (check out Javoids on sourceforge -- need latest JRE or JDK).
    - Do I need to close the clip with the playing has stopped, or is it ok to keep it "open" so its ready to play again and again?Don't bother. Use this instead "clip.setFramePosition(0);"
    - If I can keep it open, do I need to worry about cleaning it up later?Java is garbage collected so you rarely have to be worried about that. No.

  • How to play a wav file to LINE_OUT with javax.sound.sampled

    Hello all,
    I need to play a wav file to LINE_OUT port, but not to the SPEAKER. I am trying to do this under window NT. For some reason I get false for isLineSupported(Port.Info.LINE_OUT). I am new to javax.sound so I might be doing something wrong. Here is the code that I am using to iterate through all availible mixers and check if LINE_OUT is supported by any of them:
    Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    for(int i = 0; i < mixers.length; i++){
    Mixer mixer = AudioSystem.getMixer(mixers);
    if (mixer.isLineSupported(Port.Info.LINE_OUT)) {
    System.out.println("the port is supported");
    try {
    Port line = (Port) AudioSystem.getLine(
    Port.Info.LINE_OUT);
    catch(Exception ex){
    ex.printStackTrace();
    else{
    System.out.println("the port is not supported");
    Please help

    The new bugID of the request to implement Ports is: 4558938.
    (If you voted for the old bug id: 4504642, be sure to change your votes to this new bug report since the old one was closed as a duplicate of the new one.)

  • Javax.sound.sampled - Writie PCM audio samples to an AIFF file?

    Greetings,
    I've been going at this for awhile and seem to be stuck.
    I'm importing data from an old system that stored audio recordings in raw PCM. I can easily use a Clip object to convert that PCM into a DataLine (Clip) object and play it.
    Now I need to write out that audio data as an AIFF file (actually I need to capture it and store it in a database, but the AIFF file format seems like a good, portable, format).
    To write a file using AudioSystem.write() you need an AudioInputStream. To create an AudioInputStream you need a TargetDataLine. However, I can see no direct way of creating a TargetDataLine that can be populated with raw data.
    Reading the documention (I know, it's a bad habit), it seems like what I needed to do is get a mixer, create a SourceDataLine with my data, then pump the SourceDataLine with the audio samples. The mixer would do nothing but pass the data on to the TargetDataLine where it would be captured and written to disk. Here's my code:
    public void writeAIFF( OutputStream out ) throws IOException
    try
    // First, find a mixer (we assume it will be a software mixer) that can provide us with
    // both source and target datalines that can handle the desired format.
    AudioFormat format = getAudioFormat(); // This is the format of our data
    DataLine.Info sourceInfoRequest = new DataLine.Info(SourceDataLine.class,format);
    DataLine.Info targetInfoRequest = new DataLine.Info(TargetDataLine.class,format);
    Mixer ourMixer = null;
    // Get all of the available mixers
    Mixer.Info[] info = AudioSystem.getMixerInfo();
    for (int i=0; i<info.length; i++)
    Mixer mixer = AudioSystem.getMixer(info);
    if (mixer.isLineSupported(sourceInfoRequest) && mixer.isLineSupported(targetInfoRequest))
    ourMixer = mixer;
    break;
    if (ourMixer==null)
    throw (new IOException("can't obtain audio components"));
    // Get the source and target lines from the mixer
    SourceDataLine sourceLine = (SourceDataLine)ourMixer.getLine(sourceInfoRequest);
    TargetDataLine targetLine = (TargetDataLine)ourMixer.getLine(targetInfoRequest);
    AudioInputStream targetStream = new AudioInputStream(targetLine);
    // Load up the source line with the data
    sourceLine.open(format);
    sourceLine.write(samples,0,samples.length);
    // Write our data out as an AIFF file
    AudioSystem.write(targetStream,AudioFileFormat.Type.AIFF,out);
    // So what happens to all of these lines and mixers when we're done?
    catch ( LineUnavailableException noline )
    throw (new IOException("audio line unavailable: "+noline.getMessage()));
    My problem is that the code never finds a mixer. The statement (mixer.isLineSupported(sourceInfoRequest) && mixer.isLineSupported(targetInfoRequest)) is always false.
    So here's my question (finally): Am I going about this the right way? My other thought is just to create my own TargetDataLine object and populate it with the data myself.
    Any thoughts or suggestions?
    P.S. I hope this is the right place for this question -- this is the first time I've used this forum
    P.P.S. Java 1.3.1 (MacOS X and Windows 2000)

    Solved -
    This was way over engineered! Overlooked in my reading was a constructor for an AudioInputStream that can use any input stream as it's source.
    This was all I needed:
         AudioFormat format = getOurAudioFormat();               // Get the format of our audio data
         AudioInputStream targetStream = new AudioInputStream(new ByteArrayInputStream(samples),format,samples.length);
         AudioSystem.write(targetStream,AudioFileFormat.Type.AIFF,out);

  • Record my voice using JAVAX.sound

    I want to record my voice using the javax.sound.
    Can you please give if there shortest path..
    I see javax.sound.sampled.AudioSystem
    But the sampled is confusing me.. is it a beta version and need to be developed further?
    After recording, I am interested to parse the voice.

    Hmm interesting ones, I've never programmed sound using Java myself, but have you looked at http://docs.oracle.com/javase/6/docs/technotes/guides/sound/programmer_guide/contents.html ? It seems like a good doco to get started

  • Where's javax.sound

    Hi
    i've downloaded j2se 1.4.1 but when i try to import javax.sound.* it says it can't find it. Do i need to import anything else eg java.lang or what? I'm pretty sure its not an optional package.
    Please help me

    You aren't easily distracted from a set opinion, huh?
    You got the same answer from me in that thread:
    http://forum.java.sun.com/thread.jsp?forum=31&thread=32
    334
    You'd have to return to your thread to notice,
    though,
    instead of posting a new one...
    I really appreciate your help docjekill, but even when i specify
    javax.sound.midi
    javax.sound.midi.spi
    javax.sound.sampled
    javax.sound.sampled.spi
    all of these i still get the same error.
    I've looked in scr.zip and found other packages, eg javax.swing etc, but no sound packages. Is the download incomplete or did it just stuff up?

  • Where is javax.sound?

    Compiler tells me it doesn't exist, even when setting classpath as follows:
    C:\JDK\bin\javac -classpath C:\JDK\JRC\lib\rt.jar soundApp.java ...
    I have version 1.4.1.

    How old is this application you are trying to use, or how old is the code if you are getting it from another source?
    1.3 through 1.4.1 use javax.sound.midi, javax.sound.midi.spi, javax.sound.sampled, and javax.sound.sampled.spi.
    They dont try to import javax.sound in and of themselves. This may be an old program written back when javax.sound stuff wasnt included in the API and was a part of the Java Media Framework.
    Of course, I could be totally wrong here (as I havent done anything with sound stuff in Java in quite a while.)
    Just a thought.

  • Swf won't play after uploading - white box

    I've posted a couple of previous times and gotten nothing but
    dead air, so I'm still trying to find a way to get flash movies to
    just play on a web site. Is it that hard? I hate flash. Why doesn't
    it just work?
    I'm trying to put a video gallery up and currently have eight
    .swf video clips that all begin downloading simultaneously when the
    page is opened, hogging bandwidth that should be reserved for
    whatever one video the user chooses. I can't find a way to keep the
    clips from downloading until the user clicks the play button. And I
    can't put a buffer delay in. Everything I can find on a buffer in
    Flash says to change the BufferTimer in the components inspector.
    But I HAVE NO BUFFERTIMER IN THE COMPONENTS INSPECTOR!! I've got
    Align, AutoPlay, CuePoints, etc., but no BufferTimer! I swear I
    must have a faulty version of Flash CS3.
    So I'm trying to get around it by using a gallery of images
    that link to individual pages with one .swf file. It all tests fine
    on my local machine, but once it's uploaded, the gallery images
    pull up pages with a big fat white box where the .swf should be.
    What gives?
    And while I'm ranting about the difficulties of Flash, I'll
    rave about the ease of use of Dreamweaver. Why can't Fl be more
    like Dw? ie, coding not required. I've been trying to wrestle this
    Flash gallery into reality for more than a week and I'm on the
    verge of reverting back to embedded Quicktime files.
    BTW, does anyone know of a Flash forum people actually? I
    haven't gotten a response at Creative Cow or here.
    Sorry for the rant; I'm at my wit's end.

    I am having the very same problem. When I post the swf file
    and call it up, all I see is a white box with my play button and
    stop button at the bottom. Both of these buttons work and it looks
    fine once you hit the play button. But I want the swf to come up to
    the first frame which should be a picture of a person -- not the
    white box!
    Did you ever find out how to fix this problem? I'd be ever so
    grateful to get help on this one.
    Thanks!

  • NetStream play H264/AAC flv no sound

    I used Flash builder 4.5 created a player to play recorded video from Adobe Media Server.
    Until now, mp4 is worked perfect. But now when mp4 transfer to flv, my player played the video but no sound.
    If this video transfer to H264/MP3, it will work perfect.
    I used AMS sample player to player the video and it worked perfect. Where is my pleyer wrong?
    Here is some code:
    ns:NetStream = new NetStream(nc);
    ns.addEventListener(NetStatusEvent.NET_STATUS, ns_status);
    ns.client = nsClient;
    video.attachNetStream(ns);
    ns.bufferTime = 10;
    ns.play(xxx/xxx);
    And video info get from onMetadata:
    audiocodecid: 10
    audiodatarate: 15.625
    width: 640
    videodatarate: 976.5625
    encoder: Lavf54.63.104
    height: 368
    framerate: 29.970029970029966
    filesize: 87087101
    duration: 687.488
    audiosamplerate: 8000
    audiosamplesize: 16
    stereo: false
    videocodecid: 2

    have you set these values:
    netstream.receiveAudio(true);
    netstream.receiveVideo(true);

  • Record sound sample from flash dynamic page

    Hello , I have a basic problem. I visited Emerson Lake and
    Palmers drummer Carl Palmers web page. It is www.carlpalmer.com
    When you enter the site , there appears a drumset picture. When you
    move your cursor without clicking on the drumset , it plays the
    drums. Google group members say it is a flash site . I want to
    record the sound samples at the same quality. How can I do it ? I
    tried some win audio recorder like programs but I could not
    succeed.
    Best ,
    Mustafa Umut Sarac

    Ever think that this may violate copyright law?
    If you have a legitimate use, you might try contacting the
    webmaster of the site and ask for the originals.

  • Javax.sound compare to javax.media in term of GUI

    I messed around with javax.sound these few days and my emotions have been running up and down. I was happy that i could play mp3 on my software but after i decided to move on, i found out its impossible for me to obtain the length if i'm going to stream, and mp3 files are just too huge for clips to handle. this costs me a problem in doing the GUI for my software (one like winamp, windows media player, etc, etc)
    I thinking of dropping javax.sound and moving into javax.media in hope for a much better version of sound handling. Anyone have any suggestion? or fix for my problem? I need help.
    Thanks in advance,
    KhinBoon

    You may feel disappointed if you do so, I'm afraid.
    I first tried the newly published mp3 codec for JMF.
    It seems that it doesn't work on my RedHat Linux Advance Server3 update2.
    On the other hand, I can play mpeg1 file over Internet using http protocol.
    And I can also play mp3 file on another Wndows XP.
    But the player can't realize when playing an mp3 file over the Internet.
    I think that there's still a long way to go for JMF.

  • The sound quality of my speaker suddenly got really poor when playing music or videos.  Sounds like an old AM radio or Sputnik.  Pretty sure it's hardware related but maybe not.  Thoughts on what the problem might be and if it can be repaired?

    The sound quality of my speaker suddenly got really poor when playing music or videos.  Sounds like an old AM radio or Sputnik.  Pretty sure it's hardware related but maybe not.  Thoughts on what the problem might be and if it can be repaired?

    The batterys the ipods come with are very sensetive somtimes they dont react right to the first couple of charges. Try fully charging it and then leaving it on until its completely drained a few times. If that doesnt work it might be a lemon.

  • Not sure why but when i open a new tab my top sites are no longer showing not even a toggle switch it's just one big blank white page? how can i correct this ?

    i did have my most visited sites in the pin ups yet for some reason or other not sure why but not when i open a new tab all there is, is one big blank white page. Not a toggle switch not even blank pin up thumb nails. Any help would be greatly appreciated. thank you

    See this old question:
    *[https://support.mozilla.org/en-US/questions/982051#answer-517878 /questions/982051#answer-517878]

  • I spilled some pop on the back of my iPad. I cleaned it off and everything seems to be going good but when I play music it will either play with no sound or play then continue with no sound. What can I do to fix this (without being costly, it's a iPad 2)

    I spilled some pop on the back of my iPad. I cleaned it off and everything seems to be going good but when I play music it will either play with no sound or play then continue with no sound. What can I do to fix this (without being costly, it's a iPad 2) this happens with other music apps too. Please I need help!

    You can try resetting your iPad by simultaneously pressing and holding the Home and Sleep/Wake buttons until you see the Apple Logo. This can take up to 15 seconds so be patient and don't release the buttons until the logo appears.
    Try again to see if the problem persists. If it doesn't work, there isn;t much anyone here can do for you. It may be a physical problem with your speakers.

  • I can't play any purchased movies. Sounds like it is the 6.0 update. So what is the fix? Am I just stuck not being able to use my Apple TV until...?

    I can't play any purchased movies. Sounds like it is the 6.0 update. So what is the fix? Am I just stuck not being able to use my Apple TV until...?

    Sorry I can't help but I have potentially a similar problem.
    We have an Apple TV 3 and after the most recent software update (potentially coincidental timing), our purchased movies & TV shows will no longer play for our US account. We can play previews but that's it. We also tried renting a new movie but that didn't work either. No error messsage, just select play, it thinks about it, then returns you to the same screen again.
    Any help greatly appreciated.
    Thanks

Maybe you are looking for

  • BW 3.5 authorization question - cross posting

    Hi All, My client is utilizing dynamic hier node authorizations (via customer exit) on 0EMPLOYEE, and we have come to a road block.The ABAP is processing the variable at I_step = 0 or 1, and is working perfectly. The problem is that when a query is w

  • It shuts off by itself on averae 5 times/day

    Recently my phone is shutting off by itself and when i restarted it, is either shutting off again or indicating low battery (when it was about 70%)and need to charge it right away for it to turn on. Anyone has the same problem?

  • How delta is handled ?

    Hi, We have used BPC methodology to load data from BI cube to BPC application. now what happens when we load the data from bi cube to Bpc application(assuming we have done one full load for the first time ) does it read all the data from the BI cube

  • Delete layout variant in the ALV

    Does anyone know how to delete the function 'layout variant' int the ALV of the ABAP web dynpro? So the pulldown menu won't be visible on the header of the ALV screen?

  • Half ellipse button?

    Hi, i have to make a Button which has the form of a half ellipse. I saw how i can overwrite JButton to make a round button or ellipse button, but those are all predefined forms in graphics and graphics2d. And this doesnt help me to make button with h