如何在SD卡上下载视频列表并在videoview上播放?

时间:2016-03-02 14:14:06

标签: android android-videoview downloading

我已经使用视频视频在本地播放来自原始文件夹的视频,但现在我尝试首先在SD卡上下载视频列表,然后再将其播放到媒体播放器。这是我的视频。

公共类MainActivity扩展了Activity {

/* Full Screen Mode-Sticky */
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    View decorView = getWindow().getDecorView();
    if (hasFocus) {
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);}
}



public void downloadVideoFile(String url, String dest_file_name) {
    try {

        URL domain = new URL("http://192.168.0.22");
        String video_folder = "video";
        String sdcard_path = Environment.getExternalStorageDirectory().getAbsolutePath();
        String dest_video_path = sdcard_path + File.separator + video_folder + File.separator + dest_file_name;
        File dest_file = new File(dest_video_path);
        URL u = new URL(domain + "/files/video/");
        URLConnection conn = u.openConnection();
        int contentLength = conn.getContentLength();
        DataInputStream stream = new DataInputStream(u.openStream());
        byte[] buffer = new byte[contentLength];
        stream.readFully(buffer);
        stream.close();
        DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file));
        fos.write(buffer);
        fos.flush();
        fos.close();

    } catch(FileNotFoundException e) {

        return;

    } catch (IOException e) {

        return;

    }
}


private VideoView myVideo1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.activity_main);

    String video_folder = "myvideos";
    String sdcard_path = Environment.getExternalStorageDirectory().getAbsolutePath();
    File fvideo_path = new File(sdcard_path + File.separator + video_folder);
    File videolist[] = fvideo_path.listFiles();
    String play_path = videolist[0].getAbsolutePath();

    myVideo1=(VideoView)findViewById(R.id.myvideoview);
    myVideo1.setVideoPath(play_path);
    myVideo1.start();
    myVideo1.requestFocus();

    myVideo1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setLooping(true);
        }
    });
}

}

2 个答案:

答案 0 :(得分:1)

第1步:从sdcard上的特定路径构建视频列表

String video_folder = "myvideos";
String sdcard_path = Environment.getExternalStorageDirectory();

File fvideo_path = new File(sdcard_path + File.separator + video_folder);

File videolist[] = fvideo_path.listFiles();

第2步:按索引播放列表中的任何视频

//you can next or prev index from 0 - list lenght;
String play_path = videolist[0].getAbsolutePath();

第3步:将play_path设置为媒体播放器

   myVideo1.setVideoPath(play_path);
   myVideo1.start();
   myVideo1.requestFocus();

从服务器下载文件的示例代码:

public void downloadVideoFile(String url, String dest_file_name) {
          try {


              String video_folder = "myvideos";
              String sdcard_path = nvironment.getExternalStorageDirectory();
              String dest_video_path = sdcard_path + File.separator + video_folder + File.separator + dest_file_name;
              File dest_file = new File(dest_video_path);
              URL u = new URL(url);
              URLConnection conn = u.openConnection();
              int contentLength = conn.getContentLength();
              DataInputStream stream = new DataInputStream(u.openStream());
              byte[] buffer = new byte[contentLength];
              stream.readFully(buffer);
              stream.close();
              DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file));
              fos.write(buffer);
              fos.flush();
              fos.close();

          } catch(FileNotFoundException e) {

              return; 

          } catch (IOException e) {

              return; 

          }
    }

答案 1 :(得分:0)

我使用以下代码从服务器

下载视频
docker-compose