简体   繁体   中英

Downloading .mp3 file from the server and load in MediaPlayer

I want to download a .mp3 file from the localhost server, but the only problem I think is the directory that I am downloading to. Code is not giving any errors but in if(file.Exists()) is always returning false, it seems that the file is not properly downloaded.

Downloading the file:

 if (isConnectedToInternet())
        {
            using (var client = new WebClient())
            {
                int numberFile = 1;
                ProgressDialog pd = new ProgressDialog(Activity);
                pd.SetCancelable(true);
                pd.SetMessage("Pleasy wait for files to be downloaded... 0/16");
                pd.Show();
                client.DownloadFileCompleted += (o, s) => {
                    Toast.MakeText(Activity, "Download file completed.", ToastLength.Long).Show();
                };
                try
                {


                        client.DownloadFileCompleted += (o, s) => {
                            if (numberFile == 1)
                            {
                                pd.Cancel();
                            }
                        };

                        string appDataDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
                        string filePath = soundListViewAdapter.GetItemAtPosition(e.Position).path1;
                        if (!Directory.Exists(appDataDir))
                        {
                            Directory.CreateDirectory(appDataDir);
                        }

                        //I have to do .Remove(0,1) because filePath starts with the '/'

                        string path = Path.Combine(appDataDir, filePath.Remove(0, 1));

                        Toast.MakeText(Activity, path, ToastLength.Long).Show();
                        System.Uri url = new System.Uri(server + "rpad/api" + soundListViewAdapter.GetItemAtPosition(e.Position).path1);
                        client.DownloadFileAsync(url, path);


                }
                catch
                {
                    Toast.MakeText(Activity, "Files are not downloaded", ToastLength.Long);
                }

            }
        }
        else
        {
            Toast.MakeText(Activity, "No connection", ToastLength.Long).Show();
        }

Loading the file:

m1 = new MediaPlayer();

            string appDataDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
            string filePath = prefs.GetString("path1", "empty");
            string path = Path.Combine(appDataDir, filePath.Remove(0, 1));
            Java.IO.File file = new Java.IO.File(path);
            if (file.Exists())
            {
                FileInputStream fileStream = new FileInputStream(file);
                m1.SetDataSource(fileStream.FD);
                m1.Prepare();
                m1.Start();
            }

Code is not giving any errors but in if(file.Exists()) is always returning false, it seems that the file is not properly downloaded.

By Java.IO.File file = new Java.IO.File(path); , you are only creating a File instance. The file hasn't been created on the device. You need to call File.CreateNewFile to create this file, and before that, make sure all the parent folders are created by using Directory.CreateDirectory :

string appDataDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
string filePath = "Test/empty/abc.txt";
string parentPath = Path.Combine(appDataDir, "Test/empty");
string path = Path.Combine(appDataDir, filePath);
Java.IO.File file = new Java.IO.File(path);
Directory.CreateDirectory(parentPath);//make sure the parent directory is created
file.CreateNewFile();//create the file
if (file.Exists())
{
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM