Video as a Splash Screen instead of Picture

前端 未结 5 1533
天命终不由人
天命终不由人 2020-12-13 15:43

I am doing the Android Programming Tutorial on Splash Screens where you show a picture or text for 5 Seconds than it goes to the Main Application. My Question is..Instead of

相关标签:
5条回答
  • 2020-12-13 16:00

    1) Create SplashScreen.java class.

    2) Create a raw folder inside res directory(res/raw).

    3) Paste your mp4 video file in this raw folder(if you don't have any sample mp4, you can download from the below link). http://www.mediafire.com/download/p05ki89i2dt5x2x/splash.mp4

    4) Then add the following code in your SplashScreen.java class.

    public class SplashScreenActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            VideoView videoHolder = new VideoView(this);
            setContentView(videoHolder);
            Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash);
            videoHolder.setVideoURI(video);
    
            videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    jump();
                }
            });
            videoHolder.start();
        } catch (Exception ex) {
            jump();
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        jump();
        return true;
    }
    
    private void jump() {
        if (isFinishing())
            return;
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
    

    }

    Note: splash_activity.xml is not required.

    0 讨论(0)
  • 2020-12-13 16:00

    Here is the code for adding video. In case you need to add controls on video like pause or seek etc. you can add them with:

    vv.setMediaController(new MediaController(this));
    

    Rest of the code:

    VideoView vv;
    
    @Override
    
    protected void onCreate(Bundle savedInstanceState)
    
    {
    
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_splash);
    
        vv=(VideoView)findViewById(R.id.videoView);
        Uri path=Uri.parse("android:resource://"+getPackageName()+"/"+R.raw.hello);
        vv.setVideoURI(path);
        vv.setMediaController(new MediaController(this));
    
       vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
       {
           @Override
           public void onCompletion(MediaPlayer mp) {
               Intent in=new Intent(splash.this,MainActivity.class);
            startActivity(in);
               finish();
    
           }
       });
        vv.start();
    
    0 讨论(0)
  • 2020-12-13 16:04

    I hope this will help you. You just create a simple VideoView for create a splash screen for the video.

    Checkout the source code hear and simple steps what is the best practice to create a splash screen

    0 讨论(0)
  • 2020-12-13 16:09

    Use a MediaPlayer along with a VideoView. You can then "listen" for when the video playback is done, by setting an OnCompletionListener on your MediaPlayer.

    See here: http://developer.android.com/reference/android/media/MediaPlayer.html And here: http://developer.android.com/reference/android/widget/VideoView.html

    Also, pay special attention to the state diagram on the MediaPlayer reference page. It can be a bit tricky and has been known to trip a few people up.

    0 讨论(0)
  • 2020-12-13 16:22
    imgAnim=(VideoView)findViewById(R.id.animimage);
    
    String uriPath = "android.resource://com.petnvet/" + R.drawable.vidio;
    Uri uri = Uri.parse(uriPath);
    imgAnim.setVideoURI(uri);
    imgAnim.requestFocus();
    imgAnim.start();
    //  imgAnim.setVideoPath("android.resource://com.myapplication/" + R.drawable.vidio);
    int SPLASH_DISPLAY_LENGTH = 3000;
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent mainIntent = new Intent(SplashScreen.this, Login.class);
            startActivity(mainIntent);
            finish();
        }
    }, SPLASH_DISPLAY_LENGTH);
    
    0 讨论(0)
提交回复
热议问题