ProHoster > Blog > internet news > Death of Koshchei in the list of recommendations (is it possible to make YouTube cozy and safe?)
Death of Koshchei in the list of recommendations (is it possible to make YouTube cozy and safe?)
The introduction is short: I want to talk about the YouTube online player for Android with local playlists, channels and recommendations.
Expanded introduction:
Some time ago, I faced exactly the same problem as the author of a wonderful application Channel Whitelist, and determined for myself exactly the same attitude towards her: I want to be able to give the child a tablet or smartphone with cartoons from time to time, but I’m not at all satisfied with where, after 2-3 clicks, the list of recommendations leads the child in standard applications - YouTube clients.
Unfortunately, after installing the Channel Whitelist application, it already had another more prosaic, but still fatal flaw - NIH seemed to me (and, most importantly, to my son) its interface not very convenient, especially after the habit of using the YouTube Kids player.
In general, after some time I matured to make my own implementation. After some time, it became possible to put a tag on the first release.
Key features:
Add your favorite channels and playlists - they will be saved and indexed in the local database
Turn off extra videos inside the added playlists if you don't need them
The list of recommendations is randomly generated only from the channels and playlists added to the application
What follows is an overview of the main features in more detail, plus some technical details on how to play YouTube videos in your Android app without using the Google APIs and web wrappers.
On the main screen and on the player screen: random recommendations from non-random channels
Instant search in the local database
=>
Add a new channel or playlist
Search by name online or insert a known address. The list of videos of a channel or playlist is saved to a local database, icons are not cached.
Dynamic playlist - play search results
=>
In the recommendations under the video, there will be only videos that satisfy the search query.
Similarly, if you open a video from the playlist settings, only videos from the same playlist will appear in the recommendations list.
Playlists and channels can be temporarily turned off and on again
Please note: videos from the disabled playlist will also disappear from the search results, browsing history and from the favorites list. But do not worry, they will appear there again immediately after the playlist is turned on again.
Blacklist video
A blocked video will not appear in recommendations, in search results, will disappear from the list of favorites and from the viewing history. The video will still be visible in the playlist settings.
View the blacklist and re-enable items blocked by mistake: Settings > header menu > Blacklist
Favorite videos and viewing history
Favorite videos on the player screen are marked with an asterisk in the upper right corner.
Context menus in the screen title and on long click in galleries and lists
Copy the name or address of a video or playlist in the watch screen or any list.
Quick start - add recommended channels and playlists
=>
The application will immediately look like the screenshots above.
Unnecessary channels and playlists can be turned off or deleted in the settings.
Not on Googleplay and will not be in the near future (Google bans applications that, bypassing the API, parse their site, including the aforementioned Channel Whitelist or the NewPipe player)
Keep in mind that switching between different versions from different sources on the same device will not work due to different signatures of the apk file, before installing the version from a new source, you will first have to delete the installed version along with the data - playlist cache and browsing history (or figure out how these transfer data).
Technical details
Does not require a Google / YouTube account, only the Internet is needed, uses libraries:
Question: Is scraping sites without the permission (or with an explicit ban) of the authors generally legal? Google removes apps from Google Play, which do not use their API, but parse their sites, because. they violate their user agreement. Answer: Sure, legal, it's up to you which tool to use to read public information. Furthermore: The US court completely legalized website scraping and forbade it from being technically obstructed, but Gul may have a different opinion, personally I have no desire to go to an American court to convince them.
Some code
Library NewPipeExtractor - player helper project NewPipe, allows you to download a list of videos for the specified channel or playlist, download detailed information about a famous video (what is visible on the web page of the video), get the address of the video icon, and also get the address of the video stream.
The code for loading the playlist is a little cumbersome, so I won’t bring it here, for those who are interested - look at the source, it’s basically a class ContentLoader.
Let's see how to get the address of the video stream from the address of the public video page and play it in the player.
It is curious that after that it still cannot be used, because. the examples will complain about the missing Downloader class. It can be copied to the project from the automatic tests directory NewPipeExtractor/extractor/src/test/java/org/schabi/newpipe/Downloader.java - works for version 0.17.4 (it seems that this part has been redone in a newer version of the library, but still needs to be checked).
Get the address of the video stream from the address of the video page on the YouTube website:
The player will exoplayer from Google itself. This is not a web wrapper over YouTube, but a real embeddable player for playing any videos, quite flexible and customizable. Including knows how to play video streams from YouTube, if you specify the correct address for it. We have just received the stream address, so let's see how to send it to the player.
We will not consider all the nuances of placing the player component on the application screen (you can see it in the examples on the project website or in the code), we will only look at how to start playing videos from YouTube in the player at the address obtained above:
private void playVideoStream(final String streamUrl, final long seekTo) {
if (streamUrl == null) {
// остановить проигрывание текущего ролика, если был загружен
videoPlayerView.getPlayer().stop(true);
} else {
// https://exoplayer.dev/
// https://github.com/google/ExoPlayer
final Uri mp4VideoUri = Uri.parse(streamUrl);
final MediaSource videoSource = new ProgressiveMediaSource.Factory(videoDataSourceFactory)
.createMediaSource(mp4VideoUri);
// Поставим на паузу старое видео, пока готовим новое
if (videoPlayerView.getPlayer().getPlaybackState() != Player.STATE_ENDED) {
// Если ставить на паузу здесь после того, как плеер встал на паузу сам, закончив
// играть видео, получим здесь второе событие STATE_ENDED, поэтому нам нужна здесь
// специальная проверка.
// При этом значение getPlayWhenReady() останется true, поэтому проверяем именно состояние.
// https://github.com/google/ExoPlayer/issues/2272
videoPlayerView.getPlayer().setPlayWhenReady(false);
}
// Prepare the player with the source.
((SimpleExoPlayer) videoPlayerView.getPlayer()).prepare(videoSource);
// Укажем текущую позицию сразу при загрузке видео
// (в коментах что-то пишут что-то про датасорсы, которые поддерживают или не поддерживают
// переходы seek при загрузке, похоже, что это фигня - просто делаем seek сразу после загрузки)
// Exoplayer plays new Playlist from the beginning instead of provided position
// https://github.com/google/ExoPlayer/issues/4375
// How to load stream in the desired position? #2197
// https://github.com/google/ExoPlayer/issues/2197
// в этом месте нормлаьный duration еще не доступен, поэтому его не проверяем
//if(seekTo > 0 && seekTo < videoPlayerView.getPlayer().getDuration()) {
if (seekTo > 0) {
// на 5 секунд раньше
videoPlayerView.getPlayer().seekTo(seekTo - 5000 > 0 ? seekTo - 5000 : 0);
}
videoPlayerView.getPlayer().setPlayWhenReady(true);
}
}
Known Issues
Will not play videos with age restrictions that require a Google/YouTube account login
advice: add such videos to the blacklist or ask the author of the video to remove the restriction set by mistake.
Some broadcast videos for which the service returns zero length will not play (for such videos, the duration in the lists and gallery is marked as “[dur undef]”)
If you come across a public video that does not require a login, plays in the browser, but does not play in the player, send a bug report (it is quite possible, the problem has already been fixed in the new version NewPipeExtractor and you will only need to update the assembly with this version, for example).
The interface can slow down with a slow (but not turned off) Internet
Eventually
The son moved from a tablet to a Samsung smart TV, which cannot run Android applications. Therefore, the best parental control is still personal.
But the application turned out to be convenient enough for me to start using it myself. The first impression from the early working versions was that I got into another world. All content is downloaded from YouTube, but this is no longer YouTube, but something else, safe and controlled, as if you took a centipede out of your eye and put it in a glass jar. And it's all about the recommendations.