如何使用Xamarin.Forms iOS播放下载的mp3文件

时间:2018-01-10 09:22:54

标签: ios xamarin xamarin.forms xamarin.ios download-manager

我想用我将要执行的程序播放mp3文件。我可以播放文件而无需下载文件,但我无法播放我下载的文件。你能帮我解决这个问题吗?

我正在研究Xamarin.Forms项目。我通过API从互联网上下载mp3文件。但我无法弄清楚下载文件的具体注册位置。我分享了我在IOS层和PCL一侧写的代码。

注意:我可以检查下载是否成功并检查下载状态。

注意2:我使用Xam.Plugins.DownloadManager进行下载。

我在IOS层写的代码。

public void SetDownloadPath()    
{
    CrossDownloadManager.Current.PathNameForDownloadedFile = new System.Func<IDownloadFile, string>(file =>
    {
        string fileName = (new NSUrl(file.Url, false)).LastPathComponent;
        return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);

    });
}

我在ViewModel中编写的代码。

    public async Task<DownloadItem> AddBookDownload(BookInfo _bookInfo)
    {
        var downloadItem = new DownloadItem
        {
            BookName = _bookInfo.Title,
            DownloadID = _bookInfo.ID,
            DistinctionCount = _bookInfo.SectionCount 
        };
        var sections = await LibraryClient.GetBookSections(_bookInfo.ID);
        downloadItem.Sections = sections.Data;
        if (downloadItem.Sections == null || downloadItem.Sections.Count <0)
        {
            return null;
        }
        var linkFile =
            CrossDownloadManager.Current.CreateDownloadFile(downloadItem.Sections.FirstOrDefault()
                ?.GetLink()
                .ToString());
        downloadItem.DownloadedTaskList.Add(linkFile.GetHashCode(), downloadItem);


        linkFile.PropertyChanged += (sender, e) =>
        {
            // Update UI text-fields
            var downloadFile = (IDownloadFile) sender;
            switch (e.PropertyName)
            {
                case nameof(IDownloadFile.Status):
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        downloadItem.DownloadState = downloadFile.Status;
                        Debug.WriteLine("Download Status: " + downloadFile.Status);
                    });
                    break;
                case nameof(IDownloadFile.StatusDetails):
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Debug.WriteLine("Download Details: " + downloadFile.StatusDetails);
                    });
                    break;
                case nameof(IDownloadFile.TotalBytesExpected):
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Debug.WriteLine("BytesExpected" + downloadFile.TotalBytesExpected);
                    });
                    break;
                case nameof(IDownloadFile.TotalBytesWritten):
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Debug.WriteLine("BytesWritten" + downloadFile.TotalBytesWritten);
                    });
                    break;
            }

            // Update UI if download-status changed.
            if (e.PropertyName == "Status")
                switch (((IDownloadFile) sender).Status)
                {
                    case DownloadFileStatus.COMPLETED:
                        downloadItem.DownloadState = DownloadFileStatus.COMPLETED;
                        DistinctionCount = downloadItem.DistinctionCount;
                        BookName = downloadItem.BookName;
                        DownloadedBooks.Add(downloadItem);
                        NativeServices.DownloadService.SaveDownloadsItem(downloadItem);
                        NativeServices.MediaPlayerService.PlayFromFile(Path.GetFileName(CrossDownloadManager.Current.PathNameForDownloadedFile.ToString()));
                        Debug.WriteLine("Download Completed");
                        break;
                    case DownloadFileStatus.FAILED:
                        downloadItem.DownloadState = DownloadFileStatus.FAILED;
                        Debug.WriteLine("Download Failed");
                        break;
                    case DownloadFileStatus.CANCELED:
                        Device.BeginInvokeOnMainThread(() => { Debug.WriteLine("Download Cancelled"); });
                        break;
                }

            // Update UI while donwloading.
            if (e.PropertyName == "TotalBytesWritten" || e.PropertyName == "TotalBytesExpected")
            {
                var bytesExpected = ((IDownloadFile) sender).TotalBytesExpected;
                var bytesWritten = ((IDownloadFile) sender).TotalBytesWritten;

                if (bytesExpected > 0)
                {
                    var percentage = Math.Round(bytesWritten / bytesExpected * 100);
                    Device.BeginInvokeOnMainThread(() => { Debug.WriteLine("Downloading" + percentage + "%"); });
                }
            }
        };

        CrossDownloadManager.Current.Start(linkFile);
        return downloadItem;
    }

1 个答案:

答案 0 :(得分:0)

您可以在以下网址找到音频文件Xamrin表单示例:

Audio File Integration Xamarin Forms