如何在指定目录中打开媒体文件,而不将文件添加到项目中。

huangapple go评论66阅读模式
英文:

How to open a media file in the specified directory without adding the file to the project

问题

我想要在本地网络上的计算机上以完整目录打开一个文件。类似这样的:

var path = "C:/User/Folder/video.mp4";
var stream = ...;
// 代码
mediaElement.SetSource(stream, "");

我在互联网上没有找到任何方法。只有添加文件到项目的选项,但这不是我所需要的。

英文:

I want to open a file on a computer on a local network with its full directory. Something like that:

var path = "C:/User/Folder/video.mp4";
var stream = ...;
\\code
mediaElement.SetSource(stream,""); 

I haven't found any ways on the internet. There were options only to add a file to the project, but this is not what I need.

答案1

得分: 0

以下是翻译好的部分:

FileOpenPicker

第一种方法是使用FileOpenPicker。当您使用它时,它会显示一个窗口,让用户选择并打开文件。它不需要任何额外的权限,但用户需要知道文件在哪里,并自行选择文件。

代码:

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add("*");

StorageFile file = await openPicker.PickSingleFileAsync();

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
mediaPlayer.Play();

broadFileSystemAccess

第二种方法是使用名为broadFileSystemAccess的受限权限。您需要将该权限添加到您的应用程序清单文件中,如下所示:

<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
...
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>

然后,您可以使用Windows.Storage Namespace中的API通过路径直接获取文件。例如:

StorageFile file = await StorageFile.GetFileFromPathAsync("C:\\Users\\Administrator\\Desktop\\video.mp4");

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
mediaPlayer.Play();

当您使用此权限时,请确保用户在安装应用程序后已启用文件系统隐私设置。您可以在设置 > 隐私 > 文件系统中找到这些设置。

如何在指定目录中打开媒体文件,而不将文件添加到项目中。

英文:

>How to open a media file in the specified directory without adding the file to the project

There are two ways to do it. FileOpenPicker and broadFileSystemAccess

FileOpenPicker

The first one is to use FileOpenPicker. When you use it, it will show a window that lets the user choose and open files. It does not require any extra permissions but users need to know where the file is and pick the file by themself.

Code:

        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
        openPicker.FileTypeFilter.Add(&quot;*&quot;);

        StorageFile file = await openPicker.PickSingleFileAsync();

        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
        mediaPlayer.Play();

broadFileSystemAccess

The second one is to use the restricted capability called broadFileSystemAccess. You need to add the capability into your app's manifestfile like this:

    &lt;Package
...
xmlns:rescap=&quot;http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities&quot;
  IgnorableNamespaces=&quot;uap mp rescap&quot;&gt;
...
&lt;Capabilities&gt;
    &lt;rescap:Capability Name=&quot;broadFileSystemAccess&quot; /&gt;
&lt;/Capabilities&gt;

Then you could use APIs from Windows.Storage Namespace to get files directly via path. Like:

 StorageFile file = await StorageFile.GetFileFromPathAsync(&quot;C:\\Users\\Administrator\\Desktop\\video.mp4&quot;);

        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
        mediaPlayer.Play();

When you use this capability, please make sure that user has enabled the file system privacy setting after installed the app. You could find the settings in Settings > Privacy > File system.

如何在指定目录中打开媒体文件,而不将文件添加到项目中。

huangapple
  • 本文由 发表于 2023年5月8日 02:14:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定