How to add Video in android splash screen ?
In this tutorial we will be adding VideoView in our android apps 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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.
This is all we need to for adding Video in Splash Screen. Please comment for more clarification.