MediaController's default SeekBar not in sync - android

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

Related

In ExoPlayer how do I show the thumbnail of the first frame after loading a media source and seeking to a position instead of it being blank?

How do I show the first frame of the video after the Fragment is resumed instead of the video player being blank?
1.) Add surface_type="texture_view" to PlayerView
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/player_view"
app:surface_type="texture_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2.) Add thumbnail view in front of PlayerView in layout
<View
android:id="#+id/view_thumbnail_background"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black" />
<ImageView
android:id="#+id/image_thumbnail"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3.) Create a method to get the Bitmap from the TextureView
private fun setVideoThumbnail() {
val textureView = playerView.videoSurfaceView as TextureView
val bitmap = textureView.bitmap
thumbnailImage.setImageBitmap(bitmap)
thumbnailBackground.visibility = View.VISIBLE
thumbnailImage.visibility = View.VISIBLE
}
4.) In onPause call
setVideoThumbnail()
// destroy player or not depending
5.) Add an onClick listener to thumbnailImage in onViewCreated
thumbnailImage.setOnClickListener {
// make sure player is re-created and media source is loaded, prepared, and seekTo
player.playWhenReady = true
thumbnailBackground.visibility = View.INVISIBLE
thumbnailImage.visibility = View.INVISIBLE
}

How to resize the frames of video and show it in a fixed size video view in android

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.

Android : Positionning the MediaController with setAnchorView

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 ?

Not displaying videoview in full screen

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

Show video and buttons on same screen in Android

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

Categories

Resources