Java swing:视频完成后执行动作

时间:2015-12-24 20:59:05

标签: java swing youtube djnativeswing jwebbrowser

我已嵌入了YouTube视频,如here所示。现在,当视频播放完毕时,我想要发生一些事情。

我想很难将视频作为对象下载等。

有更简单的方法吗?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:4)

The DJ project very nice example中使用的JWebBrowser WebBrowserListener组件支持一个可以添加到其中以接收浏览器事件的侦听器。您可以使用这些事件来搜索指示视频已完成的特定事件。这取决于YouTube的工作方式,这可能取决于平台/浏览器,并会随着时间的推移而发生变化。所以这里没有硬科学,但也许这种方法对你的情况来说已经足够了。

以下是上述示例的修改代码,它为浏览器事件添加了一个监听器(仅实现了十个可用的import java.awt.*; import java.text.*; import java.util.*; import javax.swing.*; import chrriis.dj.nativeswing.swtimpl.*; import chrriis.dj.nativeswing.swtimpl.components.*; public class YouTubeViewerV2 { private static final DateFormat DATE_FORMAT = new SimpleDateFormat("mm:ss.SSS"); private JWebBrowser webBrowser; public static void main(String[] arguments) { new YouTubeViewerV2().createAndShowGui(); } private void createAndShowGui() { NativeInterface.open(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("YouTube Viewer"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(getBrowserPanel(), BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationByPlatform(true); frame.setVisible(true); } }); NativeInterface.runEventPump(); // Don't forget to properly close native components. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { NativeInterface.close(); } })); } private JPanel getBrowserPanel() { JPanel webBrowserPanel = new JPanel(new BorderLayout()); webBrowser = new JWebBrowser(); webBrowser.setBarsVisible(false); //webBrowser.navigate("https://www.youtube.com/v/b-Cr0EWwaTk?fs=1"); webBrowser.navigate("https://youtu.be/RnqAXuLZlaE?t=3m7s"); webBrowser.addWebBrowserListener(getBrowserListener()); webBrowserPanel.add(webBrowser, BorderLayout.CENTER); return webBrowserPanel; } private WebBrowserListener getBrowserListener() { return new WebBrowserAdapter() { @Override public void loadingProgressChanged(WebBrowserEvent e) { log("Loading progress changed: " + webBrowser.getLoadingProgress()); log(""); } @Override public void statusChanged(WebBrowserEvent e) { log("Status changed - text: " + webBrowser.getStatusText()); log("Status changed - location: " + webBrowser.getResourceLocation()); log(""); } }; } private void log(String line) { System.out.println("[" + DATE_FORMAT.format(new Date()) + "] " + line); } } 方法中的两个)和一些日志记录。我使用了Jk1DJNativeSwing-SWT-1-0-2

[00:23.489] Status changed - text: Done
[00:23.500] Status changed - location: about:blank
[00:23.500] 
[00:23.585] Loading progress changed: 0
[00:23.585] 
[00:23.586] Status changed - text: Waiting for https://youtu.be/RnqAXuLZlaE?t=3m7s...
[00:23.587] Status changed - location: about:blank
[00:23.587] 
[00:23.691] Loading progress changed: 0
[00:23.692] 
[00:23.704] Status changed - text: Waiting for https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s...
[00:23.707] Status changed - text: 
[00:23.903] Status changed - text: Waiting for https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s...
[00:23.913] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:23.913] 
[00:23.913] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:23.913] 
[00:23.913] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:23.913] 
[00:23.997] Status changed - text: Done
[00:23.997] Loading progress changed: 100
[00:23.997] 
[00:24.039] Status changed - text: Waiting for https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s...
[00:24.042] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.042] 
[00:24.042] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.042] 
[00:24.088] Status changed - text: Done
[00:24.124] Status changed - text: Waiting for https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s...
[00:24.140] Status changed - text: Waiting for javascript:""...
[00:24.143] Status changed - text: Done
[00:24.155] Loading progress changed: 100
[00:24.155] 
[00:24.178] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.178] 
[00:24.178] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.178] 
[00:24.178] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.178] 
[00:24.178] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.178] 
[00:24.404] Status changed - text: Downloading picture data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAQAAAAnOwc2AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JQAAgIMAAPn/AACA6QAAdTAAAOpgAAA6mAAAF2+SX8VGAAAAAmJLR0QA/4ePzL8AAADdSURBVAjXAdIALf8A27rb69t02wQAANsF23jb7Nu1AAAB2/UACvr9wqMgcyRgAIQACfb7L44DbgH7QAYDFCr7QQ5RAQ77/aLixgABAACbpzFSDwYAAAAA8fml44/dAKAAAAAAAMDP2//b/9v/pOsArQBVAAACAADbcRsXAAAAAAAAJQ1/CwCrAAAD23YAgwAR9fnQ8BAFDwYoIDQhsbEB2/QAC/v9nOCj2XUJTT8EAvb7L1sCAM33/KLhlN3robNUya74/Nr0AF0BAAAAkQAjAKcAqQD8AAQAWgBXANbCiVTrFOn0bgAAAABJRU5ErkJgg
[00:24.405] Status changed - text: Done
[00:24.630] Status changed - text: Downloading picture https://i.ytimg.com/vi/6qpudAhYhpc/default.jpg...
[00:24.636] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.636] 
[00:24.636] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.636] 
[00:24.636] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.636] 
[00:24.686] Status changed - text: Waiting for https://googleads.g.doubleclick.net/pagead/html/r20151208/r20151221/zrt_lookup.html...
[00:24.736] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.736] 
[00:24.740] Status changed - text: Done
[00:24.783] Loading progress changed: 100
[00:24.783] 
[00:24.803] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.804] 
[00:24.805] Status changed - text: Downloading picture https://i1.ytimg.com/vi/6qpudAhYhpc/mqdefault.jpg...
[00:24.806] Status changed - text: Waiting for https://tpc.googlesyndication.com/safeframe/1-0-2/html/container.html...
[00:24.807] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.807] 
[00:24.807] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.807] 
[00:24.815] Status changed - text: Done
[00:24.826] Loading progress changed: 100
[00:24.826] 
[00:24.907] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.907] 
[00:24.912] Status changed - text: Done
[00:24.924] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:24.924] 
[00:24.936] Status changed - text: Downloading data https://static.doubleclick.net/instream/ad_status.js...
[00:24.936] Status changed - text: Downloading data https://googleads.g.doubleclick.net/pagead/lopri?ad_block=3&client=ca-pub-6219811747049371&output=js&adk=511001906&loeid=9407537%2C9416126%2C9417204%2C9420452%2C9422596%2C9422971%2C9423662%2C9424816%2C9425483%2C9426192%2C9426528&num_ads=1&channel=PyvWatchInRelated%2BPyvWatchNoAdX%2BPyvYTWatch%2BYtLoPri%2Bafv_user_id__5lECLhs_ceObgtoS6mWgw%2Bafv_user_sachinbpaul%2Bivp%2Bnon_lpw%2Bpw%2Byt_cid_9660960%2Byt_mpvid_YgeBBxZqHi7EIThs%2Byt_no_360%2Byt_no_ap%2Byt_no_cp%2Bytdevice_1%2Bytdevicever_1.2
[00:24.941] Status changed - text: Done
[00:25.153] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.154] 
[00:25.154] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.154] 
[00:25.154] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.154] 
[00:25.156] Status changed - text: Done
[00:25.173] Loading progress changed: 0
[00:25.173] 
[00:25.185] Status changed - text: Waiting for https://googleads.g.doubleclick.net/pagead/ads?ad_block=2&client=ca-pub-6219811747049371&format=300x250_as&output=html&h=250&adk=3611498851&loeid=9407537%2C9416126%2C9417204%2C9420452%2C9422596%2C9422971%2C9423662%2C9424816%2C9425483%2C9426192%2C9426528&channel=0854550288%2BVertical_533%2BVertical_694%2BVertical_980%2Bafv_user_id__5lECLhs_ceObgtoS6mWgw%2Bafv_user_sachinbpaul%2Bivp%2Byt_cid_9660960%2Byt_mpvid_YgeBBxZqHi7EIThs%2Byt_no_360%2Byt_no_ap%2Byt_no_cp%2Bytdevice_1%2Bytdevicever_1.2015121
[00:25.191] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.191] 
[00:25.191] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.191] 
[00:25.196] Status changed - text: Waiting for https://googleads.g.doubleclick.net/pagead/ads?ad_block=2&client=ca-pub-6219811747049371&format=300x250_as&output=html&h=250&adk=3611498851&loeid=9407537%2C9416126%2C9417204%2C9420452%2C9422596%2C9422971%2C9423662%2C9424816%2C9425483%2C9426192%2C9426528&channel=0854550288%2BVertical_533%2BVertical_694%2BVertical_980%2Bafv_user_id__5lECLhs_ceObgtoS6mWgw%2Bafv_user_sachinbpaul%2Bivp%2Byt_cid_9660960%2Byt_mpvid_YgeBBxZqHi7EIThs%2Byt_no_360%2Byt_no_ap%2Byt_no_cp%2Bytdevice_1%2Bytdevicever_1.2015121
[00:25.197] Status changed - text: Waiting for https://tpc.googlesyndication.com/sodar/cTrvNaRi.html...
[00:25.201] Status changed - text: Done
[00:25.201] Loading progress changed: 100
[00:25.201] 
[00:25.241] Status changed - text: Waiting for https://googleads.g.doubleclick.net/pagead/ads?ad_block=2&client=ca-pub-6219811747049371&format=300x250_as&output=html&h=250&adk=3611498851&loeid=9407537%2C9416126%2C9417204%2C9420452%2C9422596%2C9422971%2C9423662%2C9424816%2C9425483%2C9426192%2C9426528&channel=0854550288%2BVertical_533%2BVertical_694%2BVertical_980%2Bafv_user_id__5lECLhs_ceObgtoS6mWgw%2Bafv_user_sachinbpaul%2Bivp%2Byt_cid_9660960%2Byt_mpvid_YgeBBxZqHi7EIThs%2Byt_no_360%2Byt_no_ap%2Byt_no_cp%2Bytdevice_1%2Bytdevicever_1.2015121
[00:25.255] Status changed - text: Downloading picture https://www.google.com/ads/measurement/l?ebcid=ALh7CaT7vATfx0ewCzZAOYdhOGzDMmKTx4JUrbIb4NCxlqK3oXVqnic5t2naN4OPAGZrf-GflriAroSrJgEUrJY5OusSw6j7IA...
[00:25.256] Status changed - text: Done
[00:25.547] Status changed - text: Waiting for https://googleads.g.doubleclick.net/pagead/ads?ad_block=2&client=ca-pub-6219811747049371&format=300x250_as&output=html&h=250&adk=3611498851&loeid=9407537%2C9416126%2C9417204%2C9420452%2C9422596%2C9422971%2C9423662%2C9424816%2C9425483%2C9426192%2C9426528&channel=0854550288%2BVertical_533%2BVertical_694%2BVertical_980%2Bafv_user_id__5lECLhs_ceObgtoS6mWgw%2Bafv_user_sachinbpaul%2Bivp%2Byt_cid_9660960%2Byt_mpvid_YgeBBxZqHi7EIThs%2Byt_no_360%2Byt_no_ap%2Byt_no_cp%2Bytdevice_1%2Bytdevicever_1.2015121
[00:25.557] Status changed - text: 
[00:25.565] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.565] 
[00:25.565] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.565] 
[00:25.565] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.565] 
[00:25.565] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.565] 
[00:25.565] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.565] 
[00:25.565] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.565] 
[00:25.565] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.565] 
[00:25.565] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.565] 
[00:25.579] Status changed - text: Start downloading from site: res://ieframe.dll/navcancl.htm
[00:25.579] Status changed - text: Downloading from site: res://ieframe.dll/navcancl.htm
[00:25.591] Status changed - text: 
[00:25.598] Status changed - text: Waiting for res://ieframe.dll/navcancl.htm...
[00:25.620] Status changed - text: Downloading picture res://ieframe.dll/background_gradient.jpg...
[00:25.620] Status changed - text: Done
[00:25.626] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.626] 
[00:25.626] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.626] 
[00:25.626] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.626] 
[00:25.626] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.626] 
[00:25.626] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.626] 
[00:25.626] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:25.626] 
[00:27.663] Status changed - text: Done
[00:27.705] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:27.705] 
[00:30.895] Status changed - text: Downloading picture https://i1.ytimg.com/vi/6qpudAhYhpc/hqdefault.jpg...
[00:30.895] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:30.895] 
[00:30.912] Loading progress changed: 4
[00:30.912] 
[00:30.994] Status changed - text: Downloading picture https://s.ytimg.com/yts/imgbin/www-sharing-vflKETXDP.png...
[00:30.997] Status changed - location: https://www.youtube.com/watch?v=RnqAXuLZlaE&feature=youtu.be&t=3m7s
[00:30.997] 

这里是短测试的输出:

statusChanged

在我的计算机上webBrowser事件,其中struct SData { var OldValue: Float var Values: [Float] var Times: [UInt32] } var Symbols: [String:SData] 字段的状态文本为"正在下载图片res://ieframe.dll/background_gradient.jpg ... " 似乎表示视频已完成。