簡體   English   中英

從服務器下載.mp3文件並加載到MediaPlayer中

[英]Downloading .mp3 file from the server and load in MediaPlayer

我想從本地服務器下載.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();
            }

代碼沒有給出任何錯誤,但是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())
{
  ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM