Android multimedia controller displays display briefly

This action works fine, but the mediaController is only displayed if I click on the screen. The second problem is the display of the media controller for only 3 seconds. 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(); } } 
+6
source share
4 answers
 mediaController.requestFocus(); 

will display it as soon as the video starts (without requiring a click)

and

 mVideoView.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mediaController.show(0); } }); 

Will keep it on the screen. Hope this helps.

+18
source

Neo sentences are perfect. But I would like to add "mp.start ()" to the onPrepared (MediaPlayer mp) method, without which the media file will not start playing.

+1
source

Requesting focus or specifying 0 in the display method never worked for me.

The problem is that the MediaController class has a default timeout of 3000 ms or 3 seconds. And its show () method replaces our given parameter with its default parameter. This is a stupid error caused by Google's untrusted code.

We need to implement a lousy workaround to replace the default value with the desired value.

Try the code below. 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); 
0
source

There are two main issues with MediaController:

  • auto hide - default 3s
  • Clicking on the video shows / hides the control panel

In the first part, it easily captures the change in the default timeout value for starting to zero (zero means indefinite, it is used internally when starting a video) as follows:

 mediaController = new MediaController(this){ @Override public void show() { super.show(0);//Default no auto hide timeout } }; 

The second problem is a bit complicated because the click handler is declared closed and final, so we have no control over this. My solution is to use another function to determine the visibility and disable the hide function as follows:

 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 functions if visibility is set to false:

 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(); } }; 
0
source

Source: https://habr.com/ru/post/891695/


All Articles