📄 mediaoverview.html
字号:
TerminalConnection tc = (TermConnEv)ev.getTerminalConnection(); MediaTerminalConnection mtc = (MediaTerminalConnection)tc; /* Route the media to/from the workstation's hardware. */ try { mtc.useDefaultSpeaker(); mtc.useDefaultMicrophone(); mtc.startPlaying(); mtc.startRecording(); } catch (Exception excp) { // Handle all Exceptions } } else if (ev instance MediaTermConnUnavailableEv) { /* Stop the playing and recording */ TerminalConnection tc = (TermConnEv)ev.getTerminalConnection(); MediaTerminalConnection mtc = (MediaTerminalConnection)tc; try { mtc.stopRecording(); mtc.stopPlaying(); } catch (Exception excp) { // Handle all Exceptions } } } }}public class DesktopCall { public static final void main(String args[]) { /* * Create a provider by first obtaining the default implementation of * JTAPI and then the default provider of that implementation. */ Provider myprovider = null; try { JtapiPeer peer = JtapiPeerFactory.getJtapiPeer(null); myprovider = peer.getProvider(null); } catch (Exception excp) { System.out.println("Can't get Provider: " + excp.toString()); System.exit(0); } /* * Create a new telephone call object and place an observer on it. */ Call mycall = null; try { mycall = myprovider.createCall(); mycall.addObserver(new DesktopCallObserver()); } catch (Exception excp) { // Handle all Exceptions } /* * Locate the Terminal and Address associated with our near end and place * a telephone call to a destination address. We are assuming that * Terminals are named after the primary telephone number on that Terminal. */ try { Address address = myprovider.getAddress("4761111"); Terminal terminal = address.getTerminal("4761111"); mycall.connect(terminal, address, "5551212"); } catch (Exception excp) { // Handle all Exceptions } }}</PRE><BR><BR> <H4>A Voice Answering Machine Application</H4><P>The following code example shows how an application developer would write avoice-answering machine using the Java Telephony API call control methods toanswer an incoming telephone call and the Voice API to play a prompt andrecord a message into another file.</P><PRE>import java.telephony.*;import java.telephony.events.*;import java.telephony.media.*;import java.telephony.media.events.*;class MachineCallObserver implements MediaCallObserver { /* * This CallObserver will be notified of Call events. We want to first * answer the incoming telephone call, and then perform the neccessary * media actions for the answering machine. */ public void callChangedEvent(CallEv evlist[]) { for (int i = 0; i < evlist.length; i++) { CallEv ev = evlist[i]; /* * When the TerminalConnection associated with the local Terminal is * create we want to set it up for source and destinations of the * voice media. */ if (ev instanceof TermConnCreatedEv) { TerminalConnection tc = ((TermConnEv)ev).getTerminalConnection(); MediaTerminalConnection mtc = (MediaTerminalConnection)tc; try { mtc.usePlayURL(new URL("file:/audio/prompt.au")); mtc.useRecordURL(new URL("file:/audio/message1.au")); } catch (Exception excp) { // Handle exceptions } } else if (ev instanceof TermConnRingingEv) { /* * We want to answer the telephone. */ TerminalConnection tc = ((TermConnEv)ev).getTerminalConnection(); try { tc.answer(); } catch (Exception excp) { // Handle Exceptions } } else if (ev instanceof MediaTermConnAvailableEv) { /* We want to start playing of the prompt. */ TerminalConnection tc = (TermConnEv)ev.getTerminalConnection(); MediaTerminalConnection mtc = (MediaTerminalConnection)tc; try { mtc.startPlaying(); } catch (Exception excp) { // Handle exceptions } } else if (ev instanceof MediaTermConnStateEv) { /* When the playing stops, start the recording of the message. */ TerminalConnection tc = (TermConnEv)ev.getTerminalConnection(); MediaTerminalConnection mtc = (MediaTerminalConnection)tc; try { /* Want to find out when playing is done */ int state = mtc.getMediaState(); if (state & MediaTerminalConnection.PLAYING == 0) { mtc.startRecording(); } } catch (Exception excp) { // Handl exceptions } } else if (ev instance MediaTermConnUnavailableEv) { /* When the channel on the telephone line becomes unavailable, then * stop all activity. */ TerminalConnection tc = (TermConnEv)ev.getTerminalConnection(); MediaTerminalConnection mtc = (MediaTerminalConnection)tc; try { mtc.stopRecording(); mtc.stopPlaying(); } catch (Exception excp) { // Handle exceptions } } } }}public class MachineCall { public static final void main(String args[]) { /* * Create a provider by first obtaining the default implementation of * JTAPI and then the default provider of that implementation. */ Provider myprovider = null; try { JtapiPeer peer = JtapiPeerFactory.getJtapiPeer(null); myprovider = peer.getProvider(null); } catch (Exception excp) { System.out.println("Can't get Provider: " + excp.toString()); System.exit(0); } /* * Locate the Terminal associated with our near end and place a call * observer on it. This will instruct the implementation to add a call * observer once a telephone call comes to the Terminal. We are assuming * Terminals are named after a primary telephone number on that Terminal. */ Terminal terminal; try { terminal = myprovider.getTerminal("4761111"); terminal.addCallObserver(new MachineCallObserver()); } catch (Exception excp) { // Handle all Exceptions } }}</PRE><BR><BR><H4>A DTMF-digit Collector</H4><P>The final code example shows how applications detect DTMF-tones from thetelephone line. This application answers an incoming telephone line andsimply prints out the detect tones to the screen.</P><PRE>import java.telephony.*;import java.telephony.events.*;import java.telephony.media.*;import java.telephony.media.events.*;class DtmfCallObserver implements MediaCallObserver { /* * This CallObserver will be notified of Call events. We want to first * answer the incoming telephone call, and then perform the neccessary * media actions to perform DTMF. */ public void callChangedEvent(CallEv evlist[]) { for (int i = 0; i < evlist.length; i++) { CallEv ev = evlist[i]; if (ev instanceof TermConnRingingEv) { /* Answer the phone. */ TerminalConnection tc = ((TermConnEv)ev).getTerminalConnection(); try { tc.answer(); } catch (Exception excp) { // Handle Exceptions } } else if (ev instanceof MediaTermConnAvailableEv) { /* We want to start DTMF-detection TerminalConnection tc = (TermConnEv)ev.getTerminalConnection(); MediaTerminalConnection mtc = (MediaTerminalConnection)tc; try { mtc.setDtmfDetection(true); } catch (Exception excp) { // Handle exceptions } } else if (ev instanceof MediaTermConnUnavailableEv) { /* Turn off DTMF-detection */ TerminalConnection tc = (TermConnEv)ev.getTerminalConnection(); MediaTerminalConnection mtc = (MediaTerminalConnection)tc; try { mtc.setDtmfDetection(false); } catch (Exception excp) { // Handle exceptions } } else if (ev instanceof MediaTermConnDtmfEv) { /* Print out the DTMF digits */ char digit = ((MediaTermConnDtmfEv)ev).getDigit(); System.out.println("detected DTMF: " + digit); } } }}public class DtmfCall { public static final void main(String args[]) { /* * Create a provider by first obtaining the default implementation of * JTAPI and then the default provider of that implementation. */ Provider myprovider = null; try { JtapiPeer peer = JtapiPeerFactory.getJtapiPeer(null); myprovider = peer.getProvider(null); } catch (Exception excp) { System.out.println("Can't get Provider: " + excp.toString()); System.exit(0); } /* * Locate the Terminal associated with our near end and place a call * observer on it. This will instruct the implementation to add a call * observer once a telephone call comes to the Terminal. We are assuming * Terminals are named after a primary telephone number on that terminal. */ Terminal terminal; try { terminal = myprovider.getTerminal("4761111"); terminal.addCallObserver(new MachineCallObserver()); } catch (Exception excp) { // Handle all Exceptions } }}</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -