From b0182ed60486083de9fe7d2befee588f723ae736 Mon Sep 17 00:00:00 2001 From: Adam Howard Date: Sat, 14 Nov 2015 11:47:21 +0000 Subject: [PATCH] halfway through implementing timestamps: * still need to use the start position value stored in the PlayVideoActivity's Intent bundle, to set the VideoView using seekTo() * need to test timestamp extraction regex, and possibly move it somewhere else *need to find a better way to get the startPosition value to ActionBarHandler, which I thought used VideoInfo objects, but apparently doesn't * currently there is a small setStartPosition() method --- .../org/schabi/newpipe/ActionBarHandler.java | 9 ++++++++- .../org/schabi/newpipe/PlayVideoActivity.java | 1 + .../java/org/schabi/newpipe/VideoInfo.java | 1 + .../newpipe/youtube/YoutubeExtractor.java | 19 ++++++++++++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java b/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java index b9bef5cfd..4f28b606d 100644 --- a/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java +++ b/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java @@ -48,6 +48,7 @@ public class ActionBarHandler { private String videoTitle = ""; SharedPreferences defaultPreferences = null; + private int startPosition; class FormatItemSelectListener implements ActionBar.OnNavigationListener { @Override @@ -216,12 +217,18 @@ public class ActionBarHandler { intent.putExtra(PlayVideoActivity.VIDEO_TITLE, videoTitle); intent.putExtra(PlayVideoActivity.STREAM_URL, videoStreams[selectedStream].url); intent.putExtra(PlayVideoActivity.VIDEO_URL, websiteUrl); - activity.startActivity(intent); + intent.putExtra(PlayVideoActivity.START_POSITION, startPosition); + activity.startActivity(intent); //also HERE !!! } } // -------------------------------------------- } + public void setStartPosition(int startPositionSeconds) + { + this.startPosition = startPositionSeconds; + } + public void downloadVideo() { if(!videoTitle.isEmpty()) { String videoSuffix = "." + MediaFormat.getSuffixById(videoStreams[selectedStream].format); diff --git a/app/src/main/java/org/schabi/newpipe/PlayVideoActivity.java b/app/src/main/java/org/schabi/newpipe/PlayVideoActivity.java index d05f721a2..7a691641a 100644 --- a/app/src/main/java/org/schabi/newpipe/PlayVideoActivity.java +++ b/app/src/main/java/org/schabi/newpipe/PlayVideoActivity.java @@ -52,6 +52,7 @@ public class PlayVideoActivity extends AppCompatActivity { public static final String STREAM_URL = "stream_url"; public static final String VIDEO_TITLE = "video_title"; private static final String POSITION = "position"; + public static final String START_POSITION = "start_position"; private static final long HIDING_DELAY = 3000; private static final long TAB_HIDING_DELAY = 100; diff --git a/app/src/main/java/org/schabi/newpipe/VideoInfo.java b/app/src/main/java/org/schabi/newpipe/VideoInfo.java index e0d985d8e..64b21fec6 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoInfo.java +++ b/app/src/main/java/org/schabi/newpipe/VideoInfo.java @@ -50,6 +50,7 @@ public class VideoInfo { public VideoInfoItem nextVideo = null; public VideoInfoItem[] relatedVideos = null; public int videoAvailableStatus = VIDEO_AVAILABLE; + public int startPosition = 0;//in seconds private static final String TAG = VideoInfo.class.toString(); diff --git a/app/src/main/java/org/schabi/newpipe/youtube/YoutubeExtractor.java b/app/src/main/java/org/schabi/newpipe/youtube/YoutubeExtractor.java index 6d23ae038..0b5a20a17 100644 --- a/app/src/main/java/org/schabi/newpipe/youtube/YoutubeExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/youtube/YoutubeExtractor.java @@ -169,6 +169,23 @@ public class YoutubeExtractor implements Extractor { Document doc = Jsoup.parse(site, siteUrl); videoInfo.id = matchGroup1("v=([0-9a-zA-Z_-]{11})", siteUrl); + String timeStamp = matchGroup1("((#|&)t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s)", siteUrl); + Log.i(TAG, "time stamp:"+timeStamp); + //videoInfo.startPosition + + //TODO: test this! + if(timeStamp.length() > 0) { + String secondsString = matchGroup1("(\\d{1,3})s", timeStamp); + String minutesString = matchGroup1("(\\d{1,3})m", timeStamp); + String hoursString = matchGroup1("(\\d{1,3})h", timeStamp); + + int seconds = (secondsString.length() > 0 ? Integer.parseInt(secondsString) : 0); + int minutes = (minutesString.length() > 0 ? Integer.parseInt(minutesString) : 0); + int hours = (hoursString.length() > 0 ? Integer.parseInt(hoursString) : 0); + + videoInfo.startPosition = seconds + (60*minutes) + (3600*hours);//don't trust BODMAS! + //the ordering varies internationally + }//else, leave videoInfo.startPosition as default 0 videoInfo.age_limit = 0; videoInfo.webpage_url = siteUrl; @@ -410,7 +427,7 @@ public class YoutubeExtractor implements Extractor { Element img = li.select("img").first(); info.thumbnail_url = img.attr("abs:src"); - // Sometimes youtube sends links to gif files witch somehow seam to not exist + // Sometimes youtube sends links to gif files which somehow sesm to not exist // anymore. Items with such gif also offer a secondary image source. So we are going // to use that if we caught such an item. if(info.thumbnail_url.contains(".gif")) {