This discussion is archived
1 Reply Latest reply: Apr 21, 2008 3:23 AM by 807591 RSS

Playing a WAVE file after making a phone call.

807591 Newbie
Currently Being Moderated
Hi everyone..
I'm trying to play a wav file after making a phone call. I was able to make the call. But I get an exception when i try to paly a wav file..
Can any one tell me where I've gone wrong..

This is the code which plays the audio file..(I downloaded it by the way.. and it's working fine when used as a seperate program)

import java.io.File;
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.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
 
public class PlayWave extends Thread {
 
     private String filename;
 
     private Position curPosition;
 //30000000(7324.21 kb)
     private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
 
     enum Position {
          LEFT, RIGHT, NORMAL
     };
 
     public PlayWave(String wavfile) {
          filename = wavfile;
          curPosition = Position.NORMAL;
     }
 
     public PlayWave(String wavfile, Position p) {
          filename = wavfile;
          curPosition = p;
     }
 
     public void run() {
 
          File soundFile = new File(filename);
          if (!soundFile.exists()) {
               System.err.println("Wave file not found: " + filename);
               return;
          }
 
          AudioInputStream audioInputStream = null;
          try {
               audioInputStream = AudioSystem.getAudioInputStream(soundFile);
          } catch (UnsupportedAudioFileException e1) {
               e1.printStackTrace();
               return;
          } catch (IOException e1) {
               e1.printStackTrace();
               return;
          }
 
          AudioFormat format = audioInputStream.getFormat();
          SourceDataLine auline = null;
          DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
 
          try {
               auline = (SourceDataLine) AudioSystem.getLine(info);
               auline.open(format);
          } catch (LineUnavailableException e) {
               e.printStackTrace();
               return;
          } catch (Exception e) {
               e.printStackTrace();
               return;
          }
 
          if (auline.isControlSupported(FloatControl.Type.PAN)) {
     FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
               if (curPosition == Position.RIGHT)
                    pan.setValue(1.0f);
               else if (curPosition == Position.LEFT)
                    pan.setValue(-1.0f);
          } 
 
          auline.start();
          int nBytesRead = 0;
          byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
 
          try {
               while (nBytesRead != -1) {
          nBytesRead = audioInputStream.read(abData, 0, abData.length);
          if (nBytesRead >= 0)
               auline.write(abData, 0, nBytesRead);
               }
          } catch (IOException e) {
               e.printStackTrace();
               return;
          } finally {
               auline.drain();
               auline.close();
          }
 
     }
I inserted
new PlayWave("greeting.wav").start();
In to a method an called this method in the program where i send commands to the modem.

These are my modem commands
send("ATZ");
    expect("OK");

//Enable voice mode
    send("AT+FCLASS=8");
    expect("OK");
//Dial the number
    send("ATDT"+number);
    expect("OK");
//Choose encoding method //This is the default
    send("AT+VSM=140,8000,0,0");
    expect("OK");
// start sending audio data 
    send("AT+VTX");
    expect("OK");
   
PlayAudio();
   

send("ATH");
The size of my audio file is 100 kb.I'm using a 56 K intel externel voice modem connected to a serial port.I wonder if it's because the buffer is too big. If anyone tried this and succeded please correct me.
Thankyou in advance
goodnews