I am trying to create a view that has a video playing and two ImageButtons all on the same screen. I am using VideoView to contain my video and ImageButtons for both of my buttons. Both display correctly when I test each feature independently, but they will not display together at the same time when I try to show both on the same screen! I have tried a number of layouts (frame, linear, relative), constraining the VideoView to a smaller layout_width & layout_height, and have tried weights in the xml file, but nothing seems to be working. Displaying this seems too straightforward to require a custom View, but I will do it if I have to.
Here are my questions: do you know how to make imageButtons and VideoView display on the same screen?
Can you use Android views like VideoView and ImageButton when you create a custom view? Or can you just draw 2D things on canvases in custom views?
Here is my code as reference: XML
<LinearLayout
android:id="#+id/videopart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1">
<VideoView
android:id="#+id/videoplayer"
android:layout_width="395dp"
android:layout_height="111dp" >
</VideoView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<ImageButton
android:contentDescription="#string/top"
android:id="#+id/topbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dip"
android:src="#drawable/yesbutton"
android:background="#null"/>
<ImageButton
android:contentDescription="#string/bottom"
android:id="#+id/bottombutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:src="#drawable/nobutton"
android:background="#null">
</ImageButton>
</LinearLayout>
And activity:
public class iplayer extends Activity {
VideoView videoHolder;
MediaPlayer mp;
ImageButton top, bottom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.interactivevid);
videoHolder.setVideoURI(video);
// video finish listener:
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// The video has finished, return from this activity
finish(); //close activity
}
});
videoHolder.requestFocus();
/*
videoHolder.requestLayout();
videoHolder.invalidate();
videoHolder.getLayoutParams().width = 20;//480;
*/
//start video
drawButtons();
//videoHolder.setPadding(BIND_NOT_FOREGROUND, BIND_NOT_FOREGROUND, BIND_NOT_FOREGROUND, BIND_NOT_FOREGROUND);
videoHolder.setPadding(5, 0, 5, 200);
setContentView(videoHolder); // used to actually put in video. when removed, shows buttons
videoHolder.start();
}
private void drawButtons(){
//make buttons invisible
top = (ImageButton)findViewById(R.id.topbutton);
top.setImageResource(R.drawable.yesbutton);
top.setVisibility(View.VISIBLE);
bottom = (ImageButton)findViewById(R.id.bottombutton);
bottom.setImageResource(R.drawable.nobutton);
bottom.setVisibility(View.VISIBLE);
}
}
I'm not by a computer where I can test this now but what you may want to try is an absolute layout.
You should be able to position the buttons on top of the video with it here is an example
http://www.tutorialforandroid.com/2009/01/absolutelayout-in-android-xml.html
You may also want to try putting the buttons and the VideoView in the same layout
Related
I am adding a basic video player with VideoView and MediaController.
The SeekBar provided by the MediaController stops at 0:02 min while the video is playing till the end.
I have tried this on 3 different devices with different video lengths.
How to ensure that SeekBar progress and time are in sync with the video?
Here is the relevant code:
VideoPlayerDialog.java
videoView.setVisibility(View.VISIBLE);
MediaController mediaController = new MediaController(getContext());
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.BOTTOM;
mediaController.setLayoutParams(lp);
((ViewGroup) mediaController.getParent()).removeView(mediaController);
((FrameLayout) findViewById(R.id.videoViewWrapper)).addView(mediaController);
videoView.setMediaController(mediaController);
videoView.setVideoURI(imageUri);
videoView.requestFocus();
dialog_image_preview.xml
<?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:background="#color/black87">
<uk.co.senab.photoview.PhotoView
android:id="#+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/videoViewWrapper"
android:layout_centerInParent="true"
android:layout_margin="30dp">
<VideoView
android:id="#+id/videoplayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
<ImageButton
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/circular_button_material"
android:elevation="10dp"
android:padding="5dp"
android:src="#drawable/ic_close" />
</RelativeLayout>
I have found the answer. On your MediaController constructor, override it's hide() void and use this.show() in it so the MediaController shows when it hides.
The reason for this, is because MediaController automatically hides itself after 3 seconds by default, even with show(0). So, if you force the MediaController to be shown, it will still act as if it's hiding after 3 seconds (seekbar stops syncing, play/pause button not updating). Overriding the hide() void to show the MediaController combats this.
You can also create a custom class that extends MediaPlayer to ovverride hide() as well.
Example code:
final MediaController mediaController = new MediaController(context){
#Override
public void hide() {
this.show();
}
};
You could try connecting automatically the media controller to the Video View with
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
Followed reference from here, I was able to resolve issue of seekbar which was stopping after 2 seconds and back press issue on which the screen was getting freeze.
I used dispatchkeyevent to detect back press and showed media controller after the start of video.
videoView.setOnPreparedListener { mp ->
progressBar.visibility = View.GONE
mc = object : MediaController(mContext) {
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (event.keyCode == KeyEvent.KEYCODE_BACK) {
super.hide()//Hide mediaController
scoreDialog.dismiss()//Close this dialog/activity
return true//If press Back button, finish here
}
//If not Back button, other button work as usual.
return super.dispatchKeyEvent(event)
}
}
videoView.setMediaController(mc)
mc.setAnchorView(videoView)
(mc.parent as ViewGroup).removeView(mc)
(scoreDialog.findViewById(R.id.videoViewWrapper) as FrameLayout).addView(mc)
mp.start()
mc.show(0)
}
I am trying to play video inside a videoview but a blank video view is showing. The video view is inside a popup window. The video is stored in the external directory and I am using setVideoPath to pass the file path.
java code for it:
final View videoPopupView = getLayoutInflater().inflate(R.layout.popup_video_preview, null);
Button cancelVideo = (Button)videoPopupView.findViewById(R.id.cancelVideo);
Button confirmVideo = (Button)videoPopupView.findViewById(R.id.confirmVideo);
final VideoView videoView = (VideoView)videoPopupView.findViewById(R.id.popupVideoView);
final PopupWindow video_popup_window = new PopupWindow(videoPopupView,RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT, true);
if(new File(attachment_Path+attachment_Name).exists())
{
video_popup_window.showAtLocation(videoPopupView, 1, 0, 0);
videoView.setVideoPath(attachment_Path+attachment_Name);
if (mediaController == null) {
mediaController = new android.widget.MediaController(MainActivity.this);
}
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
}
And the popup layout looks like this:
<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="#80000000">
<VideoView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/popupVideoView"
android:background="#drawable/blackborder"
android:layout_marginTop="25dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal">
<Button
android:text="Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/cancelVideo"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Attach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/confirmVideo"
android:layout_alignParentBottom="true" />
</LinearLayout>
</LinearLayout>
I have referred other solutions and tutorials but everywhere its done like this only. Can someone tell me what is it that I am missing?
EDIT:
This is the file path that is going through :
/storage/emulated/0/Movies/issue_2016_12_30_15_31_13.mp4
EDIT2:Codec Info:
Stream 0
Type: Video
Codec: H264 - MPEG-4 AVC (part 10) (avc1)
Language: English
Resolution: 720x1280
Frame rate: 5.564744
Decoded format: Planar 4:2:0 YUV
Stream 1
Type: Audio
Codec: AMR narrow band (samr)
Language: English
Channels: Mono
Sample rate: 8000 Hz
Bits per sample: 32
You have to go for a custom dialog, add this code -
Dialog mVideoDialog ;
VideoView mVideoFullScreen;
MediaController controller;
Create a method -
mVideoDialog = new Dialog(this);
mVideoDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mVideoDialog.setContentView(R.layout.dialog);
mVideoDialog.setOnKeyListener(this);
mVideoFullScreen = (VideoView) mVideoDialog.findViewById(R.id.videoview1);
controller = new MediaController(this);
showVideo();
}
//Show video method to play the video file -
public void showVideo() {
// TODO Auto-generated method stub
mVideoDialog.show();
mVideoFullScreen.setVideoPath("file:///sdcard/video file name.m4v");
controller.setMediaPlayer(mVideoFullScreen);
mVideoFullScreen.setMediaController(controller);
mVideoFullScreen.requestFocus();
mVideoFullScreen.start();
}
try this code
I want to resize my video and show it in the video view and maintain the quality of the video also, my main focus is that the video quality should not be harmed and it should be shown in the video view
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView= (VideoView)findViewById(R.id.videoViewR);
Video v=new Video();
MediaController mc= new MediaController(this);
mc.setAnchorView(videoView);
Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/eveq.3gp");
videoView.setMediaController(mc);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
and my xml is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<VideoView
android:id="#+id/videoViewR"
android:layout_width="wrap_content"
android:layout_height="480dp"
android:layout_centerVertical="true" >
</VideoView>
</RelativeLayout>
If I understand your question then you don't have to do any special programming to resize the video to your VideoView. Let the MediaController and VideoView do all the scaling work for you.
First, your VideoView seems unusual (Why is height spec'd as 480 when the relative layout is only 270?). I'd suggest changing your VideoView to something like this:
<VideoView
android:id="#+id/videoViewR"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:adjustViewBounds="true"
android:scaleType="center" />
This will make the VideoView expand to the space available in the RelativeLayout and will adjust the bounds automatically.
Then the RelativeLayout should be as large as possible or the size you want (480x270 ?). To fill the space try
android:layout_width="match_parent"
android:layout_height="match_parent"
This will grow the layout to fill the space. Your video will then be scaled to fit these boundaries.
Since 24 hours I'm trying to set the Anchor view of my MediaControler without success.
My VideoView is embedded in a bigger application but even with a very simple testing app I don't understand the problem.
Here is the code of the onCreate of my testing activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vv = (VideoView) findViewById(R.id.videoView1);
vv.setVideoURI(Uri.parse("http://192.168.30.188/test.mp4"));
final MediaController mediaController = new MediaController(ll.getContext());
vv.setMediaController(mediaController);
vv.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaController.setAnchorView(vv);
}
});
vv.requestFocus();
}
And here is my simple 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"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/layout1"
android:layout_width="218dp"
android:layout_height="162dp"
android:background="#android:color/holo_purple">
<VideoView
android:id="#+id/videoView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>
It seems that the media controller appears aligned with the bottom of my video view, but ever horitzontally centered !!!
I had a look at a lot of stack overflow "solutions" (more or less good) or other site samples and I can't understand ...
Can anybody help me please ?
Everyone. I would like to videoview displays the video in full screen, I've tried using some commands, but they didn't work out. I need some help. I put below my code.
The video continues showing about 1/3 of screen! I'm a newbie!!!
Intro.JAVA
public class Intro extends Activity {
public void onCreate(Bundle b) {
super.onCreate(b);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.intro);
VideoView vid = (VideoView) findViewById(R.id.videoView1);
String uriPath = "android.resource://com.english/raw/videoenglish";
Uri uri = Uri.parse(uriPath);
vid.setVideoURI(uri);
vid.start();
vid.requestFocus();
vid.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Intent main = new Intent(Intro.this, IntentsDemoActivity.class);
Intro.this.startActivity(main);
Intro.this.finish();
}
});
}
}
INTRO.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" >
<VideoView
android:id="#+id/videoView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="center" />
</LinearLayout>
Usually, the actual video will not fill the screen a lot of the time, because the aspect ratio of the video does not match the aspect ratio of the screen itself.
I suspect that the VideoView itself is filling up the whole screen. Try with a different device or the emulator.
Here are some other questions for reference either way:
Reference 1
Reference 2