VLC支持的格式很全,学会如何使用后,完全可以自己给自己写一个简单的万能播放器了。

源码来自github:https://github.com/kakone/VLC.MediaElement.git(想详细了解就clone自己研究下,这里只简单了解下)

安装NuGet包 libVLCX

安装NuGet包 VLC.MediaElement,并在XAML里添加引用 xmlns:vlc="using:VLC"

后台代码,选取文件并打开,选取格式为"*",表示包含所有格式后缀

先定义字符串常量

private const string FILE_TOKEN = "{1BBC4B94-BE33-4D79-A0CB-E5C6CDB9D107}";
private const string SUBTITLE_FILE_TOKEN = "{16BA03D6-BCA8-403E-B1E8-166B0020B4A7}";
private async void OpenFileAsync()
{
var file = await PickSingleFileAsync("*", FILE_TOKEN);
if (file != null)
{
media_element.MediaSource = VLC.MediaSource.CreateFromUri($"winrt://{FILE_TOKEN}");
}
}
private async Task<StorageFile> PickSingleFileAsync(string fileTypeFilter, string token)
{
var fileOpenPicker = new FileOpenPicker()
{
SuggestedStartLocation = PickerLocationId.VideosLibrary
};
fileOpenPicker.FileTypeFilter.Add(fileTypeFilter);
var file = await fileOpenPicker.PickSingleFileAsync();
if (file != null)
{
StorageApplicationPermissions.FutureAccessList.AddOrReplace(token, file);
}
return file;
}

添加外部字幕文件,格式为(.srt)

private async void OpenSubtitleFileAsync()
{
var source = media_element.MediaSource;
if (source == null)
{
return;
}
var file = await PickSingleFileAsync(".srt", SUBTITLE_FILE_TOKEN);
if (file != null)
{
media_element.MediaSource.ExternalTimedTextSources.Add(TimedTextSource.CreateFromUri($"winrt://{SUBTITLE_FILE_TOKEN}"));
}
}

完整代码

<Page
x:Class="VLCDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:VLCDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:vlc="using:VLC"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<vlc:MediaElement x:Name="media_element" Grid.Row="1" AreTransportControlsEnabled="True" HardwareAcceleration="True"> </vlc:MediaElement>
<Button x:Name="openFile_bt" Content="OpenFile" Style="{ThemeResource ButtonRevealStyle}" HorizontalAlignment="Left" CornerRadius="10" BorderThickness="1" Tapped="openFile_bt_Tapped"/>
<Button x:Name="addSubTitle_bt" Content="AddSubTitle" Style="{ThemeResource ButtonRevealStyle}" HorizontalAlignment="Left" Margin="100,0,0,0" CornerRadius="10" BorderThickness="1" Tapped="addSubTitle_bt_Tapped"/>
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using VLC;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation; // https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板 namespace VLCDemo
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class MainPage : Page
{
private const string FILE_TOKEN = "{1BBC4B94-BE33-4D79-A0CB-E5C6CDB9D107}";
private const string SUBTITLE_FILE_TOKEN = "{16BA03D6-BCA8-403E-B1E8-166B0020B4A7}";
public MainPage()
{
this.InitializeComponent();
} private void openFile_bt_Tapped(object sender, TappedRoutedEventArgs e)
{
OpenFileAsync();
} private async void OpenFileAsync()
{
var file = await PickSingleFileAsync("*", FILE_TOKEN);
if (file != null)
{
media_element.MediaSource = VLC.MediaSource.CreateFromUri($"winrt://{FILE_TOKEN}");
}
}
private async Task<StorageFile> PickSingleFileAsync(string fileTypeFilter, string token)
{
var fileOpenPicker = new FileOpenPicker()
{
SuggestedStartLocation = PickerLocationId.VideosLibrary
};
fileOpenPicker.FileTypeFilter.Add(fileTypeFilter);
var file = await fileOpenPicker.PickSingleFileAsync();
if (file != null)
{
StorageApplicationPermissions.FutureAccessList.AddOrReplace(token, file);
}
return file;
} private void addSubTitle_bt_Tapped(object sender, TappedRoutedEventArgs e)
{
OpenSubtitleFileAsync();
} private async void OpenSubtitleFileAsync()
{
var source = media_element.MediaSource;
if (source == null)
{
return;
}
var file = await PickSingleFileAsync(".srt", SUBTITLE_FILE_TOKEN);
if (file != null)
{
media_element.MediaSource.ExternalTimedTextSources.Add(TimedTextSource.CreateFromUri($"winrt://{SUBTITLE_FILE_TOKEN}"));
}
}
}
}

demo地址:https://github.com/singhwong/uwp-vlc-demo.git

在UPW中使用VLC解码媒体的更多相关文章

  1. web网页中使用vlc插件播放相机rtsp流视频

    可参考: 使用vlc播放器做rtsp服务器 使用vlc播放器播放rtsp视频 使用vlc进行二次开发做自己的播放器 vlc功能还是很强大的,有很多的现成的二次开发接口,不需配置太多即可轻松做客户端播放 ...

  2. JavaScript中URL的解码和编码

    这些URI方法encodeURI.encodeURIComponent().decodeURI().decodeURIComponent()代替了BOM的escape()和unescape()方法. ...

  3. Android中解决图像解码导致的OOM问题

    Android中解决图像解码导致的OOM问题 原文链接:http://blog.csdn.net/zjl5211314/article/details/7042017

  4. Java 字符编码(二)Java 中的编解码

    Java 字符编码(二)Java 中的编解码 java.nio.charset 包中提供了一套处理字符编码的工具类,主要有 Charset.CharsetDecoder.CharsetEncoder. ...

  5. Java 字符编码(三)Reader 中的编解码

    Java 字符编码(三)Reader 中的编解码 我们知道 BufferedReader 可以将字节流转化为字符流,那它是如何编解码的呢? try (BufferedReader reader = n ...

  6. python3中的编解码

    #一个知识点是:python3中有两种字符串数据类型:str类型和 bytes类型:sty类型存储unicode数据,bytes类型存储bytes数据 #当我们在word上编辑文件的时候,数据保存之前 ...

  7. Javascript中Base64编码解码的使用实例

    Javascript为我们提供了一个简单的方法来实现字符串的Base64编码和解码,分别是window.btoa()函数和window.atob()函数. 1 var encodedStr = win ...

  8. python3中编码与解码的问题

    python3中编码与解码的问题 ASCII .Unicode.UTF-8 ASCII 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此 ...

  9. JS高级面试题思路(装箱和拆箱、栈和堆、js中sort()方法、.js中Date对象中的getMounth() 需要注意的、开发中编码和解码使用场景有哪些)

    1.装箱和拆箱: 装箱:把基本数据类型转化为对应的引用数据类型的操作: var num = 123 // num var objNum = new Num(123) // object console ...

随机推荐

  1. mouseup([[data],fn])

    mouseup([[data],fn]) 概述 当在元素上放松鼠标按钮时,会发生 mouseup 事件. 与 click 事件不同,mouseup 事件仅需要放松按钮.当鼠标指针位于元素上方时,放松鼠 ...

  2. adb命令积累

    1. 模拟事件全部是通过input命令来实现的,首先看一下input命令的使用: (原文:http://blog.csdn.net/huiguixian/article/details/1192538 ...

  3. 【luogu1797】faebdc的烦恼-莫队

    题目背景 鸟哥(faebdc)自从虐暴NOIP2013以来依然勤奋学习,每天上各种OJ刷题,各种比赛更是不在话下.但这天他遇到了一点小小的麻烦……在一届“Orz鸟哥杯”上,题目是在是太多了!足有n道! ...

  4. GAN生成式对抗网络(二)——tensorflow代码示例

    代码实现 当初学习时,主要学习的这个博客 https://xyang35.github.io/2017/08/22/GAN-1/ ,写的挺好的. 本文目的,用GAN实现最简单的例子,帮助认识GAN算法 ...

  5. pandas入门之Series

    一.创建Series 参数 - Series (Series)是能够保存任何类型的数据(整数,字符串,浮点数,Python对象等)的一维标记数组.轴标签统称为索引. - data 参数 - index ...

  6. 基于docker的sqli-labs搭建

    一键代码: curl https://files-cdn.cnblogs.com/files/kagari/sqli-labs.sh|bash https://files-cdn.cnblogs.co ...

  7. 第04组 Beta冲刺(1)

    队名:斗地组 组长博客:地址 作业博客:Beta冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.分配展示任务 2.收集各个组员的进度 3.写博客 展示GitHub当日代码/文档 ...

  8. python中文显示乱码,已经在开头有了coding: utf-8

    乱码原因:因为你的文件声明为 utf-8,并且也应该是用 utf-8 的编码保存的源文件.但是 windows 的本地默认编码是 cp936,也就是 gbk 编码,所以在控制台直接打印 utf-8 的 ...

  9. Xshell登录Vagrant方式

    Xshell登录Vagrant方式 我上一篇文章 介绍了vagrant 如何创建虚拟机集群,在上篇文章的基础上,用xshell 登录 虚拟机发现 默认是无法使用账号密码登录root账号,只能使用vag ...

  10. JavaWeb——Get、Post请求中文乱码问题

    最近在重温JavaWeb基础内容,碰到了之前也时常遇到的中文乱码问题,想着反正是经常要处理的,不如当即就把它整理出来放在博客里,省得遇到时再去到处搜. 1. Post请求乱码的解决方案: 手工创建一个 ...