In this post you can learn android source code example on how to play a video from a URL online from an android device. The basic functionality part is given below. This can be used in android application development for live video streaming from a URL. Using this concept you can develop android apps for online TV viewer application and online video live streaming from a valid URL containing the video.
1. Create a android project and make use of this main.xml file below.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<VideoView
android:id="@+id/video"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center" />
</FrameLayout>
</LinearLayout>
2. Now construct your main activity file and make use of the below code.
MainActivity.java
public class MainActivity extends Activity {
public static String url = "url_of_the_video_that_you_want_to_play";
private VideoView videoView = null;
private ProgressBar progress = null;
private Context ctx = null;
private MediaController mediaController = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
ctx = this;
progress = (ProgressBar) findViewById(R.id.progress);
videoView = (VideoView) findViewById(R.id.video);
Uri video = Uri.parse(url);
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
Toast.makeText(ctx, "Error occured", 500).show();
return false;
}
});
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
progress.setVisibility(View.GONE);
videoView.start();
}
});
}
@Override
protected void onDestroy() {
try {
videoView.stopPlayback();
} catch (Exception e) {
//
}
super.onDestroy();
}
}
3. Add USES permission (Internet Permission) to your android manifest xml file without fail for internet access.
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Note : The video does not play in your android emulator. Just compile your code units perfectly and install the .apk file into your android device and start watching the video from the URL which you have embedded.
No comments:
Post a Comment