2015-08-13 61 views
3

我試圖獲得視頻文件的大小,並在開始視頻之前在佈局中顯示它。我想從視頻的互聯網路徑獲取以MB爲單位的視頻大小

我已經嘗試了很多事情,但它不會工作

video_player_view = (VideoView) findViewById(R.id.videoView2); 
    media_Controller = new MediaController(this); 
    dm = new DisplayMetrics(); 
    this.getWindowManager().getDefaultDisplay().getMetrics(dm); 
    int height = dm.heightPixels; 
    int width = dm.widthPixels; 
    try { 
     video_player_view.setMinimumWidth(width); 
     video_player_view.setMinimumHeight(height); 
    } catch (Exception e) {} 
    video_player_view.setMediaController(media_Controller); 
    video_player_view.setVideoPath(path); 
    video_player_view.start(); 


    if (savedInstanceState != null) { 
     video_player_view.seekTo(savedInstanceState.getInt("current_position")); 
    } 

    vidTitle = (TextView) findViewById(R.id.vidTitle); 
    vidDesc = (TextView) findViewById(R.id.vidDesc); 
    vidSize = (TextView) findViewById(R.id.vidSize); 
    vidDur = (TextView) findViewById(R.id.vidDur); 
    vidDownload = (ImageButton) findViewById(R.id.vidDownload); 
    vidFav = (CheckBox) findViewById(R.id.vidFav); 
    vidTitle.setText(singleVideoInfo.title); 
    vidDesc.setText(singleVideoInfo.description); 
    video_player_view.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
     @Override 
     public void onPrepared(MediaPlayer mp) { 
      progressBarCircularIndeterminate.setVisibility(View.GONE); 
      int duration = video_player_view.getDuration()/1000; 
      int hours = duration/3600; 
      int minutes = (duration/60) - (hours * 60); 
      int seconds = duration - (hours * 3600) - (minutes * 60); 
      String formatted = String.format("Duration: %d:%02d:%02d", hours, minutes, seconds); 
      vidDur.setText(formatted); 
      //vidSize.setText("???"); 


     } 
    }); 

我想要的視頻大小出現在視頻正在初始化在onPreparedlistener

我想這

URL myUrl = new URL("http://your_url.com/file.mp4"); 
URLConnection urlConnection = myUrl.openConnection(); 
urlConnection.connect(); 
int file_size = urlConnection.getContentLength(); 
    file_size = file_size /1024; 

URL myUrl = new URL("http://your_url.com/file.mp4"); 
myConnection = myUrl.openConnection(); 
List headersize = myConnection.getHeaderFields().get("content-Lenght"); 

什麼都不會工作 請幫忙

+0

沒有它的工作Kizukooo? – Ben

回答

1

我認爲這樣做的方式有一個大小限制。試試這個方法:

final URL uri=new URL("http://your_url.com/file.mp4"); 
URLConnection connection; 
try 
{ 
    connection=uri.openConnection(); 
    connection.connect(); 
    final String contentLengthStr=ucon.getHeaderField("content-length"); 
    // do whatever 
} 

catch(final IOException exception) 
{ 
} 

編輯:我只注意到你已經試過類似的東西,但你拼寫錯誤的長度,也許它只是

相關問題