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();
}
}
}
阅读(1157) | 评论(0) | 转发(0) |