Pages

Wednesday 14 December 2011

Video Streaming In Android

 Hope this will help you...:-)

public class WatchVideo extends Activity {
    VideoView mVideoView;
    String TAG = "Vikram_Hooda_Blogs";
    String current = "";
    String mPath="";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.watchvideo);
        mVideoView = (VideoView) findViewById(R.id.video_view);
        Intent in = getIntent();
        String path = in.getStringExtra("path");
        playVideo(path);
    }
   
     private void playVideo(String path)
     {
                     try {  
                    
                         Log.v(TAG, "path: " + path);
                         if (path == null || path.length() == 0) {
                             Toast.makeText(WatchVideo.this, "File URL/path is empty",
                                     Toast.LENGTH_LONG).show();
            
                         } else {
                             // If the path has not changed, just start the media player
                             if (path.equals(current) && mVideoView != null) {
                                 mVideoView.start();
                                 mVideoView.requestFocus();
                                 return;
                             }
                             current = path;
                            
                             mVideoView.setVideoPath(getDataSource(path));
                             mVideoView.start();
                             mVideoView.requestFocus();
                            
            
                         }
                    } catch (Exception e) {
                     Log.e(TAG, "error: " + e.getMessage(), e);
                         if (mVideoView != null) {
                             mVideoView.stopPlayback();
                         }
                     }
                 }
           
                 private String getDataSource(String path) throws IOException {
                     if (!URLUtil.isNetworkUrl(path)) {
                         return path;
                     } else {
                         URL url = new URL(path);
                         URLConnection cn = url.openConnection();
                         cn.connect();
                         InputStream stream = cn.getInputStream();
                         if (stream == null)
                             throw new RuntimeException("stream is null");
                         File temp = File.createTempFile("mediaplayertmp", "dat");
                         temp.deleteOnExit();  
                         String tempPath = temp.getAbsolutePath();
                         FileOutputStream out = new FileOutputStream(temp);
                         byte buf[] = new byte[128];
                         do {
                             int numread = stream.read(buf);
                             if (numread <= 0)
                                 break;
                             out.write(buf, 0, numread);
                         } while (true);
                         try {
                             stream.close();
                         } catch (IOException ex) {
                         Log.e(TAG, "error: " + ex.getMessage(), ex);
                     }
                    return tempPath;
                 }
                 }
}








By:
Yours
V.K.Hooda

No comments:

Post a Comment