关于java applet加载声音的问题 我想用applet加载两个声音文件,并通过一个按钮来实现两个声音文件的交换播放,怎么才能实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个应该挺简单的,关键就是交替播放,设置一个标志变量flag,0播放music1,1播放music2,当一个文件播放完时,设定flag = (flag + 1) % 2; (这个随便想的),实现0、1转换。 设置标志位,并在播放音乐的while循环中检测。你的按钮事件侦听器可以改变这个标志。 bool=true; JButton b=new JButton();b.addActionlistener(new ActionListener(){if(bool==true){music1.play(); bool=false;}else music2.play();bool=true;}); import java.applet.Applet;import java.applet.AudioClip;import java.awt.Button;import java.awt.Container;import java.awt.Event;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.JComboBox;public class soundtest extends Applet implements ActionListener ,ItemListener{ private AudioClip sound,sound1,soundCurrent; private Button pl,lop,stops; private String paly; private JComboBox m_choose; int i=0; public void init() { try { sound=getAudioClip(getDocumentBase(),"0.wav"); sound1=getAudioClip(getDocumentBase(),"1.wav"); soundCurrent=sound; } catch(OutOfMemoryError e){ System.out.println("内存溢出"); e.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } String choices[]={"0","1"}; m_choose=new JComboBox(choices); m_choose.addItemListener(this); add(m_choose); pl=new Button("play"); pl.addActionListener(this); add(pl); lop=new Button("loop"); lop.addActionListener(this); add(lop); stops=new Button("stop"); stops.addActionListener(this); add(stops); } public void stop() { soundCurrent.stop(); }public void actionPerformed(ActionEvent e){ if(e.getSource()==pl) soundCurrent.play(); else if(e.getSource()==lop) soundCurrent.loop(); else if(e.getSource()==stops) soundCurrent.stop(); }public void itemStateChanged(ItemEvent e){ soundCurrent.stop(); soundCurrent=(m_choose.getSelectedIndex()==0?sound:sound1); } } 把音频文改小点,JVM的内存设大点试试 怎么在switch语句中判断用户是否仅仅输入回车啊 eclipse的使用 JAVA基础问题!急啊! 技术面试过后,HR多久会有通知呀? 求教各位详细的给小弟解惑 请问用JFileChoose可以写保存窗口么,我见到的都是打开文件窗口,多谢。 请教各位, 很简单的问题 !!!请问有关字符流的问题。 Java程序的返回值 如何实现类似于"网络蚂蚁"的下载(用java) java内部类作用域问题 在哪里能下载到JOGL?
这个应该挺简单的,关键就是交替播放,设置一个标志变量flag,0播放music1,1播放music2,当一个文件播放完时,设定flag = (flag + 1) % 2; (这个随便想的),实现0、1转换。
你的按钮事件侦听器可以改变这个标志。
JButton b=new JButton();
b.addActionlistener(new ActionListener()
{
if(bool==true){music1.play();
bool=false;
}else
music2.play();
bool=true;
});
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Container;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;import javax.swing.JComboBox;public class soundtest extends Applet implements ActionListener ,ItemListener
{
private AudioClip sound,sound1,soundCurrent;
private Button pl,lop,stops;
private String paly;
private JComboBox m_choose;
int i=0;
public void init()
{
try {
sound=getAudioClip(getDocumentBase(),"0.wav");
sound1=getAudioClip(getDocumentBase(),"1.wav");
soundCurrent=sound;
} catch(OutOfMemoryError e){
System.out.println("内存溢出");
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
String choices[]={"0","1"};
m_choose=new JComboBox(choices);
m_choose.addItemListener(this);
add(m_choose);
pl=new Button("play");
pl.addActionListener(this);
add(pl);
lop=new Button("loop");
lop.addActionListener(this);
add(lop);
stops=new Button("stop");
stops.addActionListener(this);
add(stops);
}
public void stop()
{
soundCurrent.stop();
}public void actionPerformed(ActionEvent e)
{
if(e.getSource()==pl)
soundCurrent.play();
else if(e.getSource()==lop)
soundCurrent.loop();
else if(e.getSource()==stops)
soundCurrent.stop();
}public void itemStateChanged(ItemEvent e)
{
soundCurrent.stop();
soundCurrent=(m_choose.getSelectedIndex()==0?sound:sound1);
}
}