MediaController always show on Android - android

I am using mediacontroller in my app, but it shows only for 3 seconds. I have searched a lot, but in every document I see only the show function, set time out, but it has no effect. How can I always show mediacontroller?
I have tested show(0), but it had no effect.

You can extend the MediaController class and programmatically set an instance of it to a VideoView class:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.MediaController;
public class MyMediaController extends MediaController {
public MyMediaController(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyMediaController(Context context, boolean useFastForward) {
super(context, useFastForward);
}
public MyMediaController(Context context) {
super(context);
}
#Override
public void show(int timeout) {
super.show(0);
}
}
Here's the usage:
VideoView myVideoView = (VideoView) findViewById(R.id.my_video_view);
MediaController mc = new MyMediaController(myVideoView.getContext());
mc.setMediaPlayer(myVideoView);
myVideoView.setMediaController(mc);

You can create anonymous class inline and override certain methods. You need to override the hide method and do nothing in there. You also need to override the dispatchKeyEvent method to check for back key press and call the super.hide(). Otherwise on back press the controller wont hide and the activity cannot be closed.
mediaController = new MediaController(this){
#Override
public void hide() {
// TODO Auto-generated method stub
//do nothing
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
super.hide();
Activity a = (Activity)getContext();
a.finish();
}
return true;
}
};

You can also create an anonymous class inline and override the hide method there instead of having to create a whole new class for it:
mediaController = new MediaController(this) {
#Override
public void hide() {
//Do not hide.
}
};

Try the show method in this way:
new media controller().show(50000);
And also check http://developer.android.com/reference/android/widget/MediaController.html#show().
SudeepSR: Please make a note of that, if you called show(0), it will show the Media Controller until hide() is called.

After trying all that I could, the following code worked for me!
mVideoView = (VideoView) findViewById(R.id.video);
mMediaController = new MediaController(this) {
//for not hiding
#Override
public void hide() {}
//for 'back' key action
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Activity a = (Activity)getContext();
a.finish();
}
return true;
}
};
mMediaController.setAnchorView(mVideoView);
mMediaController.setMediaPlayer(mVideoView);
mVideoView.setMediaController(mMediaController);
mMediaController.requestFocus();
//only this showed the controller for me!!
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mMediaController.show(900000000);
}
});
//finish after playing
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
finish();
}
});

What you need to do is, overrride the hide method in the custom controller and do nothing.
public class MyMediaController extends MediaController {
..
#Override
public void hide() {
// Do nothing here in order to always show
}
...
}
PS: You still need to click on the video to show the media controller.

This may be an old thread, but still unanswered, try this :
final MediaController mediaController = new MediaController(this);
mediaController.setAlwaysDrawnWithCacheEnabled(true);
mediaController.setAnchorView(vView);
mediaController.requestFocus();
vView.setOnPreparedListener( new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaController.show( 0 );
}
});
vView.setVideoPath(Preview_Path);
vView.setMediaController(mediaController);
vView.start();
theres a comment inside the MediaController Class "show" method
**Use 0 to show
* the controller until hide() is called**
so using 900000 or larger value wont help.
hope it helps you.
cheers.

Try this:
videoView.setOnCompletionListener(onVideoCompleted);
videoView.setOnPreparedListener(onVideoPrepared);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
MediaPlayer.OnPreparedListener onVideoPrepared = new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mc.show(0);
}
};
MediaPlayer.OnCompletionListener onVideoCompleted = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mc.hide();
}
};

I wanted to fade the controller for videos and always show it for audio. This worked
mController = new MediaController(this) {
#Override
public void hide() {
if (mType != TYPE_AUDIO) super.hide();
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
mController.hide();
Activity a = (Activity)getContext();
a.finish();
return true;
}
return false;
}
};
In MediaPlayer.onPrepared I added:
if (mType == TYPE_AUDIO) mController.show(0);
This causes the controller to show at the start of audio playback, but not video playback.
The other phone control buttons continue to work as normal.

Easy! Set visibility "GONE" in event hide and set visibility "VISIBLE" in show!
MediaController mc= new MediaController(zoom.this){
#Override
public void setMediaPlayer(MediaPlayerControl player) {
super.setMediaPlayer(player);
this.show(4000);
}
#Override
public void show(int timeout) {
super.show(timeout);
this.setVisibility(View.VISIBLE);
}
//instead of press twice with press once "back" button to back
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Activity a = (Activity)getContext();
a.finish();
}
return true;
}
#Override
public void hide() {
// TODO Auto-generated method stub
super.hide();
this.setVisibility(View.GONE);
//super.show(3000);
}
};

Related

Hide and Show Toolbar on MediaController show() and hide()

I'm using a MediaController to control video play back for my VideoView. I've overriden VideoView.setOnPreparedListener so that the ActionBar/Toolbar is hidden (hide()) after the video first finishes buffering. And I'd like the toolbar to comeback when the MediaController does. I've tried overriding the MediaController show() and hide() methods, like so:
mVideoView.start();
// Media Controller
mMediaController = new MediaController(this){
#Override
public void show() {
getSupportActionBar().show();
}
#Override
public void hide() {
getSupportActionBar().hide();
}
};
mMediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mMediaController);
// Hide toolbar once video starts
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// Hide support bar
getSupportActionBar().hide();
}
});
This works, except the playback controls stop showing up! And of course, calling a recursive mMediaController.show() within the overridden method doesn't work... Can I have my cake an eat it too?
Edit
So I've also, unsucessfully tried taking advantage of the VideoView.setOnTouchListener and VideoView.setOnCompletetionListener:
mVideoView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
getSupportActionBar().show();
return false;
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
getSupportActionBar().hide();
}
});
It does show, but it won't hide again, perhaps I misinterpret OnCompletetionListener?
Edit 2
From Amir's suggestion, I override the onTouch for VideoView, not perfect, but it's on like the right track:
mMediaController = new MediaController(this);
mVideoView = (VideoView) findViewById(R.id.media_player);
mVideoView.setOnTouchListener(new View.OnTouchListener() {
boolean flag;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if(flag) {
mMediaController.hide();
getSupportActionBar().hide();
} else {
mMediaController.show();
getSupportActionBar().show();
}
flag = !flag;
return true;
}
return false;
}
});
This mostly works, it toggles the Toolbar and so sometimes the Toolbar will appear without the MediaController, and each time I toggle it, the MediaController does it's regular 'appear for a few seconds and then disappear.' In any case, it is a working solution.
Your original code seems to work if you add calls to the corresponding super methods.
mediaController = new MediaController(this){
#Override
public void show() {
super.show();
getSupportActionBar().show();
Toast.makeText(activity_media_player.this, "SHOW TOOLBAR", Toast.LENGTH_SHORT).show();
}
#Override
public void hide() {
super.hide();
getSupportActionBar().hide();
Toast.makeText(activity_media_player.this, "HIDE TOOLBAR", Toast.LENGTH_SHORT).show();
}
};
You should change your OnTouchListener a bit. I do following in my project and works fine:
final MediaController mediaController = new MediaController(this);
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setOnTouchListener(new View.OnTouchListener() {
boolean flag = true;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
if (flag) {
mediaController.hide();
hideToolbar();
}
else {
mediaController.show(0);
showToolbar();
}
flag = !flag;
return true;
}
return false;
}
});
videoView.setMediaController(mediaController);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
}
});
videoView.setVideoURI(Uri.parse("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"));
And hideToolbar() with some traslate animation:
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
showToolbar():
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
Also if you need to HideStatusBar call setUiFlag() with true:
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setUiFlags(boolean fullscreen) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
View decorView = getWindow().getDecorView();
if (decorView != null) {
decorView.setSystemUiVisibility(fullscreen ? getFullscreenUiFlags() : View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private int getFullscreenUiFlags() {
int flags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
}
return flags;
}

How to show/hide play/pause Button in video view in android?

I am doing an Android project based on Video view. I want to show play button before the user clicks play, when user decides to pause video - pause button shows up. Clicking on pause button should trigger playing the video again from the same place where it was paused (like YouTube video).
im1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vv1.setVideoURI(Uri.parse("android.resource://com.example.cm.filmfestival/" + R.raw.mission));
im1.setVisibility(View.INVISIBLE);
im2.setVisibility(View.INVISIBLE);
vv1.start();
}
});
im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vv1.stopPlayback();
im2.setVisibility(View.VISIBLE);
im1.setVisibility(View.INVISIBLE);
}
});
#Override
public boolean onTouch(View v, MotionEvent event) {
im1.setVisibility(View.VISIBLE);
vv1.start();
im2.setVisibility(View.VISIBLE);
vv1.stopPlayback();
return true;
}
Use the code below
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
public VideoView myVideoView;
private int position = 0;
private MediaController mediaControls;
// set the media controller buttons
if (mediaControls == null)
{
mediaControls = new MediaController(MainActivity.this);
}
// initialize the VideoView
myVideoView = (VideoView) findViewById(R.id.video_view);
try
{
// set the media controller in the VideoView
myVideoView.setMediaController(mediaControls);
// set the uri of the video to be played
myVideoView.setVideoURI(Uri.parse("your UrI"));
} catch (Exception e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
// we also set an setOnPreparedListener in order to know when the video
// file is ready for playback
myVideoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mediaPlayer)
{
// if we have a position on savedInstanceState, the video
// playback should start from here
myVideoView.seekTo(position);
System.out.println("vidio is ready for playing");
if (position == 0)
{
myVideoView.start();
} else
{
// if we come from a resumed activity, video playback will
// be paused
myVideoView.pause();
}
}
});
Set an OntouchListener on your VideoView and then in Ontouch callback check if the video is playing or paused before pausing or playing it.
private boolean stopped = false;
private ImageView postVideoPlaypauseIcon;
//set it to any play/pause icon
postVideoPlaypauseIcon = mView.findViewById(R.id.playpause_icon);
private int stopPosition;
Add above code.
then, add CustomVideoView class, make VideoView extend it and create an object video and use findviewbyid
public class CustomVideoView extends VideoView {
private PlayPauseListener mListener;
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setPlayPauseListener(PlayPauseListener listener) {
mListener = listener;
}
#Override
public void pause() {
super.pause();
if (mListener != null) {
mListener.onPause();
}
}
#Override
public void start() {
super.start();
if (mListener != null) {
mListener.onPlay();
}
}
public static interface PlayPauseListener {
void onPlay();
void onPause();
}
}
replace xml for VideoView with below code
`<package-name.CustomVideoView
android:id="#+id/custom_videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>`
Finally,
add setOnTouchListener for listening to touch.
video.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(stopped == false){
stopPosition = postVideo.getCurrentPosition();
video.pause();
stopped = true;
} else if(stopped == true){
video.seekTo(stopPosition);
video.start();
stopped = false;
}
Log.e("TAP","from video ");
return false;
}
});
add setPlayPauseListener on your videoView object
video.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {
#Override
public void onPlay() {
System.out.println("Play!");
videoPlaypauseIcon.setVisibility(View.VISIBLE);
}
#Override
public void onPause() {
System.out.println("Pause!");
videoPlaypauseIcon.setVisibility(View.INVISIBLE);
}
});
used this for reference

Adding additional methods to MediaController buttons

I have a VideoView which plays a video and I call
videoPlayer.setMediaController(new MediaController(this));
to show the media controller buttons (play, pause, forward, backward).
The question is how am I able to add additional methods when I click the play or pause button of the MediaController?
Here's my class:
public class VideoPlayer extends Activity implements OnCompletionListener {
private String filename;
private VideoView videoPlayer;
private MediaPlayer mediaplayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.video_player);
System.gc();
Intent i = getIntent();
Bundle extras = i.getExtras();
filename = extras.getString("video_url");
System.out.println(filename);
videoPlayer = (VideoView) findViewById(R.id.videoPlayer);
videoPlayer.setOnCompletionListener(this);
videoPlayer.setKeepScreenOn(true);
videoPlayer.setVideoPath(filename);
videoPlayer.setMediaController(new MediaController(this));
videoPlayer.requestFocus();
videoPlayer.start();
mediaplayer = new MediaPlayer();
mediaplayer = MediaPlayer.create(
this,
Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.alo));
mediaplayer.start();
}
/** This callback will be invoked when the file is finished playing */
#Override
public void onCompletion(MediaPlayer mp) {
// Statements to be executed when the video finishes.
this.finish();
}
/** Use screen touches to toggle the video between playing and paused. */
#Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
if (videoPlayer.isPlaying()) {
videoPlayer.pause();
mediaplayer.pause();
} else {
videoPlayer.start();
mediaplayer.start();
}
return true;
} else {
return false;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I'm not sure what you are trying to achieve.
You could extend a class with MediaController and implement some methods of your own. Basic OO principles.
private class MyMediaController extends MediaController {
#Override
public boolean onTouchEvent(MotionEvent event) {
// Your logic here
return super.onTouchEvent(event);
}
}

Make MediaController show without hide

I try to use MediaController to play music.
I want the MediaController appear until the "back" button is pressed.
Now I have try below code:
MediaController mediaController = new MediaController(this){
#Override
public void setMediaPlayer(MediaPlayerControl player) {
super.setMediaPlayer(player);
this.show();
}
#Override
public void show(int timeout) {
super.show(0);
}
//instead of press twice with press once "back" button to back
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Activity a = (Activity)getContext();
a.finish();
}
return true;
}
};
But it still one trouble while the MediaController visible.
When the MediaController appear touch the screen, the MediaController will hide.
I also already try below code:
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("screen","touch");
return true;
}
But it did not work.
The string did not show in Logcat.
Anyone has idea to do it?
Override this method also inside media controller
#Override
public void hide() {
// TODO Auto-generated method stub
super.show();
}
If you want to keep the hide() method but not have the controller disappearing every time a control is used :
this.mediaController = new MediaController(this){
#Override
public void show(int timeout) {
super.show(0);
}
};

Problem with back button in VideoView

I am having difficulty getting the back button to actually finish my activity when pressed. I am running a very simple videoview, using a progressdialog to show loading dialog and onpreparedlistener, etc etc. simple stuff. Anyways, currently when I press the back button, it will just cancel the progressdialog, and leave a black screen, and pressed again, the progressdialog restarts!!! and then when I click the back button again, it displays an alert dialog, "video cannot be played." very annoying. Thanks for your help.
public class VideoActivity extends Activity {
private VideoView mVideoView;
private static ProgressDialog progressdialog;
private String path;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
progressdialog = ProgressDialog.show(this, "", " Video Loading...", true);
progressdialog.setCancelable(true);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setMediaController(new MediaController(this));
Bundle b = this.getIntent().getExtras();
path = b.getString("path");
mVideoView.setVideoURI(Uri.parse(path));
mVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressdialog.dismiss();
mVideoView.requestFocus();
mVideoView.start();
}
});
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
super.finish();
}
}
You can simply write: (No need to create new class for MediaController)
mVideoView.setMediaController(new MediaController(this){
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
((Activity) getContext()).finish();
return super.dispatchKeyEvent(event);
}
});
You'll want to create a custom MediaController class and override the dispatchKeyEvent function to capture the back KeyEvent and tell the activity to finish.
See Android back button and MediaController for more info.
public class CustomMediaController extends MediaController {
public CustomMediaController(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomMediaController(Context context, boolean useFastForward) {
super(context, useFastForward);
}
public CustomMediaController(Context context) {
super(context, true);
}
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
((Activity) getContext()).finish();
return super.dispatchKeyEvent(event);
}
}
From CommansWare
Based on the source code, this should work:
Extend MediaController (for the purposes of this answer, call it
RonnieMediaController)
Override dispatchKeyEvent() in RonnieMediaController
Before chaining to the superclass, check for KeyEvent.KEYCODE_BACK,
and if that is encountered, tell your activity to finish()
Use RonnieMediaController instead of MediaController with your
VideoView
Personally, I'd just leave it alone, as with this change your user
cannot make a RonnieMediaController disappear on demand.
Here is the link to the original post.
finish() doesn't kill your activity, it just signals to Android that it doesn't need to run the Activity anymore.
I remember solving this by putting "return" in proper places.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
System.exit(0);
}
return false;
}

Categories

Resources