title: How to play live video streaming from remote url in android?
body: <p>I have create a one function which triggers a notification service at particular time interval.Notification can be of text,image or video...</p>
  
  <p>Now,for video i download it first and then play it which takes more time...so is there any mechanism that i can play the video directly from remote url ???</p>
  
  <p>Please help me ... 
  i desperately need the answer asap...</p>
  
  <p>Thanks in Advance ........</p>
  
  <p>Have a look at my code snippets ::</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.notificationvideo);
  
      mVideoView = (VideoView) findViewById(R.id.video);
      //pd=ProgressDialog.show(this, "Loading...", "Please Wait...",true,false);
      playVideo();
      //pd.dismiss();
  
      img_back = (ImageView) findViewById(R.id.img_back); 
      img_back.setOnClickListener(new View.OnClickListener() 
       {          
          public void onClick(View v) 
          {
                  Intent int_back=new Intent(NotificationsVideoActivity.this,MyChannelsActivity.class);
                  startActivity(int_back);
                  finish();
          }               
      });  
  }
  
  private void playVideo() 
  {
      try {
           path = getIntent().getStringExtra("url");
  
          Log.v(TAG, "path: " + path);
          if (path == null || path.length() == 0) {
              Toast.makeText(NotificationsVideoActivity.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) &amp;&amp; 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", "mp4");
          temp.deleteOnExit();
          String tempPath = temp.getAbsolutePath();
          FileOutputStream out = new FileOutputStream(temp);
          byte buf[] = new byte[128];
          //byte buf[] = new byte[8192];
  
          do 
          {
              int numread = stream.read(buf);
              if (numread &lt;= 0)
                  break;
              out.write(buf, 0, numread);
          } while (true);
  
          try 
          {
              stream.close();
          }
          catch (IOException ex) 
          {
              Log.e(TAG, "error: " + ex.getMessage(), ex);
          }
          return tempPath;
  
      }
  }
  </code></pre>