qq494257084 / m3u8Download

java下载m3u8视频,解密并合并ts java download m3u8 video,decrypt & merge ts slice
https://github.com/qq494257084/m3u8Download
242 stars 108 forks source link

单例模式模式下,不同的下载链接只下载第一个链接 #12

Open lsldragon opened 2 years ago

lsldragon commented 2 years ago

参考如下代码: ` downloadVideoButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) {

            int index = list.getSelectedIndex();
            String name = nameList.get(index);

            String html = "https://" + nameListURL.get(index);
            System.out.println(html);

            String html1 = GetContent.getHtml(html);

            String videoUrl = GetContent.getVideoUrl(html1);
            System.out.println(videoUrl);

            File file = new File(name);
            if (!file.exists()) {
                file.mkdir();
            }
            String filePath = file.getAbsolutePath();

            M3u8DownloadFactory.M3u8Download m3u8Download = M3u8DownloadFactory.getInstance(videoUrl);
            m3u8Download.setDir(filePath);
            m3u8Download.setFileName(name);
            m3u8Download.setThreadCount(100);
            m3u8Download.setRetryCount(20);
            m3u8Download.setTimeoutMillisecond(10000L);
            m3u8Download.setLogLevel(Constant.DEBUG);
            m3u8Download.setInterval(500L);
            m3u8Download.addListener(new DownloadListener() {
                @Override
                public void start() {
                    System.out.println("开始下载!");
                    progressBar.setValue(0);
                }

                @Override
                public void process(String downloadUrl, int finished, int sum, float percent) {
                    System.out.println("下载网址:" + downloadUrl + "\t已下载" + finished + "个\t一共" + sum + "个\t已完成" + percent + "%");
                    progressBar.setValue((int) percent);
                }

                @Override
                public void speed(String speedPerSecond) {
                    System.out.println("下载速度:" + speedPerSecond);
                }

                @Override
                public void end() {
                    System.out.println("下载完毕");
                    progressBar.setString("Video download completed!");
                }
            });
            m3u8Download.start();
        }
    });`

改变 list 的值,每次点击 downloadVideoButton 应该产生不同的 videoUrl , 但是每次下载都是第一个 videoUrl (软件不关闭情况下)。

后来看到了 工厂类的 getInstance() 方法: public static M3u8Download getInstance(String downloadUrl) { if (m3u8Download == null) { synchronized (M3u8Download.class) { if (m3u8Download == null) m3u8Download = new M3u8Download(downloadUrl); } } return m3u8Download; }

当第二次点击 downloadVideoButton 时,m3u8Download 不为null 直接 return 了, 所以还是第一个的 videoUrl, 这就导致了每次下载只下载第一个链接。 建议不要把 downloadUrl 放在getInstance()参数里,增加 set get 方法。

qq494257084 commented 2 years ago

当初这么写是自己没事玩的。。。等我有时间重构下

---原始邮件--- 发件人: "Elliot @.> 发送时间: 2022年1月11日(周二) 中午12:32 收件人: @.>; 抄送: @.***>; 主题: [qq494257084/m3u8Download] 单例模式模式下,不同的下载链接只下载第一个链接 (Issue #12)

参考如下代码: downloadVideoButton.addActionListener(new ActionListener() { @OverRide public void actionPerformed(ActionEvent actionEvent) { int index = list.getSelectedIndex(); String name = nameList.get(index); String html = "https://" + nameListURL.get(index); System.out.println(html); String html1 = GetContent.getHtml(html); String videoUrl = GetContent.getVideoUrl(html1); System.out.println(videoUrl); File file = new File(name); if (!file.exists()) { file.mkdir(); } String filePath = file.getAbsolutePath(); M3u8DownloadFactory.M3u8Download m3u8Download = M3u8DownloadFactory.getInstance(videoUrl); m3u8Download.setDir(filePath); m3u8Download.setFileName(name); m3u8Download.setThreadCount(100); m3u8Download.setRetryCount(20); m3u8Download.setTimeoutMillisecond(10000L); m3u8Download.setLogLevel(Constant.DEBUG); m3u8Download.setInterval(500L); m3u8Download.addListener(new DownloadListener() { @Override public void start() { System.out.println("开始下载!"); progressBar.setValue(0); } @Override public void process(String downloadUrl, int finished, int sum, float percent) { System.out.println("下载网址:" + downloadUrl + "\t已下载" + finished + "个\t一共" + sum + "个\t已完成" + percent + "%"); progressBar.setValue((int) percent); } @Override public void speed(String speedPerSecond) { System.out.println("下载速度:" + speedPerSecond); } @Override public void end() { System.out.println("下载完毕"); progressBar.setString("Video download completed!"); } }); m3u8Download.start(); } });
改变 list 的值,每次点击 downloadVideoButton 应该产生不同的 videoUrl , 但是每次下载都是第一个 videoUrl (软件不关闭情况下)。

后来看到了 工厂类的 getInstance() 方法: public static M3u8Download getInstance(String downloadUrl) { if (m3u8Download == null) { synchronized (M3u8Download.class) { if (m3u8Download == null) m3u8Download = new M3u8Download(downloadUrl); } } return m3u8Download; }

当第二次点击 downloadVideoButton 时,m3u8Download 不为null 直接 return 了, 所以还是第一个的 videoUrl, 这就导致了每次下载只下载第一个链接。 建议不要把 downloadUrl 放在getInstance()参数里,增加 set get 方法。

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you are subscribed to this thread.Message ID: @.***>