I am hiding the VideoView
initially and when the video is loaded I am showing the VideoView
. But onPrepared
is never called if the VideoView
is invisible
initially. However onPrepared
is called properly if VideoView
is visible. Is there any way to hide the videoView
until video is loaded. Any help would be appreciated. Thanks!
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <VideoView android:id="@+id/video" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:visibility="invisible" /> </RelativeLayout>
MainActivity.java
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.video); Uri videoUri = Uri.parse(url); videoView.setVideoURI(videoUri); videoView.requestFocus(); videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { Toast.makeText(mActivity, "on prepared", Toast.LENGTH_SHORT).show(); videoView.setVisibility(View.VISIBLE); } }); }
android.widget.VideoView. Displays a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.
Use VideoView. start() to start the video playback, once the video is available for playing. Use VideoView. stopPlayback() to stop the video playback and release the resources the VideoView is using.
You could try setting alpha channel of video view to 0 or close to 0.
Solved it making its layout invisible instead of videoView itself. Thanks to @J.Kowalski .
Layout:
<FrameLayout android:id="@+id/layout_video_view" android:layout_width="match_parent" android:layout_height="wrap_content"> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="false" android:scrollbars="none"/> </FrameLayout> <ProgressBar android:id="@+id/progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" />
Set OnPreparedListener:
videoView.setOnPreparedListener(this);
Show progress bar before ready:
@Override public void showProgress() { progress.setVisibility(View.VISIBLE); layoutVideoView.setVisibility(View.INVISIBLE); }
Load video URI:
@Override public void loadVideo(Uri uri) { videoView.setVideoURI(uri); }
When its ready, onPrepared is called:
@Override public void onPrepared(MediaPlayer mp) { Log.d("debug", "onPrepared"); iStepPreviewPresenter.onVideoViewReady(); }
Finally show layout and start video:
@Override public void hideProgress() { progress.setVisibility(View.INVISIBLE); layoutVideoView.setVisibility(View.VISIBLE); } @Override public void startVideo() { videoView.start(); }
Nailed it!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With