Sunday 9 October 2016

How To Play Local Video in VideoView Android Studio-Java

Learn from this tutorial how to Play mp4 Movie using VideoView in Android Studio version 1.4.
Create New Project with Application name “Play Video“; Minimum SDK – API 9: Android 2.3 (Gingerbread).
Create New Directory in app -> res with name “raw“.
Copy a video you want to display, in new created folder, raw; I used in this example a video with name “movie.mp4“. You can download it from here.
In file activity_main.xml, create a VideoView to display your video.
<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
In file activity_main.xml, create a transparent Button to play again video, on click.
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@android:color/transparent" />
Complete code in MainActivity.java (in onCreate).
Button buttonPlayVideo2 = (Button)findViewById(R.id.button1);
getWindow().setFormat(PixelFormat.UNKNOWN);
//displays a video file
VideoView mVideoView2 = (VideoView)findViewById(R.id.videoView1);
String uriPath2 = "android.resource://com.example.toyo.playvideo/"+R.raw.movie;
Uri uri2 = Uri.parse(uriPath2);
mVideoView2.setVideoURI(uri2);
mVideoView2.requestFocus();
mVideoView2.start();
buttonPlayVideo2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
VideoView mVideoView2 = (VideoView) findViewById(R.id.videoView1);
// VideoView mVideoView = new VideoView(this);
String uriPath = "android.resource://com.example.toyo.playvideo/" + R.raw.movie;
Uri uri2 = Uri.parse(uriPath);
mVideoView2.setVideoURI(uri2);
mVideoView2.requestFocus();
mVideoView2.start();
}
});
how to Play Video in Android Studio 1.4
Run the app in Emulator.
Enjoy the result!
Attention: the app is working only with Video with extension mp4. If you have an .avi movie or other extension, convert it on mp4 and add video to Android project.
Watch the video tutorial to Play Video in Android Studio version 1.4:
Download app PlayVideo.apk from here and install it on your device to play video.
For further questions leave a message.