2016-11-25 92 views
-2

我是離子應用程序開發的新手。離線下載和存儲視頻離子應用程序

我有,我需要下載視頻從我的應用程序和他們商店內的應用,使視頻不是從文件夾訪問的需求,但可以只能從App觀看。

示例:離線存儲的工作方式可用於下載/存儲和查看視頻。

對此方向的任何幫助表示讚賞。

謝謝。 :)

回答

0

我已經在Xamarin中實現了這一點。您必須在應用程序路徑/配置下的應用程序特殊文件夾中創建您的文件。用戶無法查看該文件。創建文件後,您必須按字節下載視頻字節並寫入您的文件。下面是我用來創建文件並下載並讀入xamarin中的文件的代碼。

var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData); 
var filename = Path.Combine(path, ad.adid + ".mp4"); 
System.Uri url = new System.Uri(ad.video_url); 
const int BUFFER_SIZE = 16 * 1024; 

using (var outputFileStream = File.Create(filename)) 
{ 


    try 
    { 
     var req = WebRequest.Create(url); 
     using (var response = req.GetResponse()) 
     { 

     using (var responseStream = response.GetResponseStream()) 
      { 
       var buffer = new byte[BUFFER_SIZE]; 
       int bytesRead = -1; 
       do 
       { 

       bytesRead = responseStream.Read(buffer, 0, BUFFER_SIZE); 
       outputFileStream.Write(buffer, 0, bytesRead); 


       } while (bytesRead > 0); 



       catch (Exception e) 
       { 
         File.Delete(filename); 
         StopSelf(); 
         return; 
       } 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     File.Delete(filename); 
     StopSelf(); 
     return; 
    } 
} 

}

P.S:正如我所提到這是xmarin升C代碼。這不會在Android上工作,但想法保持不變。您可以輕鬆地將此代碼轉換爲Java。