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(); } }
When I copy the code and run, it says that raw cannot be resolved or is not a field. What is this?
ReplyDeleteYou 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
DeleteSimply 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
ReplyDeleteThis 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)
DeleteThis comment has been removed by a blog administrator.
ReplyDeletei can find activity main in
ReplyDeleteprivate synchronized void jumpMain() {
-> Intent intent = new Intent(ActivitySplash.this, ActivityMain.class);
startActivity(intent);
finish();
}
how i can create that?
I love you Sherif!!!! You're the best!!!! :D mwahhh
ReplyDeleteI love you too, Ahmad. :D hehe
Delete"The method setOnCompletionListener(MediaPlayer.OnCompletionListener) in the type VideoView is not applicable for the arguments (new OnCompletionListener(){})"
ReplyDeletei found this error what should i do to encounter it?
Make sure you are using MediaPlayer.OnCompletionListener.
DeleteI 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
ReplyDeleteewr
ReplyDeleteThank 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.
ReplyDeleteIn 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!).
I actually did this for demonstration so, for sure, you need to do your own implementation. Thank you. Good job
DeleteI created a new class with your Code and added a mp4 into the raw folder but my app chrashes all the time :(
ReplyDeleteOh sry, now it works fine, I've forgot the activity entry in the manifest.
Deletehow to show videos in list view from raw folder and how to display them?
ReplyDeleteThanku in advance...
This comment has been removed by the author.
ReplyDeleteVideoView video_view = (VideoView) findViewById(R.id.video_view);
ReplyDeleteUri 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
You're a legend my brother!!
ReplyDeleteUri uri = Uri.parse("android.resource://" +getPackageName()+"/"+R.raw.video1 );
ReplyDeletein this case getPackageName() highlight in red and says the symbol cannot be resolved
You need a Context instance to do this. So if you are in a fragment for example you can use getActivity().getPackageName() instead
DeleteEngineering mathematics 1
ReplyDeleteVTU 2nd SEM syllabus
Basic Electronics Engineering Tuition Classes in Vijayanagar, Bangalore
Basic electrical engineering Tuition classes for B.tech students
Best Java Training in Bangalore
Java Training institute in Bangalore
Best Java Training institute in Bangalore
Java Training institutes in Bangalore
Best Java Training institutes in Bangalore
Internet Business Ideas for 2020
ReplyDeleteBenefits Of Micro Switch and More About the China Micro Switch Company
ReplyDeleteonline student management system
ReplyDeleteschool software
school management erp
e learning tools for education
online classrooms for teachers
lms online school
Web Development Company in chennai
ReplyDeleteXmedia Solution About us
Xmedia Solution infrastructure
Xmedia Solution Career
Xmedia Solution Contact us
Thank you for sharing.
ReplyDeleteCorporate Excel Training in Mumbai
Advanced Excel Corporate Training in Hyderabad
Excel Corporate Training in Pune
Corporate Excel Training in Dubai
Excel Corporate Training in Abu Dhabi
Corporate Excel Training in Doha
Excel Corporate Training in Muscat
Corporate Excel Training in Riyadh
maintenance automation in usa
ReplyDeleteemail and task management in usa
project management automation in usa
b2b web development in usa
retail process automation in usa
email and task management in usa
ReplyDeleteproject management automation in usa
b2b web development in usa
retail process automation in usa
industries IT services in usa
ecommerce service company in usa
ReplyDeleteshopify website development in usa
prestashop development in usa
magento development in usa
oscommerce development in usa
Buy marathon og online
ReplyDeleteBuy dark star strain online
Buy marijuana flowers online
Buy marijuana edibles online
Buy bc big Bud strain leafly
Buy vapes & catridges
Buy psychedelics online
Buy white ice moon rock