facebook twitter hatena line email

「Android/VideoView/ダウンロード」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行14: 行14:
 
class VideoDownloaderConnection extends AsyncTask<Void, Long, Boolean> {
 
class VideoDownloaderConnection extends AsyncTask<Void, Long, Boolean> {
 
     private String TAG = "VideoDownloaderConnection";
 
     private String TAG = "VideoDownloaderConnection";
     private Context context;
+
     private Context mContext;
     String url = "";
+
     String mUrl = "";
     VideoDownloaderConnectionListener listener;
+
     VideoDownloaderConnectionListener mListener;
 
     public VideoDownloaderConnection(Context context) {
 
     public VideoDownloaderConnection(Context context) {
         this.context = context;
+
         mContext = context;
 
     }
 
     }
 
     public void setUrl(String url) {
 
     public void setUrl(String url) {
         this.url = url;
+
         mUrl = url;
 
     }
 
     }
 
     public void setListener(VideoDownloaderConnectionListener listener) {
 
     public void setListener(VideoDownloaderConnectionListener listener) {
         this.listener = listener;
+
         mListener = listener;
 
     }
 
     }
 
     @Override
 
     @Override
行35: 行35:
 
         InputStream is = null;
 
         InputStream is = null;
 
         try {
 
         try {
             u = new URL(url);
+
             u = new URL(mUrl);
 
             is = u.openStream();
 
             is = u.openStream();
 
             HttpURLConnection huc = (HttpURLConnection)u.openConnection();
 
             HttpURLConnection huc = (HttpURLConnection)u.openConnection();
行41: 行41:
 
             if (huc != null) {
 
             if (huc != null) {
 
                 String fileName = "FILE.mp4";
 
                 String fileName = "FILE.mp4";
                 String storagePath = context.getExternalCacheDir().toString();
+
                 String storagePath = mContext.getExternalCacheDir().toString();
 
                 File f = new File(storagePath,fileName);
 
                 File f = new File(storagePath,fileName);
 
                 FileOutputStream fos = new FileOutputStream(f);
 
                 FileOutputStream fos = new FileOutputStream(f);
行57: 行57:
 
         } catch (MalformedURLException mue) {
 
         } catch (MalformedURLException mue) {
 
             mue.printStackTrace();
 
             mue.printStackTrace();
             if (listener != null) {
+
             if (mListener != null) {
                 listener.downloadFailed();
+
                 mListener.downloadFailed();
 
             }
 
             }
 
         } catch (IOException ioe) {
 
         } catch (IOException ioe) {
 
             ioe.printStackTrace();
 
             ioe.printStackTrace();
             if (listener != null) {
+
             if (mListener != null) {
                 listener.downloadFailed();
+
                 mListener.downloadFailed();
 
             }
 
             }
 
         } finally {
 
         } finally {
行71: 行71:
 
                 }
 
                 }
 
             } catch (IOException ioe) {
 
             } catch (IOException ioe) {
                    if (listener != null) {
+
                if (mListener != null) {
                        listener.downloadFailed();
+
                    mListener.downloadFailed();
                    }
+
                }
 
             }
 
             }
 
         }
 
         }
行86: 行86:
 
         super.onPostExecute(aBoolean);
 
         super.onPostExecute(aBoolean);
 
         Log.i(TAG, "onPostExecute");
 
         Log.i(TAG, "onPostExecute");
         if (listener != null) {
+
         if (mListener != null) {
             listener.downloadSuccess();
+
             mListener.downloadSuccess();
 
         }
 
         }
 
     }
 
     }

2018年11月22日 (木) 15:15時点における版

サンプル

事前ダウンロードして動画表示 VideoDownloaderConnection.java

import android.content.Context;
import android.os.AsyncTask;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
class VideoDownloaderConnection extends AsyncTask<Void, Long, Boolean> {
    private String TAG = "VideoDownloaderConnection";
    private Context mContext;
    String mUrl = "";
    VideoDownloaderConnectionListener mListener;
    public VideoDownloaderConnection(Context context) {
        mContext = context;
    }
    public void setUrl(String url) {
        mUrl = url;
    }
    public void setListener(VideoDownloaderConnectionListener listener) {
        mListener = listener;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Boolean doInBackground(Void... params) {
        URL u = null;
        InputStream is = null;
        try {
            u = new URL(mUrl);
            is = u.openStream();
            HttpURLConnection huc = (HttpURLConnection)u.openConnection();
            int size = huc.getContentLength();
            if (huc != null) {
                String fileName = "FILE.mp4";
                String storagePath = mContext.getExternalCacheDir().toString();
                File f = new File(storagePath,fileName);
                FileOutputStream fos = new FileOutputStream(f);
                byte[] buffer = new byte[1024];
                int len1 = 0;
                if (is != null) {
                    while ((len1 = is.read(buffer)) > 0) {
                        fos.write(buffer,0, len1);
                    }
                }
                if (fos != null) {
                    fos.close();
                }
            }
        } catch (MalformedURLException mue) {
            mue.printStackTrace();
            if (mListener != null) {
                mListener.downloadFailed();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
            if (mListener != null) {
                mListener.downloadFailed();
            }
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException ioe) {
                if (mListener != null) {
                    mListener.downloadFailed();
                }
            }
        }
        return true;
    }
    @Override
    protected void onProgressUpdate(Long... values) {
        super.onProgressUpdate(values);
    }
    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        Log.i(TAG, "onPostExecute");
        if (mListener != null) {
            mListener.downloadSuccess();
        }
    }
}

VideoDownloaderConnectionListener.java

public interface GNSMediaDownloaderListener
{
    void downloadSuccess();
    void downloadFailed();
}

上記サンプルDLの使い方

private VideoDownloaderConnectionListener mediaDownloaderLisener = new VideoDownloaderConnectionListener() {
        public void downloadSuccess() {
        }
        public void downloadFailed() {
        }
};
VideoDownloaderConnection connection = new VideoDownloaderConnection(MainActivity.this.getApplicationContext());
connection.setUrl("~.mp4");
connection.setListener(mediaDownloaderLisener);
connection.execute();

ストレージをcachedirとしているがいろいろ変更できる

String storagePath = context.getExternalCacheDir().toString(); // cachedir
String storagePath = Environment.getExternalStorageDirectory().toString(); // sdcard

参考:http://blog.lciel.jp/blog/2014/02/08/android-about-storage/

上記DLしたmp4のvideoViewでの再生

mVideoView.setVideoPath(getApplicationContext().getExternalCacheDir().toString() + "/FILE.mp4");

参考

動画のダウンロード https://blog.engineer.adways.net/entry/2016/09/03/010627

Webview download/cache video https://stackoverflow.com/questions/23876103/webview-download-cache-video

YoutubeDownloadMp4 https://stackoverflow.com/questions/7203047/code-for-download-video-from-youtube-on-java-android