Chinaunix首页 | 论坛 | 博客
  • 博客访问: 594573
  • 博文数量: 110
  • 博客积分: 8016
  • 博客等级: 中将
  • 技术积分: 1217
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-28 10:14
文章分类

全部博文(110)

文章存档

2008年(1)

2007年(13)

2006年(96)

我的朋友

分类: Java

2006-07-06 15:07:10

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AudioPlay extends JApplet {
  private AudioClip sound1, sound2, currentSound;
  private JButton playSound, loopSound, stopSound;
  private JComboBox chooseSound;
  public void init(){
    Container container = getContentPane();
    container.setLayout( new FlowLayout() );
    String choices[] = { "main", "spacemusic" };
    chooseSound = new JComboBox( choices );
    chooseSound.addItemListener( new ItemListener() {
      public void itemStateChanged( ItemEvent e )
      {
        currentSound.stop();
        currentSound =chooseSound.getSelectedIndex() == 0 ? sound1 : sound2;
      }
    }); // end addItemListener method call
    container.add( chooseSound );
    ButtonHandler handler = new ButtonHandler();
    playSound = new JButton( "Play" );
    playSound.addActionListener( handler );
    container.add( playSound );
    loopSound = new JButton( "Loop" );
    loopSound.addActionListener( handler );
    container.add( loopSound );
    stopSound = new JButton( "Stop" );
    stopSound.addActionListener( handler );
    container.add( stopSound );
    sound1 = getAudioClip( getDocumentBase(), "main.wav" );
    sound2 = getAudioClip( getDocumentBase(), "spacemusic.au" );
    currentSound = sound1;
  } // end method init
  public void stop(){
    currentSound.stop();
  }
  private class ButtonHandler implements ActionListener {
    public void actionPerformed( ActionEvent actionEvent )
    {
      if ( actionEvent.getSource() == playSound )
        currentSound.play();
      else if ( actionEvent.getSource() == loopSound )
        currentSound.loop();
      else if ( actionEvent.getSource() == stopSound )
        currentSound.stop();
    }
  }
}
阅读(1128) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~