Skip to content

Instantly share code, notes, and snippets.

@vishhmakasana
Last active August 10, 2020 14:11
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vishhmakasana/7c17bf7905256070ff70 to your computer and use it in GitHub Desktop.
Save vishhmakasana/7c17bf7905256070ff70 to your computer and use it in GitHub Desktop.
In Android Play mp4 video as a background intro video in your app like latest freelancer app plays it on their intro page of android app.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.example.videointro.IntroVideoView
android:id="@+id/introVideoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
package com.example.videointro;
import java.io.IOException;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class IntroVideoView extends SurfaceView implements
SurfaceHolder.Callback {
private static final String TAG = "INTRO_VIDEO_CALLBACK";
private MediaPlayer mp;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public IntroVideoView(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public IntroVideoView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public IntroVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public IntroVideoView(Context context) {
super(context);
init();
}
private void init() {
mp = new MediaPlayer();
getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.intro); // your intro video file placed in raw folder named as intro.mp4
try {
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getDeclaredLength());
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
android.view.ViewGroup.LayoutParams lp = getLayoutParams();
int screenHeight = getHeight();
int screenWidth = getWidth();
// this plays in full screen video
lp.height = screenHeight;
lp.width = screenWidth;
setLayoutParams(lp);
mp.setDisplay(getHolder());
mp.setLooping(false);
mp.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mp.stop();
}
}
package com.example.videointro;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment