Package javax.sound.sampled

Examples of javax.sound.sampled.Clip


            synchronized(queue){queue.notifyAll();}
        }
 
        public final void run()
        {
            Clip clip=null;
            byte[] data=null;
            AudioFormat format=null;
            while (isRunning)
            {
                synchronized(queue)
                {
                    if (queue.isEmpty())
                    {
                        if(isRunning)
                        {
                            try
                            {
                                queue.wait();
                            }
                            catch(InterruptedException e)
                            {//bug when thrown? called when interrupted
                                e.printStackTrace();
                                return;
                            }
                        }
                        continue;
                    }
                    else
                    {
                        if(singleLine)
                        {
                            Object[] temp =(Object[]) queue.removeFirst();
                            format=(AudioFormat)temp[0];
                            data = (byte[])temp[1];
                        }
                        else clip = (Clip)queue.removeFirst();
                    }
                }
                if(singleLine)
                {
                    playSingleLine(data, format);
                }
                else
                {
                    clip.setFramePosition(0);
                    if(clip.isControlSupported(FloatControl.Type.MASTER_GAIN))
                    {
                        FloatControl flc = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
                        float volume = Preferences.getInteger("sound","volume",0);
                        if (volume >flc.getMaximum())
                        {
                            volume=flc.getMaximum();
                        }
                        flc.setValue(volume);
                    }
                    clip.start();
                }
            }
            //cleanup
            if(!singleLine)
            {
View Full Code Here


        {
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            AudioInputStream stream = new AudioInputStreambais, format,data.length / format.getFrameSize());

            Line.Info linfo = new Line.Info (Clip.class);
            Clip clip=null;
            try
            {
                clip = (Clip)AudioSystem.getLine(linfo);
                clip.open(stream);
            } catch (LineUnavailableException e3)
            {
                System.out.println(I18N.gettext("sound.Problem_playing_sound_check_your_sound_system"));
            } catch (IOException e3)
            {
                // TODO Auto-generated catch block
                e3.printStackTrace();
            }
       
            if(clip.isControlSupported(FloatControl.Type.MASTER_GAIN))
            {
                FloatControl flc = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
                float volume = Preferences.getInteger("sound","volume",0);
                if (volume >flc.getMaximum())
                {
                    volume=flc.getMaximum();
                }
         
                flc.setValue(volume);
            }
         
            //clip.setFramePosition(0);
            clip.start()
            try
            {//wait until clip finishes and then close it so it works on OS/2
                sleep(clip.getMicrosecondLength()/1000);
                clip.stop();
                clip.close();
                clip=null;
            } catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
View Full Code Here

                        clip.start();
                        return true;
                    }
                }
                try {
                    Clip clip = AudioSystem.getClip();
                    clip.addLineListener(this);
                    AudioInputStream stream = null;
                    try {
                        stream = AudioSystem
                            .getAudioInputStream(fileOrURL.asURL());
                    } catch (IOException ex) {
                        // Handle jar urls from WebStart or the installer
                        try {
                            URL possibleJarURL = ClassUtilities.jarURLEntryResource(fileOrURL.getExpression());

                            stream = AudioSystem
                                .getAudioInputStream(possibleJarURL);
                        } catch (Exception ex2) {
                            IOException ioException = new IOException("Failed to open \""
                                    + fileOrURL.getExpression() + "\".");
                            ioException.initCause(ex);
                            throw ioException;
                        }
                    }

                    clip.open(stream);
                    clip.start();
                    _clips.add(clip);
                } catch (Exception e) {
                    throw new IllegalActionException(this, e,
                            "Error opening audio file or URL: "
                                    + fileOrURL.getExpression());
                }
            } else {
                // Restart the last clip.
                Clip clip = _clips.get(_clips.size() - 1);
                // NOTE: Possible race condition: could become inactive
                // before the stop() is called, which could result in
                // two stop notifications to the update() method.
                // Will the Clip give to stop notifications?
                if (clip.isActive()) {
                    clip.stop();
                }
                clip.setFramePosition(0);
                clip.start();
            }
        }
        return true;
    }
View Full Code Here

  public void play(String fileName) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    URL url = new UrlUtil().getUrl(fileName);
    AudioInputStream stream = AudioSystem.getAudioInputStream(url);     
    AudioFormat format = stream.getFormat();
    DataLine.Info info = new DataLine.Info(Clip.class, format);     
    Clip clip = (Clip) AudioSystem.getLine(info);        
    clip.open(stream);     
    clip.start();
  }
View Full Code Here

        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      AudioInputStream audioInputStream = AudioSystem
          .getAudioInputStream(url);
      Clip clip = AudioSystem.getClip();
      clip.open(audioInputStream);
      clip.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here

    /**
     * Plays the file for duration milliseconds or loops.
     */
    private void play(Project project, File file, int loops, Long duration) {

        Clip audioClip = null;

        AudioInputStream audioInputStream = null;


        try {
            audioInputStream = AudioSystem.getAudioInputStream(file);
        } catch (UnsupportedAudioFileException uafe) {
            project.log("Audio format is not yet supported: "
                + uafe.getMessage());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        if (audioInputStream != null) {
            AudioFormat format = audioInputStream.getFormat();
            DataLine.Info   info = new DataLine.Info(Clip.class, format,
                                             AudioSystem.NOT_SPECIFIED);
            try {
                audioClip = (Clip) AudioSystem.getLine(info);
                audioClip.addLineListener(this);
                audioClip.open(audioInputStream);
            } catch (LineUnavailableException e) {
                project.log("The sound device is currently unavailable");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (duration != null) {
                playClip(audioClip, duration.longValue());
            } else {
                playClip(audioClip, loops);
            }
            audioClip.drain();
            audioClip.close();
        } else {
            project.log("Can't get data from file " + file.getName());
        }
    }
View Full Code Here

  public static synchronized void playSound(final String path) {
      new Thread(new Runnable() {
        public void run() {
          try {
            //load clip
            Clip clip = AudioSystem.getClip();
           
            InputStream audiosrc = getClass().getResourceAsStream(path);
            InputStream bufferedIn = new BufferedInputStream(audiosrc);
            AudioInputStream ai = AudioSystem.getAudioInputStream(bufferedIn);
            clip.open(ai);
           
            //volume control
            FloatControl control = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            //reduce volume by 30 dec
            control.setValue(-20.0f);
           
            clip.start();
           
          } catch (Exception e) {
            System.err.println(e.getMessage());
          }
        }
View Full Code Here

    private void playHarlem() {
        try {
            if(!harlemWav.isFile() && allowDownload)
                saveHarlem(harlemWav);
            Clip clip = AudioSystem.getClip();
            if(harlemWav.isFile()) {
                clip.open(AudioSystem.getAudioInputStream(harlemWav));
            }
            else {
                clip.open(AudioSystem.getAudioInputStream(new URL("https://dl.dropbox.com/u/30971563/harlem.wav")));
            }
            clip.start();
        }
        catch (Exception exc) {
            exc.printStackTrace(System.out);
        }
    }
View Full Code Here

    private void playHarlem() {
        try {
            if(!harlemWav.isFile() && allowDownload)
                saveHarlem(harlemWav);
            Clip clip = AudioSystem.getClip();
            if(harlemWav.isFile()) {
                clip.open(AudioSystem.getAudioInputStream(harlemWav));
            }
            else {
                clip.open(AudioSystem.getAudioInputStream(new URL("https://dl.dropbox.com/u/30971563/harlem.wav")));
            }
            clip.start();
        }
        catch (Exception exc) {
            exc.printStackTrace(System.out);
        }
    }
View Full Code Here

  //http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java
  public static synchronized void playSound(final String strPath) {
      new Thread(new Runnable() {
        public void run() {
          try {
            Clip clp = AudioSystem.getClip();

            AudioInputStream aisStream =
                AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(strPath));
    
           
            clp.open(aisStream);
            clp.start();
          } catch (Exception e) {
            System.err.println(e.getMessage());
          }
        }
      }).start();
View Full Code Here

TOP

Related Classes of javax.sound.sampled.Clip

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.