Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)
Since 6.3 version JxBrowser provides API that allows muting/unmuting audio on the loaded web page and checking whether audio is muted or not.(从6.3版开始,JxBrowser提供了允许对加载的网页上的音频进行静音/取消静音以及检查是否静音的API。)
To mute audio use the following code:(要使音频静音,请使用以下代码:)
browser.setAudioMuted(true);
To check whether audio is muted use the following code:(要检查音频是否被静音,请使用以下代码:)
boolean audioMuted = browser.isAudioMuted();
Entire example that demonstrates how how to mute audio sound on the opened web page and check whether audio is muted or not is the following:(整个示例演示如何在打开的网页上使音频声音静音以及如何检查音频是否静音:)
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* This sample demonstrates how to mute audio sound on the opened web page
* and check whether audio is muted or not.
*/
public class MuteAudioSample {
public static void main(String[] args) {
final Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
final JButton muteAudioButton = new JButton();
muteAudioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
browser.setAudioMuted(!browser.isAudioMuted());
updateButtonText(muteAudioButton, browser);
}
});
updateButtonText(muteAudioButton, browser);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(muteAudioButton, BorderLayout.NORTH);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.loadURL("https://www.youtube.com/");
}
private static void updateButtonText(final JButton button, final Browser browser) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
button.setText(browser.isAudioMuted() ? "Unmute Audio" : "Mute Audio");
}
});
}
}