How to add Video in Android App Splash Screen
Splash screen is shown when you app starts upon clicking of app icon. Splash screen is generally 2–4 sec long. We had earlier added tutorial in which a logo is shown in splash screen.
This screen is basically shown for your brand promotion by showing some brand related icon. Google uses this in many of its products like YouTube, Google Maps.
Implementation Details
In this tutorial we will be using VideoView to play video in our activity. 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.
So here we will be creating 2 activities. One is Splash activity which is shown until video is being played. Other is MainActivity which is shown when Splash activity is completed.
Following is code for SplashActivity.java
public class SplashActivity extends AppCompatActivity {
VideoView videoView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
videoView = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hello);
videoView.setVideoURI(video);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
startNextActivity();
}
});
videoView.start();
}
private void startNextActivity() {
if (isFinishing())
return;
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
Here we have taken a video file in raw folder in resources. We can reference raw folder content using R.raw.file_name . Now we have parsed a uri using raw resource reference and video url on VideoView.
Also added onComplete() listener so that we get call when video is complete. In this call we are finishing SplashActivity and starting MainActivity.
Full code is available on github. You can also check this tutorial on my blog.