Playing a video file is simple in android. This post will help you.
Put your video in res/raw folder. If no raw folder present then create a new folder inside res.
package com.coderzheaven.pack; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public class Example extends Activity { /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); showVideo(); } private void showVideo() { VideoView vd = (VideoView)findViewById(R.id.videoview); Uri uri = Uri.parse("android.resource://package/"+R.raw.movie); MediaController mc = new MediaController(this); vd.setMediaController(mc); vd.setVideoURI(uri); vd.start(); } }
The main. xml file.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/widget32" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <VideoView android:id="@+id/videoview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" > </VideoView> </RelativeLayout>
Please leave your comments if this post was useful.
useful code.. thanks!!!
hi this code is really good but when i put any fil e on raw folder the invalid file name error occur pls suggest me to solve this problem
Did you use your package name in the place of “package”.
How to programatically close the player when the video ended.
Hello Dale, please check this http://coderzheaven.com/2012/01/how-to-check-whether-a-video-has-completed-playing-in-android/
Hello James, thank you for reply. Can you help me once more? When I run a video and sliding on page during ten seconds, I get pop up menu ‘Activity MyActivity (in application MyActivity) is not responding’ appears (Android 3.2.1). How to ban the sliding?
@Dale :- I couldn’t understand clearly what you are trying to say.
When I try paging on screen during 10 sec., I get error ‘Activity MyActivity (in application MyActivity) is not responding’. This error shows on Android 3.2.1 (on Android 2.3.3 – all OK). How to freeze the paging on screen?
You can see the stacktrace here – http://stackoverflow.com/questions/8849409/android-video-crash
Pingback: How to check whether a video has completed playing in android? | Coderz Heaven
Hello Sir,
When i run your code so we can only listening music video is not see in my emulator…….
please help me sir
Hello James,
How to play Video in full screen mode?
I have used this for videoview in xml
android:layout_alignParentRight=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:layout_alignParentBottom=”true”
but it doesnt display it in full screen.
Thanks in advance.
Hey,
When i put this code i my application it shows a pop up saying video can not be played.
Plz help
Check whether you video is in supported format or try your project on the device.
Pingback: how to display the videos in listview in youtube player? : Android Community - For Application Development