EQの動画ダウンロード機能を利用し、動画ファイルを端末に保存します。
動画ファイルをダウンロードするためのURLは、EQ管理画面から取得いただけます。
※詳細は、「動画コンテンツのダウンロードリンク取得」をご確認ください。
■iOS端末向けアプリの場合
AFNetworkingフレームワーク(Ver.1.x)を利用する方法をご紹介します。
- AFNetworkingパッケージを入手のうえ、インクルードします。
- ダウンロードリンクURLを指定し、端末に動画ファイルをダウンロード、保存します。
// 動画コンテンツのダウンロードリンクURLを指定します NSURL *url = [NSURL URLWithString:@“[ダウンロードリンクURL];"]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; // 保存先を指定します NSString *videoName = @"video.mp4"; //保存先ファイル名 NSString *dir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; //保存先ディレクトリ NSString *downloadDestinationPath = [documentDir stringByAppendingPathComponent:videoName]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:downloadDestinationPath append:NO]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { //成功 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { / [operation start]; |
【補足】
■Compiler Flagsの変更
AFNetworkingを利用する場合、ARCではビルド時にエラーとなるため、プロジェクト全体もしくは関連部分のソースコードについてARCを適用しないようCompiler Flagsを変更します。
■Android端末向けアプリの場合
DownloadManager(Andorid API Level9/Android 2.3以上)を利用する例をご紹介します。
※上記以外のOSバージョンを対象とする場合などは、httpClient(httpget)で実装する方法もあります。
- マニフェストに「permission」を追加します。
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
- 必要なパッケージを「import」します。
import android.net.Uri; import android.os.Environment; import android.app.DownloadManager; import android.app.DownloadManager.Request; import android.content.Context; |
- 「DownloadManager」にダウンロードリンクURLを指定し、動画コンテンツをダウンロード、保存します。
Context context = getApplicationContext(); DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE); //動画コンテンツのダウンロードリンクURLを指定します String url = “[ダウンロードリンクURL]"; Uri uri = Uri.parse(url); Request request = new Request(uri); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/video.mp4"); //保存先の指定 request.setTitle("video.mp4"); request.setVisibleInDownloadsUi(false); int id = downloadManager.enqueue(request); |