日期:2014-05-20 浏览次数:20869 次
//新建一个img文件夹,里面放上要播放的音乐。mid类型的吧。 import java.applet.*; import java.awt.event.*; import java.awt.*; public class PlayAudio extends Applet implements ActionListener{ AudioClip audio; Button btExit,btOpen,btPlay,btLoop,btStop; /** *<br>方法说明:初始化Applet *<br>输入参数: *<br>返回类型: */ public void init() { //加载声音文件 audio = getAudioClip(getDocumentBase(),"img/1.mid");//这里放上你加在新建文件夹里面的歌曲名称 //构造按钮 setLayout(new FlowLayout()); btPlay=new Button("Play"); btPlay.addActionListener(this); btLoop=new Button("Loop"); btLoop.addActionListener(this); btStop=new Button("Stop"); btStop.addActionListener(this); //给Play按钮添加一个监听事件 //将按钮添加到Applet中 add(btPlay); add(btLoop); add(btStop); } public void actionPerformed(ActionEvent e) { //如果点击的是Play按钮 if (e.getSource()==btPlay) { play(); } //如果点击的是loop按钮 if (e.getSource()==btLoop) { loop(); } //如果点击的是stop按钮 if (e.getSource()==btStop) { stop(); } } /** *<br>方法说明:播放声音 *<br>输入参数: *<br>返回类型: */ public void play(){ if (audio!=null) stop(); audio.play(); } /** *<br>方法说明:循环播放声音 *<br>输入参数: *<br>返回类型: */ public void loop(){ if (audio!=null) audio.loop(); } /** *<br>方法说明:停止播放声音 *<br>输入参数: *<br>返回类型: */ public void stop(){ if (audio!=null) audio.stop(); } }
------解决方案--------------------
是哎~~基础版..
像我这样的小鸟还没去过高级的版本呢..
------解决方案--------------------
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AudioApplet extends Applet implements ActionListener {
AudioClip ac;
Button btnStart=new Button("开始");
Button btnPause=new Button("暂停");
Button btnContine=new Button("继续");
public void init(){
ac=getAudioClip(getDocumentBase(), "1.au");
btnStart.addActionListener(this);
btnPause.addActionListener(this);
btnContine.addActionListener(this);
this.add(btnStart);
this.add(btnPause);
this.add(btnContine);
}
public void stop(){
if(ac!=null){
ac.stop();
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(ac==null){
System.out.println("AudioClip object is not created");
return;
}
Object cmd=e.getSource();
if(cmd==btnStart){
ac.play();
ac.loop();
}else if(cmd==btnPause){
ac.stop();
}else if(cmd==btnContine){
ac.play();
ac.loop();
}
}
}
------解决方案--------------------
路过 好久没用APPLE了
------解决方案--------------------
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; import java.applet.*; public class Sound { public static void main(String args[]) { new MusicWindow(); } } class MusicWindow extends JFrame implements ActionListener { JButton [] musicButton; File musicFile; URI uri; URL url; AudioClip clip; String[] musicName = {"sound/1.mid", "sound/2.mid", "sound/3.mid"}; MusicWindow() { musicButton = new JButton[2]; Box musicBox = Box.createHorizontalBox(); musicButton[0] = new JButton("play"); musicButton[0].addActionListener(this); musicBox.add(musicButton[0]); musicButton[1] = new JButton("stop"); musicButton[1].addActionListener(this); musicBox.add(musicButton[1]); setLayout(new FlowLayout()); add(musicBox); setBounds(120,125,250,150); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); if ( clip != null) clip.stop(); for ( int i = 0; i < musicButton.length; i++ ) { if ( button == musicButton[0] ) { for ( int j = 0; j < 3; j++ ) { musicFile = new File(musicName[j]); uri = musicFile.toURI(); try { url = uri.toURL(); } catch(Exception exp) { } clip = Applet.newAudioClip(url); clip.play(); } } else { clip.stop(); } } } }