2012-03-15 78 views
4

我創建了一個函數,在特定的時間間隔觸發通知服務。通知可以是文本,圖像或視頻...如何從android中的遠程URL播放實時視頻流?

現在,對於視頻,我先下載它然後播放它需要更多時間...所以有什麼機制,我可以直接從遠程網址播放視頻?

請幫我... 我迫切需要儘快答案...

在此先感謝........

在我的代碼片段有一個看看::

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) && 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 <= 0) 
       break; 
      out.write(buf, 0, numread); 
     } while (true); 

     try 
     { 
      stream.close(); 
     } 
     catch (IOException ex) 
     { 
      Log.e(TAG, "error: " + ex.getMessage(), ex); 
     } 
     return tempPath; 

    } 
} 

回答

2

試試這一點 - 在給予

String path="http://www.ted.com/talks/download/video/8584/talk/761"; 
String path1="http://commonsware.com/misc/test2.3gp"; 

Uri uri=Uri.parse(path1); 

VideoView video=(VideoView)findViewById(R.id.VideoView01); 
video.setVideoURI(uri); 
video.start(); 
+0

我已經嘗試過這個...我能增加緩衝區的大小是這樣????對我有幫助????? byte buf [] =新字節[8192]; – user1221162 2012-03-15 09:21:32

0
VideoView video = (VideoView) findViewById(R.id.video); 
ProgressDialog mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Loading Video Please wait..."); 
      mProgressDialog.setIndeterminate(true); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 

      video.setMediaController(new MediaController(MainActivity.this)); 
      uri = Uri.parse("live streaming url"); 
      video.setVideoURI(uri); 

video.setOnPreparedListener(new OnPreparedListener() { 

      @Override 
      public void onPrepared(MediaPlayer mp) { 
       // TODO Auto-generated method stub 


       video.start(); 
       mProgressDialog.dismiss(); 
      } 
     });