public void Downlaod_mp3_Url(string url, string audioFileName, Xamarin.Forms.ActivityIndicator LoadingDownloadProgress, Xamarin.Forms.Label DownloadingLabel) { LoadingDownloadProgress.IsRunning = true; DownloadingLabel.IsVisible = true; // LongAlert(audioFileName + " Downloading.."); var webClient = new System.Net.WebClient(); webClient.DownloadDataCompleted += (s, e) => { var text = e.Result; // get the downloaded text string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); string localPath = System.IO.Path.Combine(documentsPath, audioFileName); Console.WriteLine(localPath); File.WriteAllBytes(localPath, text); // writes to local storage LongAlert(audioFileName + " Downloaded to path"); LoadingDownloadProgress.IsRunning = false; DownloadingLabel.IsVisible = false; }; try { var urla = new Uri(url); // give this an actual URI to an MP3 webClient.DownloadDataAsync(urla); } catch (System.Threading.Tasks.TaskCanceledException) { // LongAlert("Task Canceled!"); LoadingDownloadProgress.IsRunning = false; DownloadingLabel.IsVisible = false; return; } catch (Exception a) { //LongAlert(a.InnerException.Message); LoadingDownloadProgress.IsRunning = false; DownloadingLabel.IsVisible = false; Download_Completed = false; return; } Download_Completed = false; webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged; }
public async void Downlaod_mp3_Url(string url, string audioFileName, Xamarin.Forms.ActivityIndicator LoadingDownloadProgress, Xamarin.Forms.Label DownloadingLabel) { _audioFileName = audioFileName; LoadingDownloadProgress.IsRunning = true; DownloadingLabel.IsVisible = true; // Android.Widget.Toast.MakeText(Android.App.Application.Context, _audioFileName + " Downloading..", Android.Widget.ToastLength.Long).Show(); var webClient = new System.Net.WebClient(); //var url = new Uri("http://www.maninblack.org/demos/SitDownShutUp.mp3"); // string local = "/sdcard/Download/abc.mp3"; byte[] bytes = null; webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged; try { bytes = await webClient.DownloadDataTaskAsync(url); } catch (System.Threading.Tasks.TaskCanceledException) { // Android.Widget.Toast.MakeText(Android.App.Application.Context, "Task Canceled!", Android.Widget.ToastLength.Long).Show(); return; } catch (Exception a) { //Android.Widget.Toast.MakeText(Android.App.Application.Context,a.InnerException.Message, Android.Widget.ToastLength.Long); Download_Completed = false; return; } Java.IO.File documentsPath = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads), ""); string localFilename = documentsPath + "/" + audioFileName; //Save the Mp3 using writeAsync System.IO.FileStream fs = new System.IO.FileStream(localFilename, System.IO.FileMode.OpenOrCreate); await fs.WriteAsync(bytes, 0, bytes.Length); fs.Close(); LoadingDownloadProgress.IsRunning = false; DownloadingLabel.IsVisible = false; Download_Completed = false; }