如何实现应用内下载功能,以便用户可以离线观看视频? [英] How to implement in-app download feature, so user can want videos off-line?

查看:147
本文介绍了如何实现应用内下载功能,以便用户可以离线观看视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视频流应用。我想实现应用内下载功能,因此用户可以离线观看视频。


此外,这些下载的视频不应该是其他任何应用,如VLC,MS视频播放器等。



此下载选项应位于SD卡或内部存储器中。此外,用户可以设置使用移动数据或仅在Wi-Fi可用时下载。




 

解决方案

Hi SnagFilms-ViewLift,


>>我有一个视频流应用程序。我想实现应用内下载功能,因此用户可以离线观看视频


有两种方法可以实现它。


一种方法是使用
HttpClient API下载。像这样:

     


尝试      ;  
  {           
使用(HttpClient客户端=新HttpClient())          
{               
使用(var response = await client.GetAsync(uri))            
     {          
response.EnsureSuccessStatusCode();    
         using(IInputStream inputStream = await response.Content.ReadAsInputStreamAsync())
{    
                    //将流保存到文件中。                  
  }
      }    
     }    
   }      
  catch(例外情况)      
  {          
  Debug.WriteLine("无法加载图像:{0}",ex.Message);   
  }





另一种方法是使用

BackgroundDownloader类
这样做。像这样:



 private async void StartDownload_Click(object sender ,RoutedEventArgs e)
{
try
{
Uri source = new Uri(serverAddressField.Text.Trim());
string destination = fileNameField.Text.Trim ();

StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
destination,CreationCollisionOption.GenerateUniqueName);

BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source,destinationFile);

//附加进度和完成处理程序。
HandleDownloadAsync(下载,true);
}
catch(Exception ex)
{
LogException(" Download Error",ex);
}
}





有关更多信息,请参阅此链接:

后台转账


>>此外,这些下载的视频不应该是任何其他应用程序,如VLC,MS视频播放器等。


您可以在应用中处理文件激活当您想要打开应用程序时,请选择其他方式并选择您的应用程序。请
参考此链接:
处理文件激活


>>此下载选项应位于SD卡或内部存储器中。此外,用户可以设置使用移动数据
或仅在Wi-Fi可用时下载。


您需要在创建时自行决定用于接收下载数据的文件。


在开始下载之前,您可以自己检查网络状态。


检查是否是在Wifi中,您可以使用

NetworkInformation
获取
ConnectionProfile
对象。检查IsWlanConnectionProfile和IsWwanConnectionProfile属性以查看是否使用Wifi或使用移动数据。


您可以参考以下代码:



 bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable(); 
if(isInternetConnected)
{
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
bool isWLANConnection =(InternetConnectionProfile == null)? false:InternetConnectionProfile.IsWlanConnectionProfile;
Debug.WriteLine(isWLANConnection);
}





最好的问候,


Roy



I have a video streaming app. I want to implement in- app download feature, so users can watch videos offline.

And Also these downloaded videos should not be open is any other apps, like VLC, MS video player etc.

This download option should be in SD card or in internal memory. Also user can setup use mobile data or only download when Wi-fi is avaiable.

 

解决方案

Hi SnagFilms-ViewLift,

>> I have a video streaming app. I want to implement in- app download feature, so users can watch videos offline.

There are two ways to implement it.

One way is to use HttpClient APIs to download. Like this :

 try       
 {           
     using (HttpClient client = new HttpClient())          
    {                
       using (var response = await client.GetAsync(uri))            
      {          
        response.EnsureSuccessStatusCode();    
        using (IInputStream inputStream = await response.Content.ReadAsInputStreamAsync())
       {    
                    //save the stream to the file.                   
       }
      }    
     }    
   }      
  catch (Exception ex)      
  {           
     Debug.WriteLine("Failed to load the image: {0}", ex.Message);   
  }


Another way is to use the BackgroundDownloader Class to do it. Like this:

 private async void StartDownload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Uri source = new Uri(serverAddressField.Text.Trim());
                string destination = fileNameField.Text.Trim();

                StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
                    destination, CreationCollisionOption.GenerateUniqueName);

                BackgroundDownloader downloader = new BackgroundDownloader();
                DownloadOperation download = downloader.CreateDownload(source, destinationFile);

                // Attach progress and completion handlers.
                HandleDownloadAsync(download, true);
            }
            catch (Exception ex)
            {
                LogException("Download Error", ex);
            }
        }


For more information, you could refer this link: Background transfers.

>> And Also these downloaded videos should not be open is any other apps, like VLC, MS video player etc.

You could handle file activation in your app that when you want to open the app, choose other ways and choose your app. Please refer this link:Handle file activation.

>> This download option should be in SD card or in internal memory. Also user can setup use mobile data or only download when Wi-fi is avaiable.

You need to decide it yourself when you create a file to receive the download data.

Before you start your download, you could check the network status yourself.

To check if is in Wifi, you could use NetworkInformation to get the ConnectionProfile object. Check the IsWlanConnectionProfile and IsWwanConnectionProfile properties to see if is in Wifi or using Mobile Data.

You could refer the following code:

 bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable();
            if (isInternetConnected)
            {
                ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
                bool isWLANConnection = (InternetConnectionProfile == null) ? false : InternetConnectionProfile.IsWlanConnectionProfile;
                Debug.WriteLine(isWLANConnection);
            }


Best regards,

Roy


这篇关于如何实现应用内下载功能,以便用户可以离线观看视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆