android: hiding media controller functions
I have a videoview and when the video starts, the media controller is shown for 3 seconds. I want to hide the media controller unless i tap on the screen. I tried
MediaController mc= new MediaController();
mc.hide();
Videoview.setM开发者_Python百科ediaController(mc);
..
..
..
But it didn't work.. Any suggestions please?
This isn't really a solution to hiding the MediaController, but if you want to get rid of the thing altogether, do this:
videoView.setMediaController(null);
You can have it initially hidden by doing the above, and then when you want it to show (onClick or onTouch or whatever), just make a new MediaController and set it on the videoView. I added a boolean to prevent the action from happening more than once.
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
if (controllerCreated == false) {
videoView.setMediaController(mc);
mc.show();
controllerCreated = true;
}
return true;
} else {
return false;
}
}
Specifying videoView.setMediaController(null)
is not necessary.
The problem is you cannot hide the controller till it fully prepared.
Use OnPreparedListener and in the callback onPrepared do your hide inderectly, like:
@Override
public void onPrepared (MediaPlayer mp)
{
int childs = mediaController.getChildCount();
for (int i = 0; i < childs; i++)
{
View child = mediaController.getChildAt (i);
child.setVisibility (View.GONE);
}
}
Unfortunately, this is hardcoded behavior in VideoView
:
...
if (mTargetState == STATE_PLAYING) {
start();
if (mMediaController != null) {
mMediaController.show();
}
...
As a workaround, wrap the MediaController
in your own class and suppress the initial show()
call, like this:
package pkg.your;
import android.content.Context;
import android.widget.MediaController;
public class MyMediaController extends MediaController {
private boolean suppressed = true;
public MyMediaController(Context context) {
super(context);
}
@Override
public void show(int timeout) {
if (! suppressed) {
super.show(timeout);
}
suppressed = false;
}
}
Then, simply hookup the MyMediaController
above as usual, e.g.:
MyMediaController mc = mediaController = new MyMediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
....
Now, the controls are initially hidden, and they show up as expected when the user taps the screen.
精彩评论