如果您控制applet的部署(即托管applet的网页),您可以编写自己的Applet Launcher.启动器用作包装器,为实际的applet提供自定义环境.启动器实例化真正的applet并传递给applet环境的自定义版本(AppletStub,AppletContext.)自定义环境将AudioClip实现为“无所事事”界面.
要禁用音频,您可以覆盖AppletContext,如下所示:
class CustomAppletContext implements AppletContext
{
AppletContext realContext;
// most methods delegate to the real context, either directly, or with a little modification to hide the fact that we are using this launcher
public void setStatus(String status)
{
realContext.setStatus(status);
}
// override the getAudioClip to return a dummy clip
public AudioClip getAudioClip(URl url)
{
return new DummyAudioClip();
}
}
// An AudioClip implementation that does nothing
class DummyAudioClip implements AudioClip
{
public void loop() { }
public void play() { }
public void stop() { }
}
我们还覆盖了AppletStub,因为这是Applet从中获取AppletContext的地方
class CustomAppletStub implements AppletStub
{
AppletStub realStub;
public AppletContext getAppletContext()
{
return new CustomAppletContext(realStub.getAppletContext());
}
}
然后,你的发射器:
class AppletLauncher extends Applet
{
private Applet realApplet = new NoisyApplet();
// delegate most methods to the applet, but override the stub, to inject our
// AppletContext and AudioClip implementation
public void setAppletStub(AppletStub stub)
{
realApplet.setAppletStub(new CustomAppletStub(stub));
}
}
它看起来像很多代码,但它实际上只是几个类,并且主要用于注入新的DummyAudioClip实现.
HTH!