Beispiel #1
0
        void handlePersistanceManagerAssetDownloadStateChanged(object sender, MusicAssetDownloadStateChangeArgs e)
        {
            Log.Debug($"{e.Music.DisplayName} | {e.State}");

            BeginInvokeOnMainThread(() =>
            {
                var cell = TableView.VisibleCells.FirstOrDefault(c => c.TextLabel.Text == e.Music.DisplayName);

                if (cell != null)
                {
                    TableView.ReloadRows(new NSIndexPath [] { TableView.IndexPathForCell(cell) }, UITableViewRowAnimation.Automatic);
                }
            });
        }
        public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
        {
            // This is the ideal place to begin downloading additional media selections
            // once the asset itself has finished downloading.

            MusicAsset asset;

            var assetTask = task as AVAssetDownloadTask;

            if (assetTask == null || !activeDownloadsMap.TryGetValue(assetTask, out asset))
            {
                return;
            }

            Log.Debug($"Removing {assetTask.TaskDescription} from activeDownloads");

            activeDownloadsMap.Remove(assetTask);

            if (error != null)
            {
                Log.Debug($"DidCompleteWithError: {task?.TaskDescription} | {error.Domain} :: {error.LocalizedDescription}");

                if (error.Domain == NSError.NSUrlErrorDomain)
                {
                    if (error.Code == (int)NSUrlError.Cancelled)
                    {
                        // This task was canceled, you should perform cleanup using the
                        // URL saved from AVAssetDownloadDelegate.urlSession(_:assetDownloadTask:didFinishDownloadingTo:).

                        DeleteAsset(asset);
                    }
                    else if (error.Code == (int)NSUrlError.Unknown)
                    {
                        Log.Debug($"FATAL: Downloading HLS streams is not supported in the simulator.");
                        //fatalError ("Downloading HLS streams is not supported in the simulator.")
                    }
                }

                Log.Debug($"FATAL: An unexpected error occured {error.Domain}");
                //fatalError ("An unexpected error occured \(error.domain)")
            }
            else
            {
                var change = new MusicAssetDownloadStateChangeArgs(asset.Music);

                var mediaSelectionPair = nextMediaSelection(assetTask.UrlAsset);

                if (mediaSelectionPair.Item1 != null)
                {
                    // This task did complete sucessfully. At this point the application
                    // can download additional media selections if needed.
                    //
                    // To download additional `AVMediaSelection`s, you should use the
                    // `AVMediaSelection` reference saved in `AVAssetDownloadDelegate.urlSession(_:assetDownloadTask:didResolve:)`.

                    AVMediaSelection originalMediaSelection;

                    if (!mediaSelectionMap.TryGetValue(assetTask, out originalMediaSelection))
                    {
                        return;
                    }

                    // There are still media selections to download.
                    //
                    // Create a mutable copy of the AVMediaSelection reference saved in
                    // `AVAssetDownloadDelegate.urlSession(_:assetDownloadTask:didResolve:)`.

                    var mediaSelection = originalMediaSelection.MutableCopy() as AVMutableMediaSelection;

                    // Select the AVMediaSelectionOption in the AVMediaSelectionGroup we found earlier.
                    mediaSelection.SelectMediaOption(mediaSelectionPair.Item2, mediaSelectionPair.Item1);

                    // Ask the `URLSession` to vend a new `AVAssetDownloadTask` using
                    // the same `AVURLAsset` and assetTitle as before.
                    //
                    // This time, the application includes the specific `AVMediaSelection`
                    // to download as well as a higher bitrate.


                    // TODO: workaround for bug https://bugzilla.xamarin.com/show_bug.cgi?id=44201 to get it to build in Mobile Center
                    var taskOptions = new NSDictionary(new NSString("AVAssetDownloadTaskMinimumRequiredMediaBitrateKey"), new NSNumber(2000000), new NSString("AVAssetDownloadTaskMediaSelectionKey"), mediaSelection);

                    // should be this
                    // var taskOptions = new AVAssetDownloadOptions { MinimumRequiredMediaBitrate = 2000000, MediaSelection = mediaSelection };

                    var newTask = assetDownloadURLSession.GetAssetDownloadTask(assetTask.UrlAsset, asset.Id, null, taskOptions);

                    if (newTask == null)
                    {
                        return;
                    }

                    activeDownloadsMap [newTask] = asset;

                    newTask.Resume();

                    change.State = MusicAssetDownloadState.Downloading;

                    Log.Debug($"????????? ?????????? ??????????? {mediaSelectionPair.Item2.DisplayName}");

                    //change.DisplayName = mediaSelectionPair.Item2.DisplayName;
                }
                else
                {
                    // All additional media selections have been downloaded.
                    change.State = MusicAssetDownloadState.Downloaded;
                }

                AssetDownloadStateChanged?.Invoke(this, change);
            }
        }