我想用applet加载两个声音文件,并通过一个按钮来实现两个声音文件的交换播放,怎么才能实现?

解决方案 »

  1.   


    这个应该挺简单的,关键就是交替播放,设置一个标志变量flag,0播放music1,1播放music2,当一个文件播放完时,设定flag = (flag + 1) % 2; (这个随便想的),实现0、1转换。
      

  2.   

    设置标志位,并在播放音乐的while循环中检测。
    你的按钮事件侦听器可以改变这个标志。
      

  3.   

      bool=true;
     JButton  b=new JButton();
    b.addActionlistener(new ActionListener()
    {
    if(bool==true){music1.play();
      bool=false;
    }else  
    music2.play();
    bool=true;
    });
      

  4.   

    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);

    }
      
      
    }
      

  5.   

    把音频文改小点,JVM的内存设大点试试