private void OnDownloadAvailable(YoutubeEntry feed, VideoInfo videoInfo, MediaType mediaType)
 {
     Controls.Title.Text = videoInfo.Title;
     Dm = (DownloadManager) Controls.Activity.BaseContext.GetSystemService(Context.DownloadService);
     var fileName = DownloadHelper.GetLegalPath(videoInfo.Title) + videoInfo.VideoExtension;
     var request =
         new DownloadManager.Request(Android.Net.Uri.Parse(videoInfo.DownloadUri.ToString()));
     request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile)
        .AddRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11")
        .SetShowRunningNotification(true)
        .SetAllowedOverRoaming(true)
        .SetTitle(videoInfo.Title)
        .SetDescription(fileName)
        .SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, fileName);
     Enqueue = Dm.Enqueue(request);
 }
 private static void AddToFile(YoutubeEntry entry, Uri uri, Stream destinationStream, long? start, long? stop, MSYoutubeLoading onYoutubeLoading)
 {
     if (entry.ExecutionStatus != ExecutionStatus.Normal) return;
     var response = DownloadToStreamAsync(uri, start, stop);
     if (response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable) return;
     var range = GetRange(response);
     var total =  range.Length; // (response.Headers.ContentRange.Length ?? 0);
     var to = range.To; // (response.Headers.ContentRange.To ?? 0);
     using (var stream = response.GetResponseStream()) {
         if (stream == null) return;
         CopyStream(stream, destinationStream);
         destinationStream.Flush();
         if (onYoutubeLoading != null && entry.ExecutionStatus == ExecutionStatus.Normal) onYoutubeLoading(to, total);
         if (total > to + 1)
             AddToFile(entry, uri, destinationStream, to + 1, to + BlockSize, onYoutubeLoading);
     }
 }
 private void UpdateStatus(DownloadState state, YoutubeEntry entry, double percentage)
 {
     DownloadState = state;
     Percentage = percentage;
     if (OnListDownloadStatusChange != null) OnListDownloadStatusChange(this, entry, DownloadState, Percentage);
 }
 public static void DownloadToFileAsync(YoutubeEntry entry, Uri uri, StorageFolder folder, string fileName, MSYoutubeLoading onYoutubeLoading)
 {
     if (entry.ExecutionStatus != ExecutionStatus.Normal) return;
     var storageFile = GetFile(folder, fileName);
     using (var destinationStream = storageFile.OpenStreamForWriteAsync()) {
         if (destinationStream == null) return;
         //var properties = await storageFile.GetBasicPropertiesAsync();
         var start = destinationStream.Length; // (long)properties.Size;
         destinationStream.Position = destinationStream.Length;
         AddToFile(entry, uri, destinationStream, start, start + BlockSize - 1, onYoutubeLoading);
     }
 }
 private YoutubeEntry(YoutubeEntry parent = null)
 {
     Parent = parent;
     _settings = new MSYoutubeSettings( "MS.Youtube.Downloader", "AI39si76x-DO4bui7H1o0P6x8iLHPBvQ24exnPiM8McsJhVW_pnCWXOXAa1D8-ymj0Bm07XrtRqxBC7veH6flVIYM7krs36kQg" ) {AutoPaging = true, PageSize = 50};
 }
 public static YoutubeEntry Create(Uri uri, YoutubeEntry parent = null)
 {
     var entry = new YoutubeEntry(parent);
     if (uri != null) 
         entry.Uri = uri;
     return entry;
 }
 public YoutubeEntry Clone()
 {
     var entry = new YoutubeEntry {
         Title = Title,
         BaseFolder = BaseFolder,
         Parent = Parent,
         Description = Description,
         DownloadFolder = DownloadFolder,
         ProviderFolder = ProviderFolder,
         MediaType = MediaType,
         ThumbnailUrl = ThumbnailUrl,
         Uri = Uri,
         VideoExtension = VideoExtension,
         VideoFolder = VideoFolder,
         ExecutionStatus = ExecutionStatus
     };
     if (entry.ExecutionStatus == ExecutionStatus.Deleted) entry.DownloadState = DownloadState.Deleted;
     if (ThumbnailUrls != null && ThumbnailUrls.Length > 0) {
         entry.ThumbnailUrls = new string[ThumbnailUrls.Length];
         for (var i = 0; i < ThumbnailUrls.Length; i++)
             entry.ThumbnailUrls[i] = ThumbnailUrls[i];
     }
     return entry;
 }
 public AudioConverter(YoutubeEntry youtubeEntry, EntryDownloadStatusEventHandler onEntryDownloadStatusChange)
 {
     _youtubeEntry = youtubeEntry;
     _onEntryDownloadStatusChange = onEntryDownloadStatusChange;
     _applicationPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
 }