如何在Xamarin.Forms中获取嵌入文件的URL/路径 [英] How to get URL/path to embedded file in Xamarin.Forms

查看:1068
本文介绍了如何在Xamarin.Forms中获取嵌入文件的URL/路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法访问Xamarin.Forms项目中嵌入的.mp4文件.根据 Xamarin中的文件处理.表单文档说,

I'm having trouble being able to access .mp4 files embedded within my Xamarin.Forms project. According to the File Handling in Xamarin.Forms documentation, it says,

每个平台上文件的路径都可以通过.NET确定 标准库通过使用Environment.SpecialFolder的值 枚举作为Environment.GetFolderPath的第一个参数 方法.然后,可以将其与文件名结合使用 Path.Combine方法.

The path of the file on each platform can be determined from a .NET Standard library by using a value of the Environment.SpecialFolder enumeration as the first argument to the Environment.GetFolderPath method. This can then be combined with a filename with the Path.Combine method.

我还按照页面上的指示进行操作,这些指示告诉您确保将文件的生成操作"设置为"EmbeddedResource".最终,我试图获取文件的路径/URL,以便可以将其传递到 MediaManager CrossMediaManager.Current.Play()函数.无论我做了多少调整,这里的一些代码一直在失败.其中大部分直接取自上面链接的Xamarin文档:

I've also followed the instructions on the page that tell you to make sure you set the "Build Action" to "EmbeddedResource" for the files. Ultimately I'm trying to get the path/url of the file so that I can pass it into MediaManager's CrossMediaManager.Current.Play() function. Here's some code that has continuously failed, no matter how much I tweak it. Most of this was taken directly from the Xamarin documentation linked above:

// Gets the path to the video file.
string fileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string file = "Signing.video.banana.mp4";
string pathToFile = Path.Combine(fileDirectory, file);

// Print some info about the file and its directory
System.Diagnostics.Debug.WriteLine(pathToFile);
System.Diagnostics.Debug.WriteLine(File.Exists(pathToFile));

.mp4文件位于名为视频"的文件夹中,该文件夹直接位于签名"项目中.我试过调整文件路径,使其仅是文件,即只是文件和视频文件夹,并且尝试使用'/'而不是'.分隔目录.我总是得到的打印输出是该文件不存在.在iOS模拟器上运行时,打印出的路径总是看起来像这样:

The .mp4 file is located in a folder called "video" that's directly within the Project "Signing." I've tried adjusting the file path so that it's just the file, it's just the file and video folder, and I've tried using '/' instead of '.' to separate the directories. The printout I always get is that the file doesn't exist. The path that's printed always looks something like this when running it on an iOS emulator:

/Users/doug/Library/Developer/CoreSimulator/Devices/FD9E9045-EF6A-4ECA-9192-25CEC0E7C23C/data/Containers/Data/Application/AF92806E-C68F-45ED-814D-C383821F470A/Documents/Signing.video.banana.mp4

我已经看到一些建议说要在文件路径的前面附加"file://",以及其他类似的建议.什么都行不通.如何在Xamarin.Forms中嵌入文件并稍后检索其文件路径?

I've seen suggestions saying to append "file://" to the front of the file path, as well as other similar suggestions. Nothing works. How do I embed a file in Xamarin.Forms and later retrieve its file path?

推荐答案

您需要将程序集嵌入式资源复制到临时文件中,以便MediaManager播放它.

You will need to copy the assembly embedded resource to a temp file in order for MediaManager to play it.

使用Xamarin.Essentials,您可以获取操作系统的缓存目录,然后将资源复制到该目录并通过file://网址进行播放.

Using Xamarin.Essentials you can obtain the OS's cache directory and then copy the resource to it and play it via a file:// url.

var cacheFile = Path.Combine(FileSystem.CacheDirectory, "cached.mp4");
if (File.Exists(cacheFile))
    File.Delete(cacheFile);
using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourFullEmbeddedResourceResourceID.mp4"))
using (var file = new FileStream(cacheFile, FileMode.Create, FileAccess.Write))
{
    resource.CopyTo(file);
}
await CrossMediaManager.Current.Play(new MediaFile("file://localhost/" + cacheFile, MediaFileType.Video, ResourceAvailability.Local));

注意:就个人而言,我不会使用嵌入式资源,因为它们会使您的程序集(或本机库中的.text段)segments肿,从而占用应用程序内存,导致加载时间变慢等.基于本机包/资产的文件,然后将其复制到缓存目录.您可以使用Xamarin.Essentials通过OpenAppPackageFileAsync获取那些只读的捆绑/资产文件,然后使用该流将文件复制到缓存目录中.

Note: Personally I would not use embedded resources as they bloat your assemblies (or .text segments in native libraries) and thus consume application memory, cause slower load times, etc.., but use the native bundle/asset based files and copy those to the cache dir. You can use Xamarin.Essentials to obtain those read-only bundled/asset files via OpenAppPackageFileAsync and use that stream to copy the file to the cache dir.

这篇关于如何在Xamarin.Forms中获取嵌入文件的URL/路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆