中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 多媒体
Java中的发声提示
作者:lsf82 时间:2001-11-16 11:06 出处:互联网 责编:chinaitpower
              摘要:Java中的发声提示

感谢作者: 大连博涵咨询服务有限公司 吕东兵

----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());
            out=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");
            out=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"读者可改为自己相应的地址。

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有