2015-09-02 1 views
1

j'ai un tableau rempli avec les objets javax.sound.sampled.Line.Info de tous les microphones attatchedComment jouer/son record sur/d'un javax.sound.sampled.Line

Info[] sourceInfos = AudioSystem.getSourceLineInfo(Port.Info.MICROPHONE); 

aide les i peuvent obtenir les lignes de tous les microphones

for (Info sourceInfo : sourceInfos) { 
    Line sourceLine = AudioSystem.getLine(sourceInfo); 
    // record sound from those lines 
} 

va de même pour le haut-parleur

Info[] sourceInfos = AudioSystem.getSourceLineInfo(Port.Info.SPEAKER); 
for (Info sourceInfo : sourceInfos) { 
    Line sourceLine = AudioSystem.getLine(sourceInfo); 
    // play sound on those lines 
} 

maintenant, je dois juste f igure comment jouer du son sur une Ligne et comment enregistrer du son à partir d'une Ligne. C'est là que je suis resté bloqué et que je n'ai pas trouvé de solution.

Donc juste pour l'avoir dit, la question est, comment puis-je lire/écrire sur une ligne?
Merci
Baschdi

Répondre

1

Vous pouvez essayer cet exemple pour capturer et lire des fichiers audio. Il est basé sur les exemples fournis dans les documents de l'API Java Sound.

Voici les ressources dont vous pouvez vous référer à:

  1. Accessing Audio System Resources
  2. Capturing audio.
  3. Playing audio.

    import java.io.ByteArrayOutputStream; 
    import java.io.IOException; 
    import javax.sound.sampled.AudioFormat; 
    import javax.sound.sampled.AudioInputStream; 
    import javax.sound.sampled.AudioSystem; 
    import javax.sound.sampled.DataLine; 
    import javax.sound.sampled.LineUnavailableException; 
    import javax.sound.sampled.Mixer; 
    import javax.sound.sampled.SourceDataLine; 
    import javax.sound.sampled.TargetDataLine; 
    
    public class Audio { 
    
    boolean stopCapture = false; 
    ByteArrayOutputStream byteArrayOutputStream; 
    AudioFormat audioFormat; 
    TargetDataLine targetDataLine; 
    AudioInputStream audioInputStream; 
    SourceDataLine sourceDataLine; 
    byte tempBuffer[] = new byte[500]; 
    
    public static void main(String[] args) { 
        Audio audio = new Audio(); 
        audio.captureAudio(); 
    
    } 
    
    private AudioFormat getAudioFormat() { 
        float sampleRate = 8000.0F; 
        int sampleSizeInBits = 16; 
        int channels = 1; 
        boolean signed = true; 
        boolean bigEndian = true; 
        return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); 
    } 
    
    private void captureAudio() { 
        try { 
    
         /* ~~~~~ UPDATE THIS PART OF CODE ~~~~~*/ 
    
         Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); //get available mixers 
         System.out.println("Available mixers:"); 
         for (int cnt = 0; cnt < mixerInfo.length; cnt++) { 
          System.out.println(mixerInfo[cnt].getName()); 
         } 
         audioFormat = getAudioFormat();  //get the audio format 
    
         DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat); 
    
         Mixer mixer = AudioSystem.getMixer(mixerInfo[3]); //getting the mixer for capture device 
    
         /* ~~~~~ UPDATE THIS PART OF CODE ~~~~~*/ 
    
         targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo); 
         targetDataLine.open(audioFormat); 
         targetDataLine.start(); 
    
         DataLine.Info dataLineInfo1 = new DataLine.Info(SourceDataLine.class, audioFormat); 
         sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo1); 
         sourceDataLine.open(audioFormat); 
         sourceDataLine.start(); 
    
         Thread captureAndPlayThread = new captureAndPlayThread(); //thread to capture and play audio 
         captureAndPlayThread.start(); 
    
        } catch (LineUnavailableException e) { 
         System.out.println(e); 
         System.exit(0); 
        } 
    } 
    
    class captureAndPlayThread extends Thread { 
    
        @Override 
        public void run() { 
         byteArrayOutputStream = new ByteArrayOutputStream(); 
         stopCapture = false; 
         try { 
          int readCount; 
          while (!stopCapture) { 
           readCount = targetDataLine.read(tempBuffer, 0, tempBuffer.length); //capture sound into tempBuffer 
           if (readCount > 0) { 
            byteArrayOutputStream.write(tempBuffer, 0, readCount); 
            sourceDataLine.write(tempBuffer, 0, 500); //playing audio available in tempBuffer 
           } 
          } 
          byteArrayOutputStream.close(); 
         } catch (IOException e) { 
          System.out.println(e); 
          System.exit(0); 
         } 
        } 
    } 
    
    } 
    

    `

Modifier: S'il vous plaît mettre à jour le code précédent avec ce code. L'extrait de code suivant sélectionne un mélangeur uniquement s'il prend en charge le microphone i.e, TargetDataLine. De même, vous pouvez le faire pour les haut-parleurs i.e, SourceDataLine.

 Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); //get available mixers 
     System.out.println("Available mixers:"); 
     Mixer mixer = null; 
     for (int cnt = 0; cnt < mixerInfo.length; cnt++) { 
      System.out.println(cnt + " " + mixerInfo[cnt].getName()); 
      mixer = AudioSystem.getMixer(mixerInfo[cnt]); 

      Line.Info[] lineInfos = mixer.getTargetLineInfo(); 
      if (lineInfos.length >= 1 && lineInfos[0].getLineClass().equals(TargetDataLine.class)) { 
       System.out.println(cnt + " Mic is supported!"); 
       break; 
      } 
     } 

     audioFormat = getAudioFormat();  //get the audio format 
     DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat); 
+0

Comment pourrais-je savoir si une table de mixage appartient à un microphone ou à un haut-parleur? – Basti

+0

J'ai ajouté du code pour sélectionner un mélangeur seulement s'il supporte le microphone. Fondamentalement, vous devez itérer sur les mélangeurs pour cela. Pour l'instant, vérifiez le code. Et une chose à noter est que le code que vous essayez d'implémenter à l'aide de l'interface Line doit utiliser DataLine. Une façon était d'utiliser Mixer, que je vous ai montré. S'il vous plaît lire le lien pour mieux comprendre la classe heirarchy https://docs.oracle.com/javase/tutorial/sound/sampled-overview.html –