Android media controller shows display for a short time
This below activity works fine but the mediaController display only if I click on the screen. And the second problem is the media controller display only for 3 sec. what should I do to 开发者_如何学运维remove this problem?
public class PlayingActivity extends Activity
{
private VideoView mVideoView;
private EditText mPath;
MediaController mediaController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playingactivity);
mPath = (EditText) findViewById(R.id.path);
mPath.setText(GlobalVariable.getstrEmail());
mVideoView = (VideoView) findViewById(R.id.surface_view);
Uri uri = Uri.parse("/sdcard/download/test.mp3");
mediaController = new MediaController(this);
mediaController.findFocus();
mediaController.setEnabled(true);
mediaController.show(0);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoURI(uri);
mVideoView.start();
}
}
mediaController.requestFocus();
will make it display as soon as the video starts ( without requiring the click)
and
mVideoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaController.show(0);
}
});
Will keep it on the screen. Hope it helps
Requesting focus or specifying 0 in show method never worked for me.
The problem is that MediaController class has default timeout of 3000ms or 3seconds. And its show() method replaces our given parameter to its default parameter. Its a stupid bug resulting from untested code at Google.
We need to implement a lousy workaround of replacing the default value by desired value.
Try the below code. It should work.
mediaControls = new MediaController(getActivity()){
@Override
public void show (int timeout){
if(timeout == 3000) timeout = 20000; //Set to desired number
super.show(timeout);
}
};
mVideoView.setMediaController(mediaControls);
Neo's suggestions are perfect. But I would like to add "mp.start()" to onPrepared(MediaPlayer mp) method, without which the media file won't start playing.
There are two main problem in MediaController:
- auto hide is 3s by default
- Tapping on the video shows/hide the control bar
For the first part it's easly fixed changing the default timeout value of start to zero (zero means indefinite,it is used internally as the video starts) like this:
mediaController = new MediaController(this){
@Override
public void show() {
super.show(0);//Default no auto hide timeout
}
};
The second problem is a little tricky because the click handler is declared as private and final so we do not have any control on that. My solution is to use another function to set visibility and disable the hide function like this:
mediaController = new MediaController(this){
@Override
public void show() {
super.show(0);//Default no auto hide timeout
}
@Override
public void hide() {
//DOES NOTHING
}
void setVisible(boolean visible){//USE THIS FUNCTION INSTEAD
if(visible)
super.show();
else
super.hide();
}
};
You can also add a variable to re-enable standard functionality if visibility is set to false like so:
mediaController = new MediaController(this){
private boolean forceVisible=false;
@Override
public void show() {
super.show(0);//Default no auto hide timeout
}
@Override
public void hide() {
if(!forceVisible)super.hide();
}
void setVisible(boolean visible){
forceVisible=visible;
if(visible)
super.show();
else
super.hide();
}
};
精彩评论