Android Video Player Example
By the help of MediaController and VideoView classes, we can play the video files in android.
MediaController class
The android.widget.MediaController is a view that contains media controls like play/pause, previous, next, fast-forward, rewind etc.
VideoView class
The android.widget.VideoView class provides methods to play and control the video player. The commonly used methods of VideoView
class are as follows:
Method Description
public void setMediaController(MediaController controller) sets the media controller to the video view.
public void setVideoURI (Uri uri) sets the URI of the video file.
public void start() starts the video view.
public void stopPlayback() stops the playback.
public void pause() pauses the playback.
public void suspend() suspends the playback.
public void resume() resumes the playback.
public void seekTo(int millis) seeks to specified time in miliseconds.
activity_main.xml
Drag the VideoView from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. tools:context=".MainActivity" >
6.
7. <VideoView
8. android:id="@+id/videoView1"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:layout_alignParentLeft="true"
12. android:layout_centerVertical="true" />
13.
14. </RelativeLayout>
Activity class
Let's write the code of to play the video file. Here, we are going to play 1.mp4 file located inside the sdcard/media directory.
File: MainActivity.java
1. package com.example.video1;
2.
3. import android.net.Uri;
4. import android.os.Bundle;
5. import android.app.Activity;
6. import android.view.Menu;
7. import android.widget.MediaController;
8. import android.widget.VideoView;
9.
10. public class MainActivity extends Activity {
11.
12. @Override
13. protected void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.activity_main);
16.
17. VideoView videoView =(VideoView)findViewById(R.id.videoView1);
18.
19. //Creating MediaController
20. MediaController mediaController= new MediaController(this);
21. mediaController.setAnchorView(videoView);
22.
23. //specify the location of media file
24. Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/media/1.mp4");
25.
26. //Setting MediaController and URI, then starting the videoView
27. videoView.setMediaController(mediaController);
28. videoView.setVideoURI(uri);
29. videoView.requestFocus();
30. videoView.start();
31.
32. }
33.
34. @Override
35. public boolean onCreateOptionsMenu(Menu menu) {
36. // Inflate the menu; this adds items to the action bar if it is present.
37. getMenuInflater().inflate(R.menu.activity_main, menu);
38. return true;
39. }
40.
41. }
Ref:
https://developer.android.com/reference/android/media/MediaRecorder