2011-02-10 190 views
0

我想播放mp4視頻。所以我嘗試以下方法流媒體和播放mp4視頻

private void playVideo() { 
     try { 

      final String path = s; 
      Log.v(TAG, "path: " + path); 
      if (path == null || path.length() == 0) { 
      Toast.makeText(VideoPlay1.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) { 
        MediaController mediaController = new MediaController(this); 
        mediaController.setAnchorView(mVideoView); 
        Uri video = Uri.parse(getDataSource(path)); 
        Log.e("Uri video",video.toString()); 
        mVideoView.setMediaController(mediaController); 
        mVideoView.setVideoURI(video);  
        mVideoView.setVideoPath(getDataSource(path)); 
        mVideoView.setOnPreparedListener(new OnPreparedListener() { 

         public void onPrepared(MediaPlayer arg0) { 
          dialog.dismiss(); 
          mVideoView.start(); 
         } 
        }); 
        mVideoView.requestFocus(); 
       return; 
      } 
      current = path; 

      MediaController mediaController = new MediaController(this); 
      Uri video = Uri.parse(getDataSource(path)); 

      Log.e("Uri video",video.toString()); 
      mVideoView.setMediaController(mediaController); 
      mVideoView.setVideoURI(video);  
      mVideoView.setVideoPath(getDataSource(path)); 
       mVideoView.setOnPreparedListener(new OnPreparedListener() { 

        public void onPrepared(MediaPlayer arg0) { 
         dialog.dismiss(); 
         mVideoView.start(); 
        } 
       }); 
      mVideoView.requestFocus(); 
    } 
     } catch (Exception e) { 
      Log.e(TAG, "error123: " + e.getMessage(), e); 
      if (mVideoView != null) { 
       mVideoView.stopPlayback(); 
      } 
     } 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5 
       && keyCode == KeyEvent.KEYCODE_BACK 
       && event.getRepeatCount() == 0) { 
      Log.d("CDA", "onKeyDown Called"); 
      onBackPressed(); 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    public void onBackPressed() { 

     Log.d("CDA", "onBackPressed Called"); 
     mVideoView.stopPlayback(); 
     Intent setIntent = new Intent(VideoPlay1.this,VideoPage.class); 
     startActivity(setIntent); 
     finish(); 
     return; 
    } 
    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; 
     } 
    } 

上述工作需要花費更多的時間來播放視頻,請告訴我,如果有另一種方式。

謝謝。

最好的問候。

回答

1

對不起,但將視頻流式傳輸到移動設備需要時間。你受限於連接,而不是代碼

+0

還有其他方法可以同時播放和播放視頻 – Ramakrishna 2011-02-10 14:11:21

0

我知道已經有一個被接受的答案,但可能有其他標準可能有幫助,特別是文件的編碼。

一個MP4視頻通常在默認情況下具有MOOV ATOM。 「moov原子,也被稱爲電影原子,定義了電影的時間刻度,持續時間,顯示特性,以及包含電影中每個軌道信息的子原子。」 (http://www.adobe.com/devnet/video/articles/mp4_movie_atom.html)。如果您在開始時使用moov原子對視頻進行編碼(您可以使用FFMPEG進行此操作),那麼您的流媒體體驗可能會得到改善。