String Audur = "结点URL";
try {
play(new URL(Audur));
} catch(Exception e){}
或
String Audur = "结点URL";
try {
play(new URL(Audur),声音文件名);
} catch(Exception e){}
后者使用的格式是:
String Audur = "结点URL";
AudioClip loopClip;
try {
loopClip = getAudioClip(new URL(Audur));
}
catch(Exception e){
System.out.println("Can′t open the URL ");
}
或
String Audur = "结点URL";
AudioClip loopClip;
try {
loopClip = getAudioClip(new URL(Audur) ,声音文件名);
}
catch(Exception e){
System.out.println("Can′t open the URL ");
}
URL Audur =null;
try {
Audur=new URL("结点URL ");
} catch(Exception e){
System.out.println("Can′t open the URL ");
}
play(Audur);
下面对前述四种从网络上获取并播放声音文件的格式各举一简单的例子,
以作编程时参考:
●程序4 格式一
import java.applet.*;
import java.awt.*;
import java.net.*;
public class sound1 extends Applet
{ AudioClip loopClip;
public void paint(Graphics g) {
String Audur = "http://www.shu.edu.cn/~xyx/java/Animator/audio/bark.au";
try {
play(new URL(Audur));
} catch(Exception e){}
}
}
●程序5 格式二
import java.applet.*;
import java.awt.*;
import java.net.*;
public class sound2 extends Applet
{ AudioClip loopClip;
public void paint(Graphics g) {
String Audur = "http://www.shu.edu.cn/~xyx/java/Animator/audio/";
try {
play(new URL(Audur),"bark.au");
} catch(Exception e){}
}
}
●程序6 格式三
import java.applet.*;
import java.awt.*;
import java.net.*;
public class sound extends Applet{
AudioClip loopClip;
public void init() {
String Audur = "http://www.shu.edu.cn/~xyx/java/Animator/audio/bark.au";
try {
loopClip = getAudioClip(new URL(Audur));
} catch(Exception e){}
}
public void paint(Graphics g){
loopClip.loop();
}
}
●程序7 格式四
import java.applet.*;
import java.awt.*;
import java.net.*;
public class sound0 extends Applet{
AudioClip loopClip;
URL auur;
public void init() {
try {
auur=new URL("http://www.shu.edu.cn/~xyx/java/Animator/audio/");
}
catch (MalformedURLException e) {
System.out.println("Can′t open the URL ");
}
loopClip = getAudioClip(auur,"bark.au");
}
public void paint(Graphics g){
loopClip.loop();
}
}