How to create a mute option

感情迁移 提交于 2019-11-30 09:58:57

There is the Port which gives you access to the computer's mixer, but muting sound there would mute all sound of the computer. So that's probably not a good option.

Other than that, I believe that you require the class instance of the Clip or SourceDataLine that currently plays. Most probably, however, the applet uses Applet's AudioClip class for playback, which may or may not use a Clip/SourceDataLine internally...

Anyway, you can try the following approach, it should work on most Java Sound implementations:

  • from AudioSystem, get all mixers

    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    for (Mixer.Info info: infos) {
        Mixer mixer = AudioSystem.getMixer(info);
    }
    

    make sure to import javax.sound.sampled.*

  • from each Mixer, retrieve the playback lines with getSourceLines()
  • for each returned line, try to get the Mute control:

    BooleanControl bc = (BooleanControl) line.getControl(BooleanControl.Type.MUTE)
    if (bc != null) {
        bc.setValue(true); // true to mute the line, false to unmute
    }
    

Note that there is no guarantee that you will get lines, and no guarantee that the Java Sound implementation provides the MUTE control for a given line.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!