Results 1 to 2 of 2
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default Need help understanding Java Sound

    I'm trying to learn about how to do sound in java, but I'm having some troubles getting through this code I found on the Internet. My main problem is not being able to find where the byte array in it receives the actual raw data from the .wav file I'm reading from.

    If anyone could help me take apart this code, I'd be grateful to them :3

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.sound.sampled.*;
    
    public class Audiotest2
    {
      public AudioFormat audioFormat;
      public AudioInputStream audioInputStream;
      public SourceDataLine sourceDataLine;
      public File soundFile;
      public DataLine.Info dataLine;
      public byte tempBuffer[] = new byte[10000];
    
      public static void main(String[] args)
      {
        new Audiotest2();
      }
    
    
      public Audiotest2()
      {
        playAudio();
      }
    
      public void playAudio()
      {
        try
        {
          soundFile = new File("90mm cannon.wav");
          audioInputStream = AudioSystem.getAudioInputStream(soundFile);
          audioFormat = audioInputStream.getFormat();
    
          dataLine = new DataLine.Info(SourceDataLine.class,audioFormat);
    
          sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLine);
          sourceDataLine.open(audioFormat);
          sourceDataLine.start();
    
          int cnt;
    
          while((cnt = audioInputStream.read(tempBuffer,0,tempBuffer.length))
            != -1)
          {
            if(cnt > 0)
            {
              sourceDataLine.write(tempBuffer, 0, cnt);
            }
          }
          sourceDataLine.drain();
          sourceDataLine.close();
        }
        catch (Exception e)
        {
          e.printStackTrace();
          System.exit(0);
        }
      }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    the following lines read stuff from the audio input stream (e.g. file) into a temporary buffer, up to in this case 10000 bytes at a time, and writes them to the audio player
    Java Code:
    while((cnt = audioInputStream.read(tempBuffer,0,tempBuffer.length))
            != -1)
          {
            if(cnt > 0)
            {
              sourceDataLine.write(tempBuffer, 0, cnt);
            }
          }

Similar Threads

  1. Sound in java applet - please help
    By LinuxDev@Flanders in forum Java Applets
    Replies: 3
    Last Post: 08-16-2010, 11:26 AM
  2. Replies: 1
    Last Post: 05-19-2010, 08:45 AM
  3. Calibration of Sound in Java
    By satishrah@gmail.com in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 02:44 PM
  4. uploading sound file in java
    By po0oker in forum Advanced Java
    Replies: 8
    Last Post: 11-04-2007, 12:00 AM
  5. Sound in applet Java
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 06:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •