Playing a video in my app's background? - android

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.

Related

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!

Splash Screen Background Image color inconsistent

I have a Splash Screen with a background Image on top of it. I have a Video View which plays the video. The background of the video and that of the rest of the screen must match, hence I extracted the background color from the video and added a background image of the same color in the XML file.
The problem is, the color of the image does not match with the video on all versions of Android.
On Sony Xperia SP with v4.3:
On Micromax A1 with v6.0.1:
How do I get consistent result across all (or most) versions of Android?
XML of Splash Screen:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/logoback"
>
<VideoView android:id="#+id/myvideo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Instead of using drawable for setting the background, extract the colour code and set the colour code to background. It might resolve the issue.

No resource found that matches the given name keeps showing

when am trying to put a background to the layout the app just stops and exit ("unfortunatly, xxxx has stopped working") although the splash and main layouts works fine but as soon as i enter a new layout ex: contact us page ) with no background the app works fine :S :S
here's the code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/ibg" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/au" />
</LinearLayout>
Try setting another background and check if the problem still exists. I thing there is some problem with your files in drawable folder. Try not setting a .gif image as background and set a jpg or png image.

Make use of the default buttons in the Android sdk

I would like to make a button, a simple button, having the default Search icon that is used in Android.
However, I do not want to make my own xml file and put the images in the Drawable folder, because i know they already exist in the Android sdk.
So why not making use of them?
I tried to make something like this:
android:background="#android:drawable/...." but there in this directory it seems that all the files are png file not xml file able to interact with the user (on button pressed, etc..)
I hope an expert can help solving this problem.
You don't need an xml file for the button to work. The png files in the drawables are just for the image. You can create a button programmatically or in the xml but you still have to create it somewhere because the Button instance is what is used for the onClick() and not necessarily the xml. Either way you must have an xml file for your Layout to use in setContentView() so you can put a Button in that layout file or create it in your Java code but either way, you have to create a Button
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
in this Button you can set the background or use an ImageButton instead and set `android:src="#drawable/..."
Then in your code you still have to get the button instance
Button btn1 = (Button) findViewById(R.id.button)
after you have called your layout like
setContentView(R.layout.your_layout_file);
If you use an ImageButton just replace Button with ImageButton which is what it sounds like you want. Hope this helps it make a little more sense for you

Adding interactive movie to Android application

I'm building a new application to the Android platform and I need to add an interactive movie that would show to the user, how to use the application.
I want something like the video in Angry Birds, with the button that spreads to the outside of the dialog.
Like this:
First I though that I need some kind of a animation video, so I checked the Android video view, but I can't figure out how to use it, and if I actually need it?
Secondly is this window is a regular dialog or something else?
I tried the regular dialog but I didn't came with the idea of this button, all the buttons is inside the dialog itself and I can't show it like I wan't (e.g. Angry Birds dialog).
Any help will be great, Thanks.
EDIT: This is the custom dialog:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/videoView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Test"
android:textSize="50dip"
android:textColor="#android:color/white"/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#id/videoView"
android:layout_marginBottom="20dp"
android:layout_marginRight="85dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
And this is what I get:
Angry birds doesn't use the the Android Ui elements. They use a custom framework to show their dialogs. Therefore you will need to customize your own dialog as well.
If you your application really needs a video to demonstrate its use, maybe you should reconsider your application functionallity. Maybe a simple application will do with a set of screenshots and annotations.

Categories

Resources