Comment lire un son (alerte) dans une application java? [dupliquer]


Cette question a déjà une réponse ici:

Je travaille avec un logiciel de gestion de SMS basé sur java et je veux émettre un bip / alerte chaque fois que nous recevons un message. J'ai essayé de regarder le java.bibliothèques sonores et ne pouvait pas trouver quoi que ce soit. Je ne sais pas si aller l'applet façon de jouer un son bien dans une application java! Existe-il des prédéfinis sons dans toutes les bibliothèques java que nous pouvons appeler dans une application? Tous les pointeurs seront appréciés!

Author: Krithika, 2010-09-23

5 answers

Si vous voulez juste un bip ou une alerte rapide, essayez

Toolkit.getDefaultToolkit().beep();
 42
Author: Sean, 2010-09-23 16:24:54

Vous pouvez générer votre propre son si vous cherchez quelque chose de moins ennuyeux qu'un beep() sans fichier son externe.

import javax.sound.sampled.*;

public class SoundUtils {

  public static float SAMPLE_RATE = 8000f;

  public static void tone(int hz, int msecs) 
     throws LineUnavailableException 
  {
     tone(hz, msecs, 1.0);
  }

  public static void tone(int hz, int msecs, double vol)
      throws LineUnavailableException 
  {
    byte[] buf = new byte[1];
    AudioFormat af = 
        new AudioFormat(
            SAMPLE_RATE, // sampleRate
            8,           // sampleSizeInBits
            1,           // channels
            true,        // signed
            false);      // bigEndian
    SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
    sdl.open(af);
    sdl.start();
    for (int i=0; i < msecs*8; i++) {
      double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
      buf[0] = (byte)(Math.sin(angle) * 127.0 * vol);
      sdl.write(buf,0,1);
    }
    sdl.drain();
    sdl.stop();
    sdl.close();
  }

  public static void main(String[] args) throws Exception {
    SoundUtils.tone(1000,100);
    Thread.sleep(1000);
    SoundUtils.tone(100,1000);
    Thread.sleep(1000);
    SoundUtils.tone(5000,100);
    Thread.sleep(1000);
    SoundUtils.tone(400,500);
    Thread.sleep(1000);
    SoundUtils.tone(400,500, 0.2);

  }
}

Plus d'expériences sonores ici: Produire un effet sonore spécial

 27
Author: RealHowTo, 2011-07-14 21:17:46

Vous pouvez jeter un œil à la méthode bip dans la classe Toolkit, comme indiqué ici

 6
Author: npinti, 2010-09-23 16:20:51

La route de l'applet devrait être correcte (et est très simple). Pour éviter de créer une instance d'Applet vous pouvez utiliser le statique newAudioClip méthode, puis appelez play() sur le AudioClip créé.

URL url = getClass().getResource("/foo/bar/sound.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();
 3
Author: Adamski, 2010-09-23 16:22:17

Si vous souhaitez utiliser le package son pour lire un fichier son arbitraire, vous pouvez utiliser le package javax.sound.sampled. Voici le code qui va lire un fichier son:

private void playSound(File f) {
    Runnable r = new Runnable() {
        private File f;

        public void run() {
            playSoundInternal(this.f);
        }

        public Runnable setFile(File f) {
            this.f = f;
            return this;
        }
    }.setFile(f);

    new Thread(r).start();
}

private void playSoundInternal(File f) {
    try {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(f);
        try {
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            try {
                clip.start();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                clip.drain();
            } finally {
                clip.close();
            }
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } finally {
            audioInputStream.close();
        }
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 1
Author: Erick Robertson, 2011-07-14 21:01:36