Tải bản đầy đủ (.pdf) (30 trang)

Code Examplets phần 7 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.93 MB, 30 trang )



java.rmi
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Starting Up the RMI Registry
Starting up the RMI registry allows you to create and export
remote objects.
> rmiregistry
Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 24-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:04 AM]



java.rmi
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Defining and Exporting a Remote Object
1. Define the remote interface.
import java.rmi.*;

public interface RObject
extends Remote {
void aMethod() throws RemoteException;
}
2. Define the remote object implementation.
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class RObjectImpl
extends UnicastRemoteObject
implements RObject {
public RObjectImpl()
throws RemoteException {
super();
}
// All remote methods must
//throw RemoteException
public void aMethod()
throws RemoteException {
}
}
3. Compile the remote object implementation.

> javac RObject.java RObjectImpl.java
4. Generate the skeletons and stubs.
> rmic RObjectImpl
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:05 AM]
5. Create an instance of the remote object and bind it to the RMI
Registry.
try {
RObject robj =
new RObjectImpl();
Naming.rebind(
"//localhost/RObjectServer", robj);
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (RemoteException e) {
}
Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 31-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's

AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
(2 of 2) [8/1/2000 7:50:05 AM]


java.rmi
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Looking Up a Remote Object and Invoking a Method
try {
// Look up remote object
RObject robj = (RObject) Naming.lookup(
"//localhost/RObjectServer");

// Invoke method on remote object
robj.aMethod();
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (NotBoundException e) {
} catch (RemoteException e) {
}
Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:05 AM]


java.rmi
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Passing Parameters to a Remote Method
Arguments to remote methods must be primitive, serializable, or
Remote. This example demonstrates the declaration and use of all
three parameter types.
1. Define the remote interface.
import java.rmi.*;

public interface RObject extends Remote {
// This parameter is primitive.
void primitiveArg(int num)
throws RemoteException;


// This parameter implements Serializable.
void byValueArg(Integer num)
throws RemoteException;

// This parameter implements Remote.
void byRefArg(ArgObject arg)
throws RemoteException;
}

public interface ArgObject extends Remote {
int aMethod()
throws RemoteException;
}
2. Define the remote object implementation.
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class RObjectImpl
extends UnicastRemoteObject
implements RObject {
public RObjectImpl()
Code Samples from the Java Developers Almanac 2000
(1 of 3) [8/1/2000 7:50:06 AM]
throws RemoteException {
super();
}
public void primitiveArg(int num)
throws RemoteException {
}

public void byValueArg(Integer num)
throws RemoteException {
}
public void byRefArg(ArgObject arg)
throws RemoteException {
}
}
3. Compile the remote object implementation.
> javac RObject.java RObjectImpl.java
4. Generate the skeletons and stubs.
> rmic RObjectImpl
5. Create an instance of the remote object and bind it to the RMI
Registry.
try {
RObject robj = new RObjectImpl();
Naming.rebind("//localhost/RObjectServer", robj);
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (RemoteException e) {
}
6. Look up the remote object and pass the parameters.
try {
// Look up the remote object
RObject robj =
(RObject) Naming.lookup(
"//localhost/RObjectServer");

// Pass a primitive value as argument
robj.primitiveArg(1998);


// Pass a serializable object as argument
robj.byValueArg(
new Integer(9));

// Pass a Remote object as argument
robj.byRefArg(
new ArgObjectImpl());
Code Samples from the Java Developers Almanac 2000
(2 of 3) [8/1/2000 7:50:06 AM]
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (NotBoundException e) {
} catch (RemoteException e) {
}
Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 31-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.

All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
(3 of 3) [8/1/2000 7:50:06 AM]


java.rmi
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Returning Values from a Remote Method
Return values from remote methods must be primitive, serializable,
or Remote. This example demonstrates the declaration and use of
all three return types.
1. Define the remote interface.
import java.rmi.*;

public interface RObject extends Remote {
// This return value is primitive.
int primitiveRet() throws RemoteException;

// This return value implements Serializable.
Integer byValueRet() throws RemoteException;

// This return value implements Remote.
ArgObject byRefRet() throws RemoteException;
}

public interface ArgObject extends Remote {
int aMethod() throws RemoteException;
}

2. Define the remote object implementation.
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class RObjectImpl extends UnicastRemoteObject
implements RObject {
public RObjectImpl() throws RemoteException {
super();
}
public int primitiveRet() throws RemoteException {
return 3000;
}
Code Samples from the Java Developers Almanac 2000
(1 of 3) [8/1/2000 7:50:07 AM]
public Integer byValueRet() throws RemoteException {
return new Integer(2000);
}
public ArgObject byRefRet() throws RemoteException {
return new ArgObjectImpl();
}
}
3. Compile the remote object implementation.
> javac RObject.java RObjectImpl.java
4. Generate the skeletons and stubs.
> rmic RObjectImpl
5. Create an instance of the remote object and bind it to the RMI
Registry.
try {
RObject robj = new RObjectImpl();
Naming.rebind("//localhost/RObjectServer", robj);

} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (RemoteException e) {
}
6. Look up the remote object, invoke the methods, and receive the
return values.
try {
// Look up the remote object
RObject robj = (RObject) Naming.lookup(
"//localhost/RObjectServer");

// Receive the primitive value as return value
int r1 = robj.primitiveRet();

// Receive the serializable object as return value
Integer r2 = robj.byValueRet();

// Receive the Remote Object as return value
ArgObject aobj = robj.byRefRet();
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (NotBoundException e) {
} catch (RemoteException e) {
}
Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
Code Samples from the Java Developers Almanac 2000

(2 of 3) [8/1/2000 7:50:07 AM]
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
(3 of 3) [8/1/2000 7:50:07 AM]


java.rmi
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Throwing an Exception from a Remote Method
1. Define the remote interface.
import java.rmi.*;

public interface RObject extends Remote {
void aMethod() throws RemoteException;
}
2. Define the remote object implementation.
import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

public class RObjectImpl extends UnicastRemoteObject
implements RObject {
public RObjectImpl() throws RemoteException {
super();
}
public void aMethod() throws RemoteException {
// The actual exception must be wrapped in
// a RemoteException
throw new RemoteException(
"message", new FileNotFoundException(
"message"));
}
}
3. Compile the remote object implementation.
> javac RObject.java RObjectImpl.java
4. Generate the skeletons and stubs.
> rmic RObjectImpl
5. Create an instance of the remote object and bind it to the RMI
Registry.
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:07 AM]
try {
RObject robj = new RObjectImpl();
Naming.rebind("//localhost/RObjectServer", robj);
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (RemoteException e) {
}

6. Look up the remote object, invoke the method, and catch the
exception.
try {
// Look up the remote object.
RObject robj = (RObject) Naming.lookup(
"//localhost/RObjectServer");

// Invoke the method.
robj.aMethod();
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (NotBoundException e) {
} catch (RemoteException e) {
// Get the actual exception that was thrown.
Throwable realException = e.detail;
}
Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's

AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
(2 of 2) [8/1/2000 7:50:07 AM]


javax.sound.sampled
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Loading and Playing Sampled Audio
Supported audio file formats: aif, au, and wav.
try {
// From file
AudioInputStream stream =
AudioSystem.getAudioInputStream(
new File("audiofile"));

// From URL
stream = AudioSystem.getAudioInputStream(
new URL("http://hostname/audiofile"));

// At present, ALAW and ULAW encodings must be
//converted
// to PCM_SIGNED before it can be played.
AudioFormat format = stream.getFormat();
if (format.getEncoding() !=
AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(

AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(
format, stream);
}
DataLine.Info info = new DataLine.Info(
Clip.class, stream.getFormat(),

//The next two lines should be in one line.
((int)stream.getFrameLength(
)*format.getFrameSize()));
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:08 AM]

Clip clip = (Clip) AudioSystem.getLine(info);

// This method does not return until the audio
//file is completely loaded.
clip.open(stream);

// Start playing.
clip.start();
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (LineUnavailableException e) {

} catch (UnsupportedAudioFileException e) {
}

Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
(2 of 2) [8/1/2000 7:50:08 AM]


javax.sound.sampled
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Determining the File Format of a Sampled Audio File
try {

// From file
AudioFileFormat fformat =
AudioSystem.getAudioFileFormat(
new File("audiofile"));

// From URL
fformat = AudioSystem.getAudioFileFormat(
new URL("http://hostname/audiofile"));

if (fformat.getType() ==
AudioFileFormat.Type.AIFC) {
} else if (fformat.getType() ==
AudioFileFormat.Type.AIFF) {
} else if (fformat.getType() ==
AudioFileFormat.Type.AU) {
} else if (fformat.getType() ==
AudioFileFormat.Type.WAVE) {
}

} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (UnsupportedAudioFileException e) {
// File format is not supported.
}
Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:08 AM]


javax.sound.sampled
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Determining the Encoding of a Sampled Audio File

try {
// From file
// The next three lines should be in one line
AudioInputStream stream =
AudioSystem.getAudioInputStream(
new File("audiofile"));

// From URL
stream = AudioSystem.getAudioInputStream(
new URL("http://hostname/audiofile"));

AudioFormat format = stream.getFormat();
if (format.getEncoding() ==
AudioFormat.Encoding.ULAW) {
} else if (format.getEncoding() ==
AudioFormat.Encoding.ULAW) {
}
} catch (MalformedURLException e) {

} catch (IOException e) {
} catch (UnsupportedAudioFileException e) {
// Audio format is not supported.
}

Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:09 AM]


javax.sound.sampled
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Determining the Duration of a Sampled Audio File
To create a DataLine object, see ``Loading and Playing Sampled
Audio'' and ``Playing Streaming Sampled Audio''.

double durationInSecs = dataline.getBufferSize() /
//The next two lines should be in one line
(dataline.getFormat().getFrameSize(
) * dataline.getFormat().getFrameRate());


Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:09 AM]


javax.sound.sampled
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Playing Streaming Sampled Audio

try {
// From file

AudioInputStream stream =
AudioSystem.getAudioInputStream(
new File("audiofile"));

// From URL
stream = AudioSystem.getAudioInputStream(
new URL("http://hostname/audiofile"));

// At present, ALAW and ULAW encodings must be
// converted
// to PCM_SIGNED before it can be played.
AudioFormat format = stream.getFormat();
if (format.getEncoding() !=
AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(
format, stream);
}
SourceDataLine.Info info = new DataLine.Info(
//The next two lines should be in one line
SourceDataLine.class, stream.getFormat(),
//The next two lines should be in one line
((int)stream.getFrameLength(

)*format.getFrameSize()));

Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:10 AM]
//The next two lines should be in one line
SourceDataLine line = (SourceDataLine)
AudioSystem.getLine(info);
line.open(stream.getFormat());
line.start();

int numRead = 0;
byte[] buf = new byte[line.getBufferSize()];

//The next two lines should be in one line
while ((numRead = stream.read(buf,
0, buf.length)) >= 0) {
int offset = 0;
while (offset < numRead) {
offset += line.write(buf, offset,
numRead-offset);
}
}
line.drain();
line.stop();
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (LineUnavailableException e) {
} catch (UnsupportedAudioFileException e) {
}


Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
(2 of 2) [8/1/2000 7:50:10 AM]


javax.sound.sampled
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Continuously Playing a Sampled Audio File
Only DataLine objects that are of type Clip can be repeated. See
the ``Loading and Playing Sampled Audio'' examplet to create a
clip.
// Loop forever.

clip.loop(Clip.LOOP_CONTINUOUSLY);

// Loop for a certain number of times.
clip.loop(3);

// Start playing.
clip.start();
Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:10 AM]


javax.sound.sampled
Code Samples Index

These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Determining the Position of a Sampled Audio Player
To create a DataLine object, see ``Loading and Playing Sampled
Audio'' and ``Playing Streaming Sampled Audio''.

double timeInSeconds =
dataline.getMicrosecondPosition()/1000000.0d;

Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's
AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:11 AM]



javax.sound.sampled
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Determining When a Sampled Audio Player has
Finished Playing
To create a DataLine object, see ``Loading and Playing Sampled
Audio'' and ``Playing Streaming Audio''.

dataline.addLineListener(new LineListener() {
public void update(LineEvent evt) {
if (evt.getType() == LineEvent.Type.STOP) {
}
}
});

Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's

AT&T Direct Access Number first.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:11 AM]


javax.sound.sampled
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Controlling the Volume of Playing Sampled Audio
To create a DataLine object, see ``Loading and Playing Sampled
Audio'' and ``Playing Streaming Audio''.

// Set Volume
FloatControl gainControl =
(FloatControl)dataline.getControl(
FloatControl.Type.MASTER_GAIN);
double gain = .5d; // number between 0 and 1.
float dB = (float)(Math.log(gain)/Math.log(10.0)*20.0);
gainControl.setValue(dB);

// Mute On
//The next three lines should all be in one line
BooleanControl muteControl =
(BooleanControl)dataline.getControl(
BooleanControl.Type.MUTE);
muteControl.setValue(true);


// Mute Off
muteControl.setValue(false);

Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:12 AM]


javax.sound.midi
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Loading and Playing Midi Audio
Supported audio file formats: mid
try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();

// From file
Sequence sequence = MidiSystem.getSequence(
new File("midifile"));


// From URL
sequence = MidiSystem.getSequence(new URL(
"http://hostname/midifile"));

sequencer.setSequence(sequence);

// Start playing.
sequencer.start();
}
catch (MalformedURLException e) {
}
catch (IOException e) {
}
catch (MidiUnavailableException e) {
}
catch (InvalidMidiDataException e) {
}
Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:12 AM]


javax.sound.midi
Code Samples Index

These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Determining the File Format of a Midi Audio File
try {
// From file
MidiFileFormat fformat =
MidiSystem.getMidiFileFormat(new File(
"midifile"));

// From URL
fformat = MidiSystem.getMidiFileFormat(new URL(
"http://hostname/midifile"));

switch (fformat.getType()) {
case 0:
// mid
break;
case 1:
// rmf
break;
}

}
catch (MalformedURLException e) {
}
catch (IOException e) {
}
catch (InvalidMidiDataException e) {
// File format is not supported.
}

Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon
[ This page was updated: 19-Jul-2000 ]
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:13 AM]

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×