importjava.applet.*;importjava.awt.*;importjava.awt.event.*;importjava.net.*;publicclassSoundextendsAppletimplementsActionListener{StringonceFile=file:/e:/myjava/eg0910/src/1.aif;StringloopFile=file:/e:/myjava/eg0910/src/1.mid;AudioCliponceClip;AudioCliploopClip;ButtonplayOnce;ButtonstopOnce;ButtonstartLoop;ButtonstopLoop;booleanlooping=false;booleanplaying=false;publicvoidinit(){try{onceClip=getAudioClip(newURL(onceFile));loopClip=getAudioClip(newURL(loopFile));}catch(MalformedURLExceptione){}playOnce=newButton(Playaif);stopOnce=newButton(Stopaif);stopOnce.setEnabled(false);playOnce.addActionListener(this);add(playOnce);stopOnce.addActionListener(this);add(stopOnce);startLoop=newButton(Loopmidi);stopLoop=newButton(StopLoop);stopLoop.setEnabled(false);startLoop.addActionListener(this);add(startLoop);stopLoop.addActionListener(this);add(stopLoop);}publicvoidstop(){if(playing){onceClip.stop();//暂停播放}if(looping){loopClip.stop();//暂停循环播放}}publicvoidstart(){if(playing){onceClip.play();//重新开始播放}if(looping){loopClip.loop();//重新开始循环播放}}publicvoidactionPerformed(ActionEventevent){Objectsource=event.getSource();//响应playButton事件if(source==playOnce){if(onceClip!=null){playing=true;//Threadt=newThread(newt1());//t.start();//播放音乐文件onceClip.loop();//play按钮变灰,stop按钮可用stopOnce.setEnabled(true);playOnce.setEnabled(false);showStatus(Playingsound+onceFile+.);}else{showStatus(Sound+onceFile+notloadedyet.);}return;}//响应stopButton事件if(source==stopOnce){if(playing){playing=false;//暂停播放onceClip.stop();//play按钮可用,stop按钮变灰playOnce.setEnabled(true);stopOnce.setEnabled(false);}showStatus(Stoppedplayingsound+onceFile+.);return;}//响应loopButton事件if(source==startLoop){if(loopClip!=null){looping=true;//开始声音的循环播放loopClip.loop();//loop按钮可用,stoploop按钮变灰stopLoop.setEnabled(true);startLoop.setEnabled(false);showStatus(Playingsound+loopFile+continuously.);}else{showStatus(Sound+loopFile+notloadedyet.);}return;}//响应stoploopButton事件if(source==stopLoop){if(looping){looping=false;//停止声音的循环播放loopClip.stop();//loop按钮变灰,stoploop按钮可用startLoop.setEnabled(true);stopLoop.setEnabled(false);}showStatus(Stoppedplayingsound+loopFile+.);return;}}}