从服务器下载.mp3文件并加载MediaPlayer

时间:2017-01-28 09:03:30

标签: c# android xamarin

我想从localhost服务器下载.mp3文件,但我认为唯一的问题是我要下载的目录。代码没有给出任何错误,但在if(file.Exists())中总是返回false,似乎文件没有正确下载。

下载文件:

 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();
        }

加载文件:

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();
            }

1 个答案:

答案 0 :(得分:0)

  

代码没有给出任何错误,但在if(file.Exists())总是返回false时,似乎文件没有正确下载。

Java.IO.File file = new Java.IO.File(path);,您只创建一个File实例。尚未在设备上创建该文件。您需要调用File.CreateNewFile来创建此文件,在此之前,请确保使用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())
{
  ...
}