I want to get the video size in MB from videoview from internet path

空扰寡人 提交于 2019-12-07 13:37:01

问题


I'm trying to get the video file size and display it in the layout before starting the video.

I have tried many thing but it wont work

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("???");


        }
    });

I want the video size to appear while the video is initializing in onPreparedlistener

I tried this

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;

and this

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

nothing would work Please help


回答1:


I think there's a size limit with the way you're doing it. Try this way:

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)
{
}

edit: i just noticed you have tried something similar, but you spelt length incorrectly, maybe it's just that



来源:https://stackoverflow.com/questions/31982855/i-want-to-get-the-video-size-in-mb-from-videoview-from-internet-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!