0

I have embedded a youtube video as shown here. Now I want something to happen, when the video finishes.

I guess the hard way would be to download the video as an object etc.

Is there an easier way?

Any help would be appreciated.

Community
  • 1
  • 1
Selphiron
  • 897
  • 1
  • 12
  • 30
  • 3
    The key will be to fully learn the libraries that are used in the example shown in your link. You will be able to solve this if this library can notify you when the video is done. Since this solution will be library specific, I'm not sure anyone can help you unless they know that library, and since the number of folks in that pool are small, you will want to study the library in depth yourself to gain this understanding yourself. – Hovercraft Full Of Eels Dec 24 '15 at 21:10

1 Answers1

4

The JWebBrowser component from The DJ project as used in the very nice example by Jk1 supports a listener that you can add to it to receive browser events. You could use these events to search for specific events that indicate the video is finished. This depends on how YouTube works, which could be platform/browser dependent and will change over time. So no hard science here, but perhaps this approach is good enough for your situation.

Here is the modified code of the above mentioned example, which adds a listener for the browser events (implementing only two of the ten available WebBrowserListener methods) and some logging. I used DJNativeSwing-SWT-1-0-2 and swt-4.5-win32-win32-x86_64:

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);
    }
}

And here the output for a short test:

[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] 

On my machine, the statusChanged event where the status text of the webBrowser field is "Downloading picture res://ieframe.dll/background_gradient.jpg..." seems to indicate that the video is finished.

Community
  • 1
  • 1
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
  • yes that works but only if you use the standard youtube version. I would like to use https://www.youtube.com/v/RnqAXuLZlaE&autoplay=1 rather than https://youtu.be/RnqAXuLZlaE?t=3m7s Do you think there is another way? – Selphiron Dec 26 '15 at 17:06
  • Why do you prefer one type of URL over another (since it's easy to convert from one type to the other)? Perhaps https://youtube.com/v/RnqAXuLZlaE?autoplay=1 works better ('?' instead of '&'; see http://stackoverflow.com/a/3433300/1694043)? The shortened "youtu.be" type of URLs seem to autoplay by default for me. – Freek de Bruijn Dec 27 '15 at 16:51
  • Because it is fullscreen if you use the youtube.com/v/VIDEO_ID version. Anyway I found another solution for youtube. I look up the duration of the video with `https://www.googleapis.com/youtube/v3/videos?part=contentDetails&key=DEV_KEY&id=VIDEO_ID` and start a timer. – Selphiron Dec 27 '15 at 23:19
  • Nice solution! Using that official API will work fine, assuming that the network is stable and fast enough; otherwise buffering might delay the actual playback compared to the ideal play time. – Freek de Bruijn Dec 27 '15 at 23:26
  • thanks :) Thats why I add some extra seconds. – Selphiron Dec 28 '15 at 11:08
  • It's nice. But I want to hide youtube control bar. Can anyone help me how I hide youtube control bar in this program. – Syeful Islam Jul 14 '16 at 10:36
  • @SyefulIslam: if you add a "controls=0" parameter to the URL, the control bar should be gone. For example: `"https://youtube.com/v/RnqAXuLZlaE?controls=0"`. – Freek de Bruijn Jul 17 '16 at 16:44