Audible hints in Java

Source: Internet
Author: User
Tags abstract exit readline socket

Java as a network programming language, in the browser that can be animated demo, you can play sound. In the process of human-machine interaction, in order to enhance the effect or play a role in prompting, need to join the sound.

The Java package java.applet has a audioclip interface that encapsulates some common methods for sound fragments. Usage is as follows:

AudioClip audio;
audio=getAudioClip(getCodeBase(),"Hi.au");
audio.play();

The first line generates a variable for the interface AudioClip, and the second line uses this variable to get the sound file hi.au, which is in the same directory as the program itself, and the GetCodeBase () method is the URL address of the applet's class file. The third line is to play the sound file. Use this usage in the applet to make a sound in the browser. So is it possible to use this method to make sounds in application? No. Because the interface AudioClip is in the package Java.applet, this package applies only to applets. Is it possible to implement it in other ways? We can use 1 of the techniques to write a program that is the applet is also application to try. As a result, it still doesn't work. This is because the play () method can only be implemented in the applet and cannot be invoked for the Applicationplay () method.

So how do you implement audible cues in application?

Remember VB has beep statements to make the system sounder (systemspeaker) audible, then, Java should have a similar method.

There is a method beep () In the Java Java.awt.Toolkit class to implement this functionality. Class Toolkit is an abstract class, which is the parent of all the tools that implement AWT. Abstract classes in Java cannot be instantiated, but generally, abstract classes can generate variables and then use one of the methods in an abstract class to get a substitute for such a class. In toolkit, the Getdefaulttoolkit () method is used to achieve this. Now give an example:

This is a client/server application. When the server is running, if a client is connected to the server, the server automatically alerts the server that the user has a client to talk to.

The----procedures are as follows:

Server:

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:" +clie
                    Ntrequest);
                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++) {//audible prompts Toolkit.getdefaulttoolkit (). Beep (); } System.out.println ("\ nthe 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!"); }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.