简体   繁体   English

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

[英]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. 我想从本地服务器下载.mp3文件,但我认为的唯一问题是我要下载到的目录。 Code is not giving any errors but in if(file.Exists()) is always returning false, it seems that the file is not properly downloaded. 代码没有给出任何错误,但是if(file.Exists())始终返回false,似乎文件未正确下载。

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. 代码没有给出任何错误,但是if(file.Exists())始终返回false,似乎文件未正确下载。

By Java.IO.File file = new Java.IO.File(path); 通过Java.IO.File file = new Java.IO.File(path); , you are only creating a File instance. ,您仅创建一个File实例。 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 : 您需要调用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())
{
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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