I am working on an Android TV Application using leanback library. I was able to integrate VideoPlaybackActivty using Exoplayer. One of my requirement is when there is about 1 minute left for current video to end, I need to make a Rest call to the server to fetch next video to play and than I would like to show the Next Video thumbnail at the bottom right of the screen. Very much similar to the Netflix functionality of "Next Episode playing in....".
My questions is, how can I find out if there is just 1 minute left in the video. Also, how can I show that "Play Next overlay" on the screen? Currently my activity_videoplayback.xml look as follows.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextureView
android:id="#+id/texture_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<fragment
android:id="#+id/playback_controls_fragment"
android:name="com.myapp.ui.playback.VideoPlaybackFragment"
android:tag="#string/playback_tag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/stream_progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminateTint="#color/orange"
android:visibility="gone" />
</merge>
Really appreciate the help.
ExoPlayer does not provide a progress update listener at the moment - the reason cited is that it would eat up too much CPU to keep raising progress events. See the discussion on the ExoPlayer issue tracker here and note that the issue is considered closed at this time (Feb 2021):
https://github.com/google/ExoPlayer/issues/3980
The suggested approach is to build a handler to poll the player and query the current position using player.getCurrentPosition().
For completeness, in case you are also looking at HTML5 video, in a WebView for example, you can check the video length and then use the progress event to check for 1 second from the end - this is 'standard' HTML5 and javascript so should work with any framework using the HTML5 video element.
The code might look something like this:
const yourVideo = document.querySelector('yourVideoID');
//Create the event handler
function handleProgressEvent(event) {
//Check if video has reached last 10 seconds
if (yourVideo.currentTime > (yourVideo.duration - 10.0)) {
// DO your work to prepare the next video here
}
}
// Add the event listener to your video
yourVideo.addEventListener('progress', handleProgressEvent);
Related
I have a very strange problem with both YouTubePlayerView and the YouTubePlayerFragment.
My app is an alarm clock and i want my users to be able to set a youtube video as their alarm.
Here is the alert fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/standard_button_size"
android:gravity="center_vertical">
<TextView
android:id="#+id/tvSep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:text="-"
android:textColor="#color/white_secondary_text"
android:textSize="#dimen/title_bold" />
<TextView
android:id="#+id/tvNotificationHourYoutube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_toStartOf="#id/tvSep"
android:gravity="end"
android:text="7:00"
android:textColor="#color/white_secondary_text"
android:textSize="#dimen/title_bold" />
<TextView
android:id="#+id/tvAlarmLabelYoutube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#id/tvSep"
android:gravity="center"
android:maxLines="1"
android:text="Wake up"
android:textColor="#color/white_secondary_text"
android:textSize="#dimen/title_bold" />
</RelativeLayout>
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="#+id/ypvAlert"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="#dimen/half_activity_vertical_margin"
android:layout_marginTop="#dimen/half_activity_vertical_margin"
android:minWidth="200dp"
android:minHeight="110dp"/>
<LinearLayout
android:id="#+id/llYoutubeButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnStopAlarmBigYoutube"
android:layout_width="0dp"
android:layout_height="#dimen/standard_button_size"
android:layout_weight="1"
android:backgroundTint="#color/dark_primary_accent"
android:text="#string/stop_alarm"
android:textColor="?android:colorBackground"
android:textSize="#dimen/title_bold"
android:visibility="visible" />
<Button
android:id="#+id/btnSnoozeYoutube"
android:layout_width="0dp"
android:layout_height="#dimen/standard_button_size"
android:layout_weight="1"
android:text="#string/snooze"
android:textSize="#dimen/title_bold" />
</LinearLayout>
</LinearLayout>
And this is the layout xml file of the activity that contains the fragment:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I tested the alarm on 2 smartphones (Nexus 5 and huawei) and on one tablet (Asus). At first i tried to force the app to be on landscape if it is a youtueb alarm by using:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
The thing is that on the nexus 5 the video is almost always stops after 1 second. If i set an alarm for 2 minutes from now it will play ok for 50% of the attempts, and if i set an alarm for tomorrow morning it always stops after 1 second. On the Huawei it almost always play ok and on the tablet it's 50/50.
This is the message i get when the video stops:
YouTube video playback stopped due to unauthorized overlay on top of player.
The YouTubePlayerView is not contained inside its ancestor
android.widget.FrameLayout{89c240f V........ ......I. 0,0-0,0}. The
distances between the ancestor's edges and that of the YouTubePlayerView is:
left: 0, top: 0, right: 0, bottom: 0 (these should all be positive).
I don't have any FrameLayout on the alarm alert fragment but i have a FrameLayout on the activity xml file that contains the fragment so i replaced the FrameLayout with fragment, so the xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/articles_list"
android:name="com.sux.alarmclock.AlarmNotificationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_alarm_alert_material_youtube" />
But i still get the same error message even though now there is not any FrameLayout in the xml. I have interstitial ads in my app so i canceled the ads but the error still occurs.
So i tried to set the activity to portrait instead of landscape and now it's working on the nexus and on huawei most of the times but almost always stops on the tablet after 1 second.
I also noticed that when i include the YouTubePlayerFragment inside my Activity , or when i extend the YouTubeBaseActivity, every time i start the activity, it creates 2 instances of the activity instead of 1, regardless to the flags i add to the intent or the launch mode (I don't know if it has something to do with the issue)
I can play YouTube on WebView using the RTSP links, but in order to extract the RTSP link i need to use this project:
https://github.com/HaarigerHarald/android-youtubeExtractor
And the developer told me that they are extracting the RTSP urls through backdoor which might be aganist the Yotube policies.
I want the YouTube player to work on all devices all the times. Does anyone has an idea?
These are the possible reasons for this issue. Please check the logcat to find out which is your issue
UNAUTHORIZED_OVERLAY - Other views overlapping with youtube view
PLAYER_VIEW_TOO_SMALL - Youtube view too small
USER_DECLINED_RESTRICTED_CONTENT - Trying to play restricted content
BLOCKED_FOR_APP - Certain videos are blocked for app.
When i had the issue, it was because of UNAUTHORIZED_OVERLAY.
<RelativeLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/scImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<FrameLayout
andriod:id"#+id/youtube_parent
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
The imageview was below the framelayout which had the youtube player. I moved it to the top (The final, fixed layout is the example above) and it started to work.(I tried setting the Zindex it didnt help).
Based from this documentation, be noted that while videos are playing, View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay the view with other views while a video is playing.
Additional reference which might help:
Pausing youtube video after 1-2 seconds
It is not possible to add buttons as overlays above the player as specified by Google, otherwise the player will stop.
Padding is also not supported on YouTubePlayer.
To overlay your view on video, I will recommend you to use ExoPlayer, It is not part of the android sdk, but it's recommended by google and included in android developer documentation :
http://google.github.io/ExoPlayer/
Views overlayed above YouTubePlayerFragment or YouTubePlayerView in the layout hierarchy cause playback to pause immediately
Overlays on top of any of our YouTube players (including the YouTube Android Player) are not supported. When an overlay is detected, playback stops and the log contains information helpful in debugging the issue. We do support Action Bar overlays, take a look at this demo to learn more: https://developers.google.com/youtube/android/player/sample-applications#Overlay_ActionBar_Demo.
Hope this helps!
I am trying to implement Floating video player (like YouTube player which minimize when dragged to bottom right of the screen). For that I am using DraggablePanel library (https://github.com/pedrovgs/DraggablePanel).
I was having some problem while integrating it with our production code so I try to integrate it with JW player sample app
(the VideoPlayer that we are using in our app is JW player). JW player sample app is on GitHub: https://github.com/jwplayer/jwplayer-sdk-android-demo
I changed only the xml layout. But its not working as expected.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/output2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/ccl_blue" />
<com.github.pedrovgs.DraggableView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:draggable_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/draggable_view_my"
android:layout_width="match_parent"
android:layout_height="match_parent"
draggable_view:bottom_view_id="#+id/output"
draggable_view:top_view_id="#+id/linearlayout" //if i give id of JWPlayerView, then app crashes. So i wrapped it inside a LinearLayout
draggable_view:top_view_margin_right="1dp"
draggable_view:top_view_resize="true"
draggable_view:top_view_x_scale_factor="2.5"
draggable_view:top_view_y_scale_factor="1.5"
draggable_view:enable_click_to_maximize_view="false"
draggable_view:enable_click_to_minimize_view="false"> //this is not working. Player is getting minimised if i click on it
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
android:orientation="vertical">
<com.longtailvideo.jwplayer.JWPlayerView
android:id="#+id/jwplayer"
android:layout_width="match_parent"
android:layout_height="250dp" />
<TextView //this TextView is not coming. Neither in Expanded view nor in minimized view
android:id="#+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#f01232"
android:text="Bottom Text View" />
</LinearLayout>
</com.github.pedrovgs.DraggableView>
</FrameLayout>
1) The player is getting minimized even when draggable_view:enable_click_to_minimize_view="false". So I am not able to pause the player or click any of the icons which appear
on the player skin. Sometimes I manage to click on the play/pause button in the expanded view mode but it also results in minimizing the player view as well.
2) video frames are not coming most of time when player is minimized. see below photo link. a white screen is coming at the bottom right corner.
https://imgur.com/a/kjqyVOe
3) if I dismissed the minimized player view(by dragging it to left/right), the audio is still coming even though the VideoView is gone.
4) if i click on fullscreen icon on playerview then it goes fullscreen but instead of showing video frames only a white screen is coming.
5) Its really hard to maximize the player. You have to SLOWLY drag upwards the minimized view exactly from the center, otherwise the minimized view goes left or right and gets dismissed.
Is the library incompatible with JW PlayerView? or Is there any other library for the same? If there is no library then how can I achieve this in android?
Floating video player like youtube is technically known as picture in picture mode. When you will back press the player minimize to right bottom corner. Even you can drag player to other position. It is little bit lengthy process but you can make it.
Here is reference tutorial.
I want to implement something like this. So that I can have control over its color, number of bars, speed, etc.
Below is the animation that is shown next to the item that is currently playing in the Google-Play-Music app.
I will play this in a similar manner: next to the currently playing song in a music app.
There is a library called Mini Equalizer Library for Android which provides this functionality.
You need to add the view to your layout:
<es.claucookie.miniequalizerlibrary.EqualizerView
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/equalizer_view"
android:layout_width="30dp"
android:layout_height="30dp"
custom:foregroundColor="#color/link_text_material_light"
custom:animDuration="3500"/>
In order to start the animation, use this code:
EqualizerView equalizer = (EqualizerView) findViewById(R.id.equalizer_view);
equalizer.animateBars(); // Whenever you want to tart the animation
equalizer.stopBars(); // When you want equalizer stops animating
Also, there is another library called AndroidVuMeter which seems to have more options.
Just add the view to your layout.
<io.gresse.hugo.vumeterlibrary.VuMeterView
android:layout_width="300dp"
android:layout_height="300dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="40dp"
android:id="#+id/vumeter"
vumeter:stopSize="5dp"
vumeter:speed="10"
vumeter:blockNumber="5"
vumeter:blockSpacing="20dp"
vumeter:backgroundColor="#33b5e5"
vumeter:startOff="false"/>
I am trying to play through a sequence of video files using the native MediaPlayer, and cannot have any black flickering between them.
I realize that this issue has been brought up many times before, but the questions are mostly a few years old. Typical responses are swapping between multiple media-players, which doesn't seem to work.
I thought that the widespread use of fragments may have introduced a new solution, and indeed I could get seamless transitions by building a stack of fragments with multiple SurfaceViews visible at the same time, and playing video from the top of the stack down, but of course this is not scalable, as the entire playlist needs to be stored in memory before playback starts.
Just wondering whether anyone has achieved this in recent years? Or would there be any other viable alternative to using SurfaceView or native MediaPlayer? I am trying to use a TextureView instead, but currently cannot get it working.
I used below code to fix issue with flickering:
<CalculationLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:orientation="vertical">
<LinearLayout
android:id="#+id/activity_display_journal_fragment_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<SurfaceView
android:layout_width="0px"
android:layout_height="0px" />
</LinearLayout>
LinearLayout with id activity_display_journal_fragment_content holds fragment, that contains VideoView.
I am completely stumped on something that I thought would be the easiest thing ever. I am trying to play a video inside a frame in a relative layout.
Note that the application is meant to run on a very specific device (Archos 101 internet tab) in landscape mode, and the videos are pre-encoded with a very specific size of 480x360, which is why the layout has some fixed sizes.
Here is the layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Background -->
<ImageView
android:layout_width="980dp"
android:layout_height="568dp"
android:src="#drawable/bg_video" />
<TextView
android:id="#+id/video_headtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:layout_marginLeft="255dp"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:id="#+id/video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:layout_marginTop="112dp" />
<VideoView
android:id="#+id/video_videocontainer"
android:layout_width="480dp"
android:layout_height="360dp"
android:layout_below="#id/video_title"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I load the video using this code
player = (VideoPlayer) findViewById(R.id.video_videocontainer);
player.setVideoURI(videoUri);
The code works, but while the videoView is correctly inside the frame, the video itself plays somewhere in the top of the screen, and only part of it is shown in the VideoView.
Example of what happens here (This is only a temp video for debugging).
Screenshot of the video at the same moment here.
As you can see, the VideoView only shows part of the video, filling the rest with black.
I'm pretty sure I'm missing something really simple, but I can't figure out what at all.
Thanks in advance.
EDIT : I have tried writing a VideoPlayer class that extends VideoView and overrides onMeasure like this
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
setMeasuredDimension(480, 360);
}
public void changeVideoSize()
{
getHolder().setFixedSize(480, 360);
requestLayout();
invalidate();
}
But it doesn't help a single bit.
EDIT 2 : Tried with a SurfaceView and MediaPlayer, same result. I have the feeling I haven't understood the principle of playing a video on Android. Getting quite stuck here.
EDIT 3 : I have tried on a Samsung Galaxy HD and the video shows correctly inside the frame. I wonder if this is a problem with the tablet, or with 2.2?
EDIT 4 : AVD running under 2.2 doesn't render the video properly. AVD running 4.1 does. I guess that narrows it down a tad.
I had similar problem with a video that had a high too profile for that particular device (2.1) to be handled and it worked fine for android 4.0.
Other than that, I am using SurfaceView and the trick was to add listeners to the events for the MediaPlayer (setOnVideoSizeChangedListener in particular) and the callback to the surface linked to the mediaplayer events.
Set the VideoView attribute setZOrderOnTop to true and see if it works.
For instance if I have a videoview called qvideo
qvideo.setZOrderOnTop(true);