这是我写的一个类,先看代码:
class Music extends Thread {
private String name = null ;
private AudioClip ac = null ;
private boolean isLoop = false ;
private int count = 0 ;

public Music(String name , boolean isLoop){
this.name = name ;
this.isLoop = isLoop ;

ac = Applet.newAudioClip(MusicPlayer.class.getResource(name)) ;
}

/**
 * 此方法为播放声音,如果第一次播放则激活
 * 否则播放数量增加1
 */
public synchronized void play(){
if(count==0){
count = 1 ;
this.notify() ;
}else{
count ++ ;
}
}

public void run(){
while(true){
synchronized (this) {
while(count==0){//如果播放的次数等于0,那么就等待唤醒自己
try {
this.wait() ;
} catch (Exception e) {
e.printStackTrace() ;
}
}

if(isLoop){
ac.play() ;
}else{
ac.play() ;
count -- ;
}

}

}

}
public void pause(){
                count -- ;
ac.stop() ;
}
}
我碰到的一个很头疼的问题是:ac.play()这个方法不是个阻塞方法,或者说,根本无法检测ac这个音频片段是否已经播放完毕,有没有达人给个解决方案?在线等一个小时,没有的话睡觉明天继续等。