「Android/VideoView/ダウンロード」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプル) |
|||
(同じ利用者による、間の9版が非表示) | |||
行1: | 行1: | ||
==サンプル== | ==サンプル== | ||
事前ダウンロードして動画表示 | 事前ダウンロードして動画表示 | ||
+ | |||
VideoDownloaderConnection.java | VideoDownloaderConnection.java | ||
<pre> | <pre> | ||
行14: | 行15: | ||
class VideoDownloaderConnection extends AsyncTask<Void, Long, Boolean> { | class VideoDownloaderConnection extends AsyncTask<Void, Long, Boolean> { | ||
private String TAG = "VideoDownloaderConnection"; | private String TAG = "VideoDownloaderConnection"; | ||
− | private Context | + | private static final int MAX_VIDEO_SIZE = 25 * 1024 * 1024; // 25 MiB |
− | String | + | private Context mContext; |
− | VideoDownloaderConnectionListener | + | private String mUrl = ""; |
+ | private VideoDownloaderConnectionListener mListener; | ||
public VideoDownloaderConnection(Context context) { | public VideoDownloaderConnection(Context context) { | ||
− | + | mContext = context; | |
} | } | ||
public void setUrl(String url) { | public void setUrl(String url) { | ||
− | + | mUrl = url; | |
} | } | ||
public void setListener(VideoDownloaderConnectionListener listener) { | public void setListener(VideoDownloaderConnectionListener listener) { | ||
− | + | mListener = listener; | |
} | } | ||
@Override | @Override | ||
行35: | 行37: | ||
InputStream is = null; | InputStream is = null; | ||
try { | try { | ||
− | u = new URL( | + | u = new URL(mUrl); |
is = u.openStream(); | is = u.openStream(); | ||
HttpURLConnection huc = (HttpURLConnection)u.openConnection(); | HttpURLConnection huc = (HttpURLConnection)u.openConnection(); | ||
int size = huc.getContentLength(); | int size = huc.getContentLength(); | ||
+ | if (size > MAX_VIDEO_SIZE) { | ||
+ | log.debug_e(TAG, String.format( | ||
+ | "MediaDownloader encountered video larger than disk cap. " + | ||
+ | "(%d bytes / %d maximum).", | ||
+ | size, | ||
+ | MAX_VIDEO_SIZE)); | ||
+ | if (mListener != null) { | ||
+ | mListener.downloadFailed(); | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
if (huc != null) { | if (huc != null) { | ||
− | + | File f = new File(getStorageDirPath(), getStorageFileName()); | |
− | + | ||
− | File f = new File( | + | |
FileOutputStream fos = new FileOutputStream(f); | FileOutputStream fos = new FileOutputStream(f); | ||
byte[] buffer = new byte[1024]; | byte[] buffer = new byte[1024]; | ||
行57: | 行68: | ||
} catch (MalformedURLException mue) { | } catch (MalformedURLException mue) { | ||
mue.printStackTrace(); | mue.printStackTrace(); | ||
− | if ( | + | if (mListener != null) { |
− | + | mListener.downloadFailed(); | |
+ | return false; | ||
} | } | ||
} catch (IOException ioe) { | } catch (IOException ioe) { | ||
ioe.printStackTrace(); | ioe.printStackTrace(); | ||
− | if ( | + | if (mListener != null) { |
− | + | mListener.downloadFailed(); | |
+ | return false; | ||
} | } | ||
} finally { | } finally { | ||
行71: | 行84: | ||
} | } | ||
} catch (IOException ioe) { | } catch (IOException ioe) { | ||
− | + | if (mListener != null) { | |
− | + | mListener.downloadFailed(); | |
− | } | + | return false; |
+ | } | ||
} | } | ||
} | } | ||
行83: | 行97: | ||
} | } | ||
@Override | @Override | ||
− | protected void onPostExecute(Boolean | + | protected void onPostExecute(Boolean flag) { |
− | super.onPostExecute( | + | super.onPostExecute(flag); |
Log.i(TAG, "onPostExecute"); | Log.i(TAG, "onPostExecute"); | ||
− | if ( | + | if (flag) { |
− | + | if (mListener != null) { | |
+ | mListener.downloadSuccess(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | private String getStorageFileName() { | ||
+ | return md5(mUrl) + ".mp4"; | ||
+ | } | ||
+ | public String getStorageFilePath() { | ||
+ | return getStorageDirPath() + "/" + getStorageFileName(); | ||
+ | } | ||
+ | private String getStorageDirPath() { | ||
+ | return mContext.getExternalCacheDir().toString(); | ||
+ | } | ||
+ | private static String md5(String s) { | ||
+ | try { | ||
+ | MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); | ||
+ | digest.update(s.getBytes()); | ||
+ | byte messageDigest[] = digest.digest(); | ||
+ | StringBuffer hexString = new StringBuffer(); | ||
+ | for (int i = 0; i < messageDigest.length; i++) { | ||
+ | String h = Integer.toHexString(0xFF & messageDigest[i]); | ||
+ | while (h.length() < 2) | ||
+ | h = "0" + h; | ||
+ | hexString.append(h); | ||
+ | } | ||
+ | return hexString.toString(); | ||
+ | } catch (NoSuchAlgorithmException e) { | ||
+ | e.printStackTrace(); | ||
} | } | ||
+ | return ""; | ||
} | } | ||
} | } | ||
行95: | 行138: | ||
VideoDownloaderConnectionListener.java | VideoDownloaderConnectionListener.java | ||
<pre> | <pre> | ||
− | public interface | + | public interface VideoDownloaderConnectionListener |
{ | { | ||
void downloadSuccess(); | void downloadSuccess(); | ||
行117: | 行160: | ||
ストレージをcachedirとしているがいろいろ変更できる | ストレージをcachedirとしているがいろいろ変更できる | ||
− | + | return mContext.getExternalCacheDir().toString(); // cachedir | |
− | + | return Environment.getExternalStorageDirectory().toString(); // sdcard | |
参考:http://blog.lciel.jp/blog/2014/02/08/android-about-storage/ | 参考:http://blog.lciel.jp/blog/2014/02/08/android-about-storage/ | ||
===上記DLしたmp4のvideoViewでの再生=== | ===上記DLしたmp4のvideoViewでの再生=== | ||
− | + | VideoDownloaderConnection downloader = new VideoDownloaderConnection(context); | |
+ | downloader.setUrl("ttps://hogehoge.com/hoge.mp4"); | ||
+ | File file = new File(downloader.getStorageFilePath()); | ||
+ | if (file.exists()) { | ||
+ | mVideoView.setVideoPath(path); | ||
+ | } | ||
==参考== | ==参考== |
2018年11月29日 (木) 16:43時点における最新版
サンプル
事前ダウンロードして動画表示
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 static final int MAX_VIDEO_SIZE = 25 * 1024 * 1024; // 25 MiB private Context mContext; private String mUrl = ""; private 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 (size > MAX_VIDEO_SIZE) { log.debug_e(TAG, String.format( "MediaDownloader encountered video larger than disk cap. " + "(%d bytes / %d maximum).", size, MAX_VIDEO_SIZE)); if (mListener != null) { mListener.downloadFailed(); } return false; } if (huc != null) { File f = new File(getStorageDirPath(), getStorageFileName()); 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(); return false; } } catch (IOException ioe) { ioe.printStackTrace(); if (mListener != null) { mListener.downloadFailed(); return false; } } finally { try { if (is != null) { is.close(); } } catch (IOException ioe) { if (mListener != null) { mListener.downloadFailed(); return false; } } } return true; } @Override protected void onProgressUpdate(Long... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(Boolean flag) { super.onPostExecute(flag); Log.i(TAG, "onPostExecute"); if (flag) { if (mListener != null) { mListener.downloadSuccess(); } } } private String getStorageFileName() { return md5(mUrl) + ".mp4"; } public String getStorageFilePath() { return getStorageDirPath() + "/" + getStorageFileName(); } private String getStorageDirPath() { return mContext.getExternalCacheDir().toString(); } private static String md5(String s) { try { MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); StringBuffer hexString = new StringBuffer(); for (int i = 0; i < messageDigest.length; i++) { String h = Integer.toHexString(0xFF & messageDigest[i]); while (h.length() < 2) h = "0" + h; hexString.append(h); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } }
VideoDownloaderConnectionListener.java
public interface VideoDownloaderConnectionListener { 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としているがいろいろ変更できる
return mContext.getExternalCacheDir().toString(); // cachedir return Environment.getExternalStorageDirectory().toString(); // sdcard
参考:http://blog.lciel.jp/blog/2014/02/08/android-about-storage/
上記DLしたmp4のvideoViewでの再生
VideoDownloaderConnection downloader = new VideoDownloaderConnection(context); downloader.setUrl("ttps://hogehoge.com/hoge.mp4"); File file = new File(downloader.getStorageFilePath()); if (file.exists()) { mVideoView.setVideoPath(path); }
参考
動画のダウンロード 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