📄 soundmanager.java
字号:
import java.applet.*;
public class SoundManager
{
private final String[] AUDIO_CLIP_VENDORS = {"Sun Microsystems Inc.", "Microsoft Corp.", "Apple Computer, Inc."};
private final String[] AUDIO_STREAM_VENDORS = {"Netscape Communications Corporation"};
private final int NO_SOUND = 0;
private final int AUDIO_CLIP = 1;
private final int AUDIO_STREAM = 2;
private AudioClip[] managedAudioClips;
private SunAudioManager sunAudio;
private boolean playSounds;
private int audioType;
public SoundManager(GameApplet applet, String[] files)
{
this(applet, files, true);
}
public SoundManager(GameApplet applet, String[] files, boolean soundEnabled)
{
this.sunAudio = applet.getSunAudioManager();
this.playSounds = soundEnabled;
this.audioType = getVendorType();
if (files == null)
{
files = new String[0];
}
try
{
switch(audioType)
{
case AUDIO_CLIP :
managedAudioClips = new AudioClip[files.length];
for (int i=0 ; i<files.length ; i++)
{
managedAudioClips[i] = applet.getGameMedia().loadAudioClip(files[i]);
}
break;
case AUDIO_STREAM :
if (sunAudio == null)
{
System.err.println("Unable to play sounds [sun.audio]... Sound is disabled");
playSounds = false;
}
else
{
sunAudio.loadAudio(files);
}
break;
case NO_SOUND :
System.err.println("Unknown JVM Vendor... Sound is disabled");
playSounds = false;
break;
}
}
catch(Exception e)
{
System.err.println("Error encountered while loading sound files... Sound is disabled");
playSounds = false;
}
}
private final int getVendorType()
{
String vendor = System.getProperty("java.vendor");
System.out.println("aaaaaaaaaaaa"+vendor);
for (int i=0 ; i<this.AUDIO_CLIP_VENDORS.length ; i++)
{
if (vendor.equals(AUDIO_CLIP_VENDORS[i]))
{
return this.AUDIO_CLIP;
}
}
for (int i=0 ; i<this.AUDIO_STREAM_VENDORS.length ; i++)
{
if (vendor.equals(AUDIO_STREAM_VENDORS[i]))
{
return this.AUDIO_STREAM;
}
}
return this.NO_SOUND;
}
public final void setSoundState(boolean soundEnabled)
{
playSounds = soundEnabled;
}
public final boolean getSoundState()
{
return playSounds;
}
public final void playSound(int sound)
{
if (!playSounds) return;
try
{
switch(audioType)
{
case AUDIO_CLIP :
if (sound>=0 && sound<managedAudioClips.length)
{
managedAudioClips[sound].play();
}
break;
case AUDIO_STREAM :
if (sunAudio == null)
{
System.err.println("Unable to play sounds [package sun.audio]... Sound is disabled");
playSounds = false;
}
else
{
sunAudio.playSound(sound);
}
break;
}
}
catch(Exception e)
{
System.err.println("Error encountered while playing a sound file... Sound is disabled");
playSounds = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -