首页 文章

如何在网络中从共享文件夹中播放媒体元素中的视频

提问于
浏览
2

我想在网络中剪切文件中播放媒体元素中的视频 . 怎么办呢?

例如,我的本地IP是192.168.1.51,共享文件夹名称是'SharedFileVideo' . 我可以使用\ 192.168.1.51 \ SharedFileVideo \ video.mp4从Windows资源管理器访问此文件,但是当我在Uri中使用\ 192.168.1.51 \ SharedFileVideo或文件://192.168.1.51/SharedFileVideo/video.mp4作为元素媒体源不播放我的视频 .

如何在uwp中绑定网络中为medial元素共享的文件中的视频 .

非常感谢

2 回答

  • 3

    要在网络上的共享文件夹中播放视频文件,我们可以先获取StorageFile,然后使用SetSource方法将源设置为该文件 . 要访问共享文件夹中的文件,我们需要在应用清单中声明一些功能 .

    enter image description here

    通用命名约定(UNC)是Microsoft Windows中常用于访问共享网络文件夹的命名系统 . 有关详细信息,请参阅File access permissions .

    所以我们可以设置 Package.appxmanifest 如下:

    <Applications>
      <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="UWPApp.App">
        <uap:VisualElements DisplayName="UWPApp" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="UWPApp" BackgroundColor="transparent">
          <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png">
          </uap:DefaultTile>
          <uap:SplashScreen Image="Assets\SplashScreen.png" />
        </uap:VisualElements>
        <Extensions>
          <uap:Extension Category="windows.fileTypeAssociation">
            <uap:FileTypeAssociation Name="myvideotest">
              <uap:DisplayName>MyVideoTest</uap:DisplayName>
              <uap:SupportedFileTypes>
                <uap:FileType>.mp4</uap:FileType>
              </uap:SupportedFileTypes>
            </uap:FileTypeAssociation>
          </uap:Extension>
        </Extensions>
      </Application>
    </Applications>
    <Capabilities>
      <Capability Name="internetClient" />
      <Capability Name="privateNetworkClientServer" />
      <Capability Name="internetClientServer" />
      <uap:Capability Name="enterpriseAuthentication" />
    </Capabilities>
    

    然后在代码隐藏中,首先检索共享文件夹 .

    StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"\\192.168.1.51\SharedFileVideo");
    

    在此之后,我们可以检索视频文件并将其设置为 MediaElement 的源代码,如:

    StorageFile file = await folder.GetFileAsync("video.mp4");
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
    MediaElement.SetSource(stream, file.ContentType);
    
  • 2

    您是否尝试过将源设置为:

    \ 192.168.1.51 \ SharedFileVideo \ video.mp4

    反斜杠(\)不是正斜杠(/)并确保以双反斜杠开头

    我用我的覆盆子pi做这件事,它让他们很好

    \树莓派\薄膜\ OS1 \薄膜\ video.mp4

    别忘了在xaml中这样做你需要这个:

    <MediaElement Source="\\192.168.1.51\SharedFileVideo\video.mp4"/>
    

    或用于绑定:

    <MediaElement Source="{Binding Path=URI}"/>
    

    并在代码中:

    URI = @"\\192.168.1.51\SharedFileVideo\video.mp4";
    

    正如我所说,这对我来说很好,我不需要同步编程,我的机器和路由器似乎足够好,不会导致滞后或缓冲问题

    另外要记住的是,如果将源绑定到字符串,则有时媒体元素需要告知播放 . 如果是这样,别忘了告诉它您手动加载带有加载行为标签的视频 .

    所以xaml:

    <MediaElement Name="VideoPlayer" Source="{Binding Path=URI}" LoadedBehaviour="Manual"/>
    

    并在代码中添加:

    URI = @"\\192.168.1.51\SharedFileVideo\video.mp4";
    VideoPlayer.Play();
    

相关问题