Package javax.sound.sampled

Examples of javax.sound.sampled.Clip


    /**
     * 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


   * @param path - the path of the audio file to be loaded
   * @return the Clip object related to that audio file.
   */
  private Clip loadClip(String path) {
    AudioInputStream audio;
    Clip cl = null;
    URL url = this.getClass().getResource(path);

    try {
      audio = AudioSystem.getAudioInputStream(url);
      cl = AudioSystem.getClip()
      cl.open(audio);
      audio.close();
    } catch (UnsupportedAudioFileException e) {
      JOptionPane.showMessageDialog(null, GameStrings.AUDIO_FORMAT_ERROR, GameStrings.ERROR, JOptionPane.ERROR_MESSAGE);
    } catch (IOException e) {
      JOptionPane.showMessageDialog(null, GameStrings.IO_ERROR, GameStrings.ERROR, JOptionPane.ERROR_MESSAGE);
View Full Code Here

   * Reproduces the relative audio file continuously.
   *
   * @param soundName - the String relative to the clip in the Map
   */
  public void playLoop(String soundName) {
    Clip cl = this.getSound(soundName);
    cl.loop(Clip.LOOP_CONTINUOUSLY);
  }
View Full Code Here

   * Reproduces the relative audio file one time.
   *
   * @param soundName - the String relative to the clip in the Map
   */
  public void playOnce(String soundName) {
    Clip cl = this.getSound(soundName);
    cl.setFramePosition(0);
    cl.start();
  }
View Full Code Here

   * Stops the reproduction of the relative audio file.
   *
   * @param soundName - the String relative to the clip in the Map
   */
  public void stop(String soundName) {
    Clip cl = this.getSound(soundName);
    cl.stop()
  }
View Full Code Here

    gap = 0;
  }
 
  public static void load(String path, String key) {
    if(clips.get(key) != null) return;
    Clip clip;
    try {
      AudioInputStream ais =
        AudioSystem.getAudioInputStream(
          new BufferedInputStream(
            JukeBox.class.getResourceAsStream(path)
          )
        );
      AudioFormat baseFormat = ais.getFormat();
      AudioFormat decodeFormat = new AudioFormat(
        AudioFormat.Encoding.PCM_SIGNED,
        baseFormat.getSampleRate(),
        16,
        baseFormat.getChannels(),
        baseFormat.getChannels() * 2,
        baseFormat.getSampleRate(),
        false
      );
      AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais);
      clip = AudioSystem.getClip();
      clip.open(dais);
     
     
      clips.put(key, clip);
    }
    catch(Exception e) {
View Full Code Here

    play(s, 0);
  }
 
  public static void play(String s, int i) {
    if(mute) return;
    Clip c = clips.get(s);
    if(c == null) return;
    if(c.isRunning()) c.stop();
    c.setFramePosition(i);
    while(!c.isRunning()) c.start();
  }
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  synchronized void playSound( final String strURL) {
        new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments
          public void run() {
            try {
              Clip clp = AudioSystem.getClip();
              AudioInputStream inputStream = AudioSystem.getAudioInputStream(new FileInputStream(strURL));
              clp.open(inputStream);
              clp.start();
            } catch (Exception e) {
              System.err.println(e.getMessage());
            }
          }
        }).start();
View Full Code Here

            InputStream is = Demo.class.getResourceAsStream("/synth02.wav");
            AudioInputStream stream = AudioSystem.getAudioInputStream(is);
            AudioFormat format = stream.getFormat();

            DataLine.Info info = new DataLine.Info(Clip.class, format);
            Clip clip = (Clip) AudioSystem.getLine(info);

            clip.open(stream);
            clip.start();
        } catch (UnsupportedAudioFileException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (LineUnavailableException e) {
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.