How to play a video from resources

As am getting lots of good feedback on my answer on SO for the question How to play a video from the raw or assets folder, I thought of writing this small tutorial on playing a video in your activity.

So, say you want to create a splash activity that will show an mp4 video that you stored in your raw directory which is usually located in your projects's res. Usually, the directory raw is not there, so just create one there. Add your splash video to the raw folder: In my case, it is splash.mp4

In your splash activity create the function splashPlayer():

public void splashPlayer() {
 VideoView videoHolder = new VideoView(this);
 setContentView(videoHolder);
 Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
    + R.raw.splash);
 videoHolder.setVideoURI(video);
 videoHolder.setOnCompletionListener(new OnCompletionListener() {
  public void onCompletion(MediaPlayer mp) {
   jumpMain(); //jump to the next Activity
  }
 });
 videoHolder.start();
}

In the previous code the function jumpMain() will probably jump you to the next activity. If you want to give the user the option to cancel your video/splash, add the following line to the end of splashPlayer():

videoHolder.setOnTouchListener(new OnTouchListener() {
 @Override
 public boolean onTouch(View v, MotionEvent event) {
  ((VideoView)v).stopPlayback();
  jumpMain();
  return true;
 }
});

Finally, in your onCreate() function call this splashPlayer():

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 splashPlayer();
}

An extra step would be to surround the splashPlayer() call in your onCreate() with a try...catch block. My personal experience shows that not all videos are playable on all devices even if they are mp4 videos. If this is the case, an exception is thrown and you can for example show an image instead. In this example I will only jump to the next Activity by calling jumpMain(), so my onCreate looks something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 try {
  splashPlayer();
 } catch(Exception ex) {
  jumpMain();
 }
}

Do not forget to call finish() in your jumpMain() and enjoy!

 Here is the full SplashActivity (I am just showing the general idea so feel free to notify me of errors or bugs)
public class ActivitySplash extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  try {
   splashPlayer();
  } catch (Exception ex) {
   jumpMain();
  }
 }

 @Override
 public boolean onTouchEvent(MotionEvent ev) {
  return false;
 }

 public void splashPlayer() {
  VideoView videoHolder = new VideoView(this);
  setContentView(videoHolder);
  Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
    + R.raw.splash);
  videoHolder.setVideoURI(video);
  videoHolder.setOnCompletionListener(new OnCompletionListener() {
   public void onCompletion(MediaPlayer mp) {
    jumpMain();
   }

  });
  videoHolder.start();
  videoHolder.setOnTouchListener(new OnTouchListener() {

   @Override
   public boolean onTouch(View v, MotionEvent event) {
    ((VideoView) v).stopPlayback();
    jumpMain();
    return true;
   }
  });
 }

 private synchronized void jumpMain() {
  Intent intent = new Intent(ActivitySplash.this, ActivityMain.class);
  startActivity(intent);
  finish();
 }
}

22 comments:

  1. When I copy the code and run, it says that raw cannot be resolved or is not a field. What is this?

    ReplyDelete
    Replies
    1. You must create a folder called 'raw' in your folder 'res' and add your video to this folder. In case you already did so, just clean your project and it should be there

      Delete
  2. Simply copying above code I found message like the video can not be played. Resource file in raw folder also. I also updated to api level 8

    ReplyDelete
    Replies
    1. This could happen when the file format is not supported by the device. I usually try catch and show a picture (when this is just a splash video)

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. i can find activity main in

    private synchronized void jumpMain() {
    -> Intent intent = new Intent(ActivitySplash.this, ActivityMain.class);
    startActivity(intent);
    finish();
    }

    how i can create that?

    ReplyDelete
  5. I love you Sherif!!!! You're the best!!!! :D mwahhh

    ReplyDelete
  6. "The method setOnCompletionListener(MediaPlayer.OnCompletionListener) in the type VideoView is not applicable for the arguments (new OnCompletionListener(){})"

    i found this error what should i do to encounter it?

    ReplyDelete
    Replies
    1. Make sure you are using MediaPlayer.OnCompletionListener.

      Delete
  7. I got an error that The process has stopped unexpectedly please try again. I run it many times but getting the same error.And when i mention my video name if i give .mp4 extension it shows an error and if i remove it error disappears. Please help me in solving this and please explain me in some detail

    ReplyDelete
  8. Thank you. I needed to avoid borders, so I created a relative layout with background-color the same as the video. To this I added a VideoView.
    In the constructor I use setContentView(R.id.layout.my_layout), and ind splasPlayer() I use findViewById to get the video-view (don't set at content-view!).

    ReplyDelete
    Replies
    1. I actually did this for demonstration so, for sure, you need to do your own implementation. Thank you. Good job

      Delete
  9. I created a new class with your Code and added a mp4 into the raw folder but my app chrashes all the time :(

    ReplyDelete
    Replies
    1. Oh sry, now it works fine, I've forgot the activity entry in the manifest.

      Delete
  10. how to show videos in list view from raw folder and how to display them?
    Thanku in advance...

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. VideoView video_view = (VideoView) findViewById(R.id.video_view);
    Uri video = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.hello);
    video_view.setVideoURI(video);
    video_view.start();

    in this case .setVideoURI and .start both highlight in red and says the symbol cannot be resolved

    ReplyDelete
  13. Uri uri = Uri.parse("android.resource://" +getPackageName()+"/"+R.raw.video1 );
    in this case getPackageName() highlight in red and says the symbol cannot be resolved

    ReplyDelete
    Replies
    1. You need a Context instance to do this. So if you are in a fragment for example you can use getActivity().getPackageName() instead

      Delete