Java作为一种网络编程语言,在浏览器中即可以进行动画演示,也可以播放声音。在人机 的交互过程中,为了加强效果或起到提示的作用,需要加入声音。
Java的包java.applet中有AudioClip接口,此接口封装有关声音片断的一些常用方法。用 法如下:
AudioClip audio;audio=getAudioClip(getCodeBase(),"Hi.au");audio.play();
第一行生成接口AudioClip的一个变量,第二行利用此变量取得声音文件Hi.au,此文件与 程序本身在同一目录下,getCodeBase()方法是用来取得Applet的class文件的URL地址。第三 行是播放声音文件。在Applet中利用此用法可在浏览器中发出声音。那么,在Application中 是否也可以用此方法来发出声音呢?不可以。因为接口AudioClip是在包java.applet中,而 此包只适用于Applet。是否可以用其它方法来实现呢?我们可以利用1中的技巧来编写一个即 是Applet又是Application的程序试试。结果,还是不行。这是因为play()方法只能在Applet 中实现,对于Applicationplay()方法是不能够被调用的。
那么,如何在Application中实现发声提示呢?
记得VB中有beep语句来使系统发声器(SystemSpeaker)发声,那么,Java中也应有类似的 方法。
在Java的java.awt.Toolkit类中有方法beep()是来实现这一功能的。类Toolkit是抽象类 ,它是实现AWT的所有工具的父类。Java中的抽象类是不能够实例化的,但是一般地,抽象类 可以生成变量,然后利用抽象类中的某一方法来取得此类的替代品。在Toolkit中是利用 getDefaultToolkit()方法来实现的。现在给出一个实例:
这是一个客户机/服务器的Application。当服务器运行时如果有客户机与服务器相连,则 服务器会自动发声警报提示服务器端的用户有客户要与自己进行对话。
—-程序如下:
服务器:
import java.util.*;import java.io.*;import java.net.*;import java.awt.*;public class ServerT{ public static void main(String[] args){ Server server; String clientRequest; boolean quit=false; server=new Server(8001); while(!quit){ DataInputStream keyboard=new DataInputStream(System.in); try{ clientRequest=server.in.readLine(); if(clientRequest.trim().equals("CLOSE")){ System.out.println("Client says: "+clientRequest); System.exit(1); } System.out.println("Client says: "+clientRequest); server.out.println(keyboard.readLine()); }catch(IOException e){ System.out.println("IOException in server.in.readLine()"+e); System.exit(1); } } }}class Server{ private ServerSocket server; private Socket socket; public DataInputStream in; public PrintStream out; public Server(int port){ try{ server=new ServerSocket(port); System.out.println("/n*********************************************************"); System.out.println("/n @(#)Net Applecation Version 1.00 97/12/30 "); System.out.println(" Copyright (c) 1997 (Lui DongBing) All Rights Reserved."); System.out.println("/n*********************************************************"); System.out.println("/n Server is: /n "+server); socket=server.accept(); for(int i=0;i< 260;i++){ // 发 声 提 示 Toolkit.getDefaultToolkit().beep(); } System.out.println("/n Server is ready ! /n"); in=new DataInputStream(socket.getInputStream()); ut=new PrintStream(socket.getOutputStream()); out.println("We connect in "+new Date()); }catch(IOException e){ System.out.println("Server is failied !"); } }}
客 户 机 :
import java.util.*;import java.io.*;import java.net.*;public class ClientT{ public static void main(String[] args){ String welcome,response; Client client; client=new Client("202.120.80.20",8001); // #1 DataInputStream keyboard=new DataInputStream(System.in); boolean Bye=false; while(!Bye){ try{ welcome=client.in.readLine(); System.out.println(" Server says: "+welcome); client.out.println(keyboard.readLine()); }catch(IOException e){ System.out.println("/n The talk is CLOSED !"); System.exit(1); } } try{ Thread.sleep(200); }catch(Exception e){ System.out.println("It is a bug !"); } }}class Client{ public DataInputStream in; public PrintStream out; private Socket client; public Client(String host,int port){ try{ client=new Socket(host,port); System.out.println("/n *********************************************************"); System.out.println("/n @(#)Net Applecation Version 1.00 97/12/30 "); System.out.println(" Copyright (c) 1997 (Lui DongBing) All Rights Reserved."); System.out.println("/n *********************************************************"); System.out.println("/n Client socket:"+client); System.out.println("/n Client is ready ! /n"); ut=new PrintStream(client.getOutputStream()); in=new DataInputStream(client.getInputStream()); }catch(IOException e){ System.out.println("/n IOException !/n"+e); System.exit(1); } }}
程序中#1处的”202.120.80.20″读者可改为自己相应的地址。
只有一条路不能拒绝——那就是成长的路。