void UpdateDownloadInfo(DownloadInfo downloadInfo, DownloadInfo.STATUS status, NSUrl destinationUrl) { if (downloadInfo == null) { return; } downloadInfo.Status = status; downloadInfo.DestinationFilename = Path.GetFileName(destinationUrl.AbsoluteString); }
/// <summary> /// Plays the MP3 associated with the download. Stops output first. /// </summary> /// <param name="downloadInfo">Download info.</param> public void PlayAudio(DownloadInfo downloadInfo) { this.StopAudio(); if (downloadInfo == null) { return; } Console.WriteLine("Playing file '{0}'.", downloadInfo.DestinationFilename); if (!File.Exists(downloadInfo.FullDestinationFilePath)) { Console.WriteLine("Cannot find file '{0}'!", downloadInfo.FullDestinationFilePath); return; } NSError error = null; // Use AVAudioPlayer to load the MP3 file and start playing. this.audioPlayer = new AVAudioPlayer(NSUrl.FromFilename(downloadInfo.FullDestinationFilePath), "mp3", out error); if (error == null) { audioPlayer.Play(); this.stopBtn.Enabled = true; // Update information about currently played song. MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = new MPNowPlayingInfo { Artist = downloadInfo.Artist, Title = downloadInfo.Title }; // Update selection in the table view. this.TableView.SelectRow(NSIndexPath.FromRowSection(this.currentSongIndex, 0), true, UITableViewScrollPosition.Middle); } else { Console.WriteLine("Error playing back audio: " + error); } }
/// <summary> /// Gets called by iOS if all pending transfers are done. This will only be called if the app was backgrounded. /// </summary> public override void DidFinishEventsForBackgroundSession(NSUrlSession session) { // Nothing more to be done. This is the place where we have to call the completion handler we get passed in in AppDelegate. var handler = AppDelegate.BackgroundSessionCompletionHandler; AppDelegate.BackgroundSessionCompletionHandler = null; if (handler != null) { Console.WriteLine("Calling completion handler."); NSOperationQueue.MainQueue.AddOperation(() => { this.controller.TableView.ReloadData(); var alert = new UIAlertController { Message = "Selected files have been downloaded." }; alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); controller.PresentViewController(alert, true, null); // Bring up a local notification to take the user back to our app. Console.WriteLine("Posting notification."); var notif = new UILocalNotification { AlertBody = "Xamarin news: All pending files have been downloaded!" }; UIApplication.SharedApplication.PresentLocalNotificationNow(notif); // Start playback if download has finished. This is only possible if UIApplication.SharedApplication.BeginReceivingRemoteControlEvents () is called (which is done in MusicListController). if (this.firstCompletedDownload != null) { this.controller.PlayAudio(this.firstCompletedDownload); this.firstCompletedDownload = null; } // Invoke the completion handle. This will tell iOS to update the snapshot in the task manager. handler.Invoke(); }); } }