当前位置: 技术问答>java相关
小程序中怎么播放声音?
来源: 互联网 发布时间:2015-08-27
本文导语: 下面是我看到别人用得方法,没有声音? 声音文件和程序在同一目录下 import java.applet.*; import java.awt.*; import java.awt.event.*; public class p275 extends Applet implements ActionListener {AudioClip clip; Button button_play,button_...
下面是我看到别人用得方法,没有声音?
声音文件和程序在同一目录下
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class p275 extends Applet implements ActionListener
{AudioClip clip;
Button button_play,button_loop,button_stop;
public void init()
{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 p275 extends Applet implements ActionListener
{AudioClip clip;
Button button_play,button_loop,button_stop;
public void init()
{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();}
}
}
|
.wav的应该也可以,我试过的
我试过的原程序如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class AudioPlay extends JApplet implements ItemListener,ActionListener
{
private AudioClip audio1,audio2,audio;
private JButton play,loop,stop;
private String item[]={"欢迎光临","嗨!!"};
private JComboBox comboBox=new JComboBox(item);
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER));
comboBox.addItemListener(this);
c.add(comboBox);
play=new JButton("播放");
play.addActionListener(this);
c.add(play);
loop=new JButton("循环");
loop.addActionListener(this);
c.add(loop);
stop=new JButton("停止");
stop.addActionListener(this);
c.add(stop);
audio1=getAudioClip(getDocumentBase(),"welcome.wav");
audio2=getAudioClip(getDocumentBase(),"hi.wav");
audio=audio1;
}
public void stop()
{
audio.stop();
}
public void itemStateChanged(ItemEvent e)
{
audio.stop();
audio=comboBox.getSelectedIndex()==0?audio1:audio2;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==play)
audio.play();
else if(e.getSource()==loop)
audio.loop();
else if(e.getSource()==stop)
audio.stop();
}
};
JAVA现在支持很多音乐格式啊,不过要播放MIDI还真不入播放WAV,AU那么方便
我试过的原程序如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class AudioPlay extends JApplet implements ItemListener,ActionListener
{
private AudioClip audio1,audio2,audio;
private JButton play,loop,stop;
private String item[]={"欢迎光临","嗨!!"};
private JComboBox comboBox=new JComboBox(item);
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER));
comboBox.addItemListener(this);
c.add(comboBox);
play=new JButton("播放");
play.addActionListener(this);
c.add(play);
loop=new JButton("循环");
loop.addActionListener(this);
c.add(loop);
stop=new JButton("停止");
stop.addActionListener(this);
c.add(stop);
audio1=getAudioClip(getDocumentBase(),"welcome.wav");
audio2=getAudioClip(getDocumentBase(),"hi.wav");
audio=audio1;
}
public void stop()
{
audio.stop();
}
public void itemStateChanged(ItemEvent e)
{
audio.stop();
audio=comboBox.getSelectedIndex()==0?audio1:audio2;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==play)
audio.play();
else if(e.getSource()==loop)
audio.loop();
else if(e.getSource()==stop)
audio.stop();
}
};
JAVA现在支持很多音乐格式啊,不过要播放MIDI还真不入播放WAV,AU那么方便
|
是的,我将格式修改了一下,确实是因为格式问题
我想说明的是,大家应该知道JAVA在WEB
我想说明的是,大家应该知道JAVA在WEB