Java InstantCode. Developing Applications using Java Speech API

The AutoAnswerObserver.java file checks the status of a call. The AutoAnswerObserver class auto-replies to the caller with the predefined welcome message. Listing 8-2 shows the contents of the AutoAnswerObserver.java file:

Listing 8-2: The AutoAnswerObserver.java File

/*Import required JTAPI classes*/ import javax.telephony.*; import javax.telephony.events.*; import javax.telephony.media.*; import javax.telephony.media.events.*; import java.net.*; /* This class provides the media call observer for the Auto Reply Application. Class:AutoAnswerObserver-Class to detect the call made by caller Methods: setJtapiVariables():This method is used to initialize the terminal connection objects. */ public class AutoAnswerObserver implements MediaCallObserver, Runnable { boolean isAnswering = false; AutoAnswerGUI replyApp = null; Call phoneCall = null; private Connection connections[] = null; private TerminalConnection tconnection = null; private TerminalConnection desttconnection = null; Thread t=null; AnswerText st=null; /* Public constructor takes the object of AutoAnswerGUI class as parameter. AutoAnswerObserver(): Parameters: replyApp:object of AutoAnswerGUI Return Type:NA */ public AutoAnswerObserver(AutoAnswerGUI replyApp) { this.replyApp = replyApp; } /* This method is used to initialize the terminal connection objects. Parameters:Object of Call class Return Value:void */ private void setJtapiVariables(Call c) { phoneCall = c; try { connections = c.getConnections(); TerminalConnection[] tc; phoneCall.addObserver(this); tc = connections[0].getTerminalConnections(); tconnection = tc[0]; Terminal terminal = tconnection.getTerminal(); terminal.addCallObserver(this); /* Creating the remote terminal connection. The remote connection is the origination connection, which is always the first connection in the connections array. */ tc = connections[1].getTerminalConnections(); desttconnection = tc[0]; } catch(Exception e) { System.out.println("Exception occurred in setJtapiVariables(): " + e.toString()); } } /* The callChangedEvent() method is called every time an event associated with the phone call is raised. Parameters:Array of CallEv class eventList Return Type:void */ public void callChangedEvent(CallEv[] eventList) { if(phoneCall == null) { setJtapiVariables(eventList[0].getCall()); } for (int i = 0; i <eventList.length; i++) { CallEv ev = eventList[i]; TerminalConnection terminalConnection = ((TermConnEv)ev).getTerminalConnection(); MediaTerminalConnection mtc = (MediaTerminalConnection)terminalConnection; switch(ev.getID()) { case TermConnCreatedEv.ID: try { /* Specifying the audio file that is to be played when terminal connection is created. */ System.out.println("Connection Created."); mtc.useDefaultSpeaker(); mtc.useDefaultMicrophone(); mtc.useP layURL(new URL("file:helloworld.wav")); isAnswering = false; } catch (Exception excp) { System.out.println("Exception TermConnCreatedEv:" + excp.toString()); } break; case TermConnRingingEv.ID: try { System.out.println("Ringing"); if(isAnswering == false) { /*Answering the phone call.*/ isAnswering = true; terminalConnection.answer(); t=new Thread(this,"Node action"); t.start(); } } catch (Exception excp) { System.out.println("Exception occurred while answering the phone call:" + excp.toString()); } break; case MediaTermConnAvailableEv.ID: try { System.out.println("Media Term Connection Available event."); mtc.startPlaying(); } catch (Exception excp) { System.out.println("Exception occurred in startPlaying():" + excp.toString()); } break; case MediaTermConnStateEv.ID: try { /*Check if playing is done.*/ int state = mtc.getMediaState(); System.out.println("Playing"+state); state = state &MediaTerminalConnection.PLAYING; System.out.println("Playing"+state); if(state == 0) { /* Stop playing and closing the phone connection. */ mtc.stopPlaying(); hangupConnection(); } } catch (Exception excp) { System.out.println("Exception occurred in stopPlaying():" + excp.toString()); } break; case MediaTermConnUnavailableEv.ID: try { /* Specifying that the Media Terminal Connection is not available. */ System.out.println("Media terminal connection is not available."); mtc.stopPlaying(); } catch(Exception e) { System.out.println("Exception occurred in MediaTermConnUnavailableEv.ID:" + e.toString()); } /* Closing the telephone connection. */ hangupConnection(); break; default: break; } } } /* Run method to speak the answer Parameters:NA Return Value:Void */ public void run() { /* Initialize a new object of AnswerText class. */ st=new AnswerText("Thank You for calling me i would be glad to say hello", "kevin16"); /* Invoke the answerSelText() method of AnswerText class. */ st.answerSelText(this); } }

 

Download this listing .

In the above code, the constructor of the AutoAnswerObserver.java class takes an object of the AutoAnswerGUI.java class as an input parameter to invoke the methods of the AutoAnswerGUI.java class.

The methods defined in Listing 8-2 are:

Категории