Play video on part of the screen - android

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

Related

Netflix style next episode for Exoplayer Leanback

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

YouTubePlayer sometimes pauses after 1 second on some devices

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!

App doesn't loaded in mobile as do in eclipse emulator

I know that the question is rather rare, but also is rare what happens with my app...
I programmed yesterday a presentation activity for my app, which only appears 1,5 seconds before starting the main activity. The presentation activity just load the presentation.xml file in which there only are a LinearLayout with an ImageView and a TextView. This ImageView has attached an image with the logo of my app (.png) and the TextView is just below.
What happens is that when I run the app with the Eclipse emulator I can see perfectly just what I programmed. I see, for 1,5 seconds, a custom background with my logo in the center of the screen and the text below it, but things goes wrong when I try the app in my mobile: the logo is absolutely invisible! It just disappears.
Must say that before setting that image, there were the same logo with less resolution. Also I tried to set the image and the parameters from java code but happens the same and it's so disappointing...
Hope you know what is happening and bring me a good solution... Thank you!
Code affected:
JAVA
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.presentacion_act);
RelativeLayout lay = (RelativeLayout)findViewById(R.id.back);
lay.setBackgroundResource(R.drawable.back_presentacion);
XML
<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"
android:background="#android:color/darker_gray"
android:id="#+id/back"
tools:context=".Presentacion" >
<ImageView
android:id="#+id/lay_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:paddingBottom="70dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:src="#drawable/classmarks"/>
Done! Just making the image smaller it runs perfectly on my device.

Missing or Incorrect images and backgrounds randomly throughout app lifecycle

I was hoping someone here might have an idea what causes this sort of behaviour:
Throughout my application, in seemingly random places and in random conditions I'm observing this strange UI issue. Images are on occasion being loaded black (with the correct bounds) or with the incorrect image source (again, with the correct bounds). This effects ImageViews and has effected android:background tags with references to colour resources.
My application relies on 6 library projects, it runs Native Code via a Service and Activities in the App use GlSurfaceViews (although, not all Activities which display the problem contain OpenGL components). The problem could I suppose be from any of these places or a combination of them through using large amounts of memory.
You can see this behaviour in the following screen shots:
This is actually a 6 or so pixel wide column separator image which has been incorrectly drawn into my ImageView (the ImageView seems to have correctly sized itself).
When going out of the Application and then back in again (repeatedly) it instead appeared (and remained) like so:
After a Force Clear and a Clear App Data it returned to the correct format:
As you can also see the Magnifying Glass image next to it is displaying fine in each of these. The problems with these missing/incorrect images and backgrounds seems to happen randomly, throughout the application lifecycle, and I've been unable to find a way of reproducing it.
The layouts for these images are nothing special, I'm not doing anything funny during the rendering lifecycle (i'm not overriding onDraw() or onMeasure() or the like). The source of these images aren't being set dynamically but via the XML.
As you can see from the above example, it's not a build issue as it occurs between app lifecycles not between installs. It's also happening on different devices, Samsung 8.9, Acer Iconia Tab, Motarola XOOM,
It seems to me to be some sort of error with the reference table, could it perhaps have been nudged by my native code? Or is it an effect of me in some stages of the application using too much memory?
Here's the XML source for the above example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/browseProgressWrapper"
android:layout_width="match_parent"
android:layout_height="#dimen/actionbar_compat_height"
android:orientation="horizontal">
<RelativeLayout android:layout_width="#dimen/search_bar_width"
android:layout_height="match_parent">
<EditText android:id="#+id/browseFilter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:imeOptions="actionSearch"
android:background="#drawable/edit_text_blue"
android:maxLength="30"/>
<ImageView android:id="#+id/clearSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_input_delete"
android:layout_marginRight="5dp"/>
</RelativeLayout>
<ImageView android:id="#+id/browseFilterButton"
android:src="#drawable/ic_menu_search"
android:scaleType="center"
android:layout_width="#dimen/actionbar_compat_height"
android:layout_height="#dimen/actionbar_compat_height"
android:layout_gravity="center_vertical"
android:minWidth="#dimen/actionbar_compat_height"/>
</LinearLayout>
A more full description of the code / layout surrounding another such occurrence I happened to get the screenshot for:
I have a "Settings" Activity which restarts my app after saving new settings details. It does this by stopping a Service, calling a new Activity (the Splash Activity) and finishing itself:
mConfiguration.save();
mConfiguration = new Configuration(Configuration.getInstance());
getActivity().stopService(new Intent(getActivity(), NativeService.class));
getActivity().finish();
startActivity(new Intent(getActivity(), SplashActivity.class));
Most of the time (and on most devices) this works fine, the Splash Activity contains an image which loads correctly. Sometimes though on some devices the Splash Activity loads either an incorrect resource (what my testers refer as "an upside down Nike tick") or just a blank box (as seen below). Does anyone know why?
Here is the Layout for the Splash page, as you can see it's pretty simple, no surprises:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/ContentBackgroundColor"
android:orientation="vertical" >
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/manager_android_400" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ProgressBar
style="#android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>
Theory tested and debunct:
I've theorised that this could be a processor/memory issue where the Layout isn't being drawn fully before the Splash screen exits and moves onto the next Activity so I put in this piece of code:
image = (ImageView) findViewById(R.id.image);
image.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
image.getViewTreeObserver().removeGlobalOnLayoutListener(this);
moveToStartScreen.start();
}
});
The hope was the code above would make sure the Image is definitely loaded before moving onto the Start page but seems to have had no observable effect.
Another Theory
I was also wondering if this could be being caused by the R.id / R.colour / R.drawable resources some how being currupted in program execution? Does anyone know why that might happen.
Could my native code be running rampant on some memory addresses that Android isn't correctly allocating?
Has anybody noticed this before - or perhaps know why this behaviour occurs?
Graeme, I had almost the same problem and found out that it was a reported bug of the android plattform. It was corrected in the 3.0 version I think. Which API are you compiling with? Try to build with the last available api and be sure to compile with JDK 1.6
If your problem is related to this bug, this should fix the problem.
This is a simple problem of refresh, clean, and rebuild.
Images in your various drawable folders or resource id indices are out of sequence because they were either changed outside of the eclipse IDE (via external source control such as GIT, SVN or other edits) and not refreshed in the eclipse navigator. Or, the files may have been updated in a library project upon which your UI Activity depends.
I have found that although .java file dependencies are propagated throughout the system, this is not always the case for resources such as images and .xml files.
The solution is fairly simple, clean everything, refresh all of your projects, and rebuild. The stretched or black edges should be gone.
Note: The predominant manifestation of this problem occurs when 9-patch images become treated like standard .png images. This means that they get stretched in a linear manner across the image instead of just at the edges. To me, this explains your 'Torn/Stretched' example. I have seen similar often. Another common manifestation is that text strings occasionally get displayed with the wrong resources!

Playing a video in my app's background?

I want to make android home page background. I have imported a video file hp.mkv into the drawable folder. And call the file in main.xml. Following is my code snippet:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/hp">
<Button android:id="#+id/next_button"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:layout_marginLeft="270dp"
android:layout_marginTop="180dp"
android:background="#drawable/button_background"/>
</RelativeLayout>
Again In main activity file called like this: setContentView(R.layout.main);
But while running the application I saw a black screen coming behind the button, where I was expecting the video file to come. Can any one suggest me how to get out from this problem. Or any idea of using video in app's background in android.
Video files are not supported as "drawables". In other words you can't put it as drawable resource and expect it to be played back.
If you need video playback, you can use MediaPlayer. But that would require some extra code to be added to your activity.
A video file is NOT a drawable. You need to use a VideoView, to handle the displaying of the video.

Categories

Resources