void handleTableActionMore(UITableViewRowAction action, NSIndexPath indexPath) { activeAsset = saved ? savedAssets [indexPath.Row] : allAssets [indexPath.Row]; Log.Debug($"More: {activeAsset?.Music?.DisplayName}"); var alertController = UIAlertController.Create(activeAsset.Music.DisplayName, null, UIAlertControllerStyle.ActionSheet); var downloadState = AssetPersistenceManager.Shared.DownloadState(activeAsset); alertController.AddAction(UIAlertAction.Create("Share", UIAlertActionStyle.Default, handleAlertControllerActionShare)); switch (downloadState) { case MusicAssetDownloadState.NotDownloaded: alertController.AddAction(UIAlertAction.Create("Download", UIAlertActionStyle.Default, handleAlertControllerActionDownload)); break; case MusicAssetDownloadState.Downloading: alertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Destructive, handleAlertControllerActionCancel)); break; case MusicAssetDownloadState.Downloaded: alertController.AddAction(UIAlertAction.Create("Delete", UIAlertActionStyle.Destructive, handleAlertControllerActionDelete)); break; } alertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, handleAlertControllerActionDismiss)); PresentViewController(alertController, true, null); }
public void MockDownloadAsset(MusicAsset asset) { mockDownloadAsset = asset; AssetDownloadStateChanged?.Invoke(this, new MusicAssetDownloadStateChangeArgs(asset.Music, MusicAssetDownloadState.Downloading)); var rand = new Random(); double progress = 0; Task.Run(async() => { while (progress <= 1) { await Task.Delay(TimeSpan.FromSeconds(1)); AssetDownloadProgressChanged?.Invoke(this, new MusicAssetDownloadProgressChangeArgs(asset.Music, progress)); double inc = (double)rand.Next(1, 15) / 100; progress += inc; } await Task.Delay(TimeSpan.FromSeconds(1)); mockDownloadAsset = null; AssetDownloadProgressChanged?.Invoke(this, new MusicAssetDownloadProgressChangeArgs(asset.Music, 1)); AssetDownloadStateChanged?.Invoke(this, new MusicAssetDownloadStateChangeArgs(asset.Music, MusicAssetDownloadState.Downloaded)); }); }
/// Restores the Application state by getting all the AVAssetDownloadTasks and restoring their Asset structs. public async Task RestorePersistenceManagerAsync(List <AvContent> music) { if (baseDownloadURL == null) { throw new InvalidOperationException("must call Setup() before anything else"); } if (didRestorePersistenceManager) { return; } didRestorePersistenceManager = true; // Grab all the tasks associated with the assetDownloadURLSession var tasks = await assetDownloadURLSession.GetAllTasksAsync(); // For each task, restore the state in the app by recreating Asset structs and reusing existing AVURLAsset objects. foreach (var task in tasks) { if (task is AVAssetDownloadTask assetDownloadTask) { var song = music.FirstOrDefault(s => s.Id == assetDownloadTask.TaskDescription); if (song != null) { var asset = new MusicAsset(song, assetDownloadTask.UrlAsset); activeDownloadsMap [assetDownloadTask] = asset; } } } DidRestore?.Invoke(this, EventArgs.Empty); }
/// Cancels an AVAssetDownloadTask given an Asset. public void CancelDownload(MusicAsset asset) { foreach (var item in activeDownloadsMap) { if (asset == item.Value) { item.Key.Cancel(); } } }
/// Deletes an Asset on disk if possible. public void DeleteAsset(MusicAsset asset) { var localPath = localFilePath(asset.Id); // Check if there is a file URL stored for this asset. if (localPath != null) { NSFileManager.DefaultManager.Remove(localPath, out NSError error); if (error != null) { Log.Debug($"An error occured trying to delete the contents on disk for {asset.Id}: {error.LocalizedDescription}"); } Settings.SetSetting(asset.Id, string.Empty); AssetDownloadStateChanged?.Invoke(this, new MusicAssetDownloadStateChangeArgs(asset.Music, MusicAssetDownloadState.NotDownloaded)); } }
/// Triggers the initial AVAssetDownloadTask for a given Asset. public void DownloadAsset(MusicAsset asset) { Track.Download(asset.Music); // For the initial download, we ask the URLSession for an AVAssetDownloadTask // with a minimum bitrate corresponding with one of the lower bitrate variants in the asset. var taskOptions = new AVAssetDownloadOptions { MinimumRequiredMediaBitrate = 265000 }; var task = assetDownloadURLSession.GetAssetDownloadTask(asset.UrlAsset, asset.Id, null, taskOptions); // To better track the AVAssetDownloadTask we set the taskDescription to something unique for our sample. task.TaskDescription = asset.Id; activeDownloadsMap [task] = asset; task.Resume(); AssetDownloadStateChanged?.Invoke(this, new MusicAssetDownloadStateChangeArgs(asset.Music, MusicAssetDownloadState.Downloading)); }
/// Triggers the initial AVAssetDownloadTask for a given Asset. public void DownloadAsset(MusicAsset asset) { // For the initial download, we ask the URLSession for an AVAssetDownloadTask // with a minimum bitrate corresponding with one of the lower bitrate variants // in the asset. // 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(265000)); // should be this // var taskOptions = new AVAssetDownloadOptions { MinimumRequiredMediaBitrate = 265000 }; var task = assetDownloadURLSession.GetAssetDownloadTask(asset.UrlAsset, asset.Id, null, taskOptions); // To better track the AVAssetDownloadTask we set the taskDescription to something unique for our sample. task.TaskDescription = asset.Id; activeDownloadsMap [task] = asset; task.Resume(); AssetDownloadStateChanged?.Invoke(this, new MusicAssetDownloadStateChangeArgs(asset.Music, MusicAssetDownloadState.Downloading)); }
/// Returns the current download state for a given Asset. public MusicAssetDownloadState DownloadState(MusicAsset asset) { #if DEBUG if (mockDownloadAsset == asset) { return(MusicAssetDownloadState.Downloading); } #endif var localPath = localFilePath(asset.Id); // Check if there is a file URL stored for this asset. if (localPath != null) { // Check if the file exists on disk if (localPath.Path == baseDownloadURL.Path) { return(MusicAssetDownloadState.NotDownloaded); } if (NSFileManager.DefaultManager.FileExists(localPath.Path)) { return(MusicAssetDownloadState.Downloaded); } } // Check if there are any active downloads in flight. foreach (var item in activeDownloadsMap) { if (item.Value.Id == asset.Id) { return(MusicAssetDownloadState.Downloading); } } return(MusicAssetDownloadState.NotDownloaded); }
public bool TogglePlayback(MusicAsset asset) { if (asset == null) { Asset = null; return(false); } if (Asset != asset) { Asset = asset; return(true); } if (Asset == asset && readyForPlayback) { if (Player?.Rate == 0) { Player.Play(); updateCommandCenterNowPlayingInfo(); return(true); } if (Player?.Rate == 1) { Player.Pause(); updateCommandCenterNowPlayingInfo(); return(false); } } return(false); }