当前位置: 技术问答>java相关
急,怎么实现在web里面播放声音?(如果方法好再加分)
来源: 互联网 发布时间:2015-10-16
本文导语: 是不是一定要用APPLET,用APPLET是不是一定要安装JAVA VM | 给你个例子吧,还是好好看看书吧,不要急于求成 // import java.applet.*; import java.awt.*; import java.awt.event.*; public class Pf extends App...
是不是一定要用APPLET,用APPLET是不是一定要安装JAVA VM
|
给你个例子吧,还是好好看看书吧,不要急于求成
//
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Pf extends Applet implements ActionListener {
AudioClip clip;
Button button_play,button_loop,button_stop;
public void init()
{//getAudioClip(),getCodeBase()是Applet的方法
clip=getAudioClip(getCodeBase(),"sound01.wav");
button_play=new Button("开始");
button_loop=new Button("循环");
button_stop=new Button("停止");
button_play.addActionListener(this);
button_loop.addActionListener(this);
button_stop.addActionListener(this);
add(button_play);add(button_loop);add(button_stop);
}
public void stop(){
clip.stop();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button_play) clip.play();
else if(e.getSource()==button_loop) clip.loop();
if(e.getSource()==button_stop) clip.stop();
}
}
//
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Pf extends Applet implements ActionListener {
AudioClip clip;
Button button_play,button_loop,button_stop;
public void init()
{//getAudioClip(),getCodeBase()是Applet的方法
clip=getAudioClip(getCodeBase(),"sound01.wav");
button_play=new Button("开始");
button_loop=new Button("循环");
button_stop=new Button("停止");
button_play.addActionListener(this);
button_loop.addActionListener(this);
button_stop.addActionListener(this);
add(button_play);add(button_loop);add(button_stop);
}
public void stop(){
clip.stop();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button_play) clip.play();
else if(e.getSource()==button_loop) clip.loop();
if(e.getSource()==button_stop) clip.stop();
}
}
|
Play an audio file from an application
You need to use the undocumented sun.audio package. import sun.audio.*;
...
...
AudioPlayer p=AudioPlayer.player;
try{
AudioStream as =
new AudioStream(new FileInputStream("aSound.au"));
p.start(as);
}
catch(IOException err){
e.printStackTrace();
}
To play a sound from a JAR file (the .AU file in the JAR must be accessible via the CLASSPATH of course!) : import java.io.*;
import java.net.*;
import sun.audio.*;
public class AppAudio {
public static void main(String args[]) throws Throwable {
InputStream in = AppAudio.class.getResourceAsStream(args[0]);
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
Thread.sleep(5000);
}
}
NOTE: with Applet, the getAudioClip method() from the Applet package is used to play sound file. Starting with JDK1.2, getAudioClip() is now a static method. So it may be possible to use it without an Applet with java.applet.Applet.getAudioClip(URLofMySound);
You need to use the undocumented sun.audio package. import sun.audio.*;
...
...
AudioPlayer p=AudioPlayer.player;
try{
AudioStream as =
new AudioStream(new FileInputStream("aSound.au"));
p.start(as);
}
catch(IOException err){
e.printStackTrace();
}
To play a sound from a JAR file (the .AU file in the JAR must be accessible via the CLASSPATH of course!) : import java.io.*;
import java.net.*;
import sun.audio.*;
public class AppAudio {
public static void main(String args[]) throws Throwable {
InputStream in = AppAudio.class.getResourceAsStream(args[0]);
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
Thread.sleep(5000);
}
}
NOTE: with Applet, the getAudioClip method() from the Applet package is used to play sound file. Starting with JDK1.2, getAudioClip() is now a static method. So it may be possible to use it without an Applet with java.applet.Applet.getAudioClip(URLofMySound);
|
是不是一定要用applet我不太清楚,
用applet的不一定要安装jvm,只要浏览器支持applet就可以了
用applet的不一定要安装jvm,只要浏览器支持applet就可以了
|
格式可以使mid ra ram wav au 等 n=-1为不重复