在UPW中使用VLC解码媒体
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解码媒体的更多相关文章
- web网页中使用vlc插件播放相机rtsp流视频
可参考: 使用vlc播放器做rtsp服务器 使用vlc播放器播放rtsp视频 使用vlc进行二次开发做自己的播放器 vlc功能还是很强大的,有很多的现成的二次开发接口,不需配置太多即可轻松做客户端播放 ...
- JavaScript中URL的解码和编码
这些URI方法encodeURI.encodeURIComponent().decodeURI().decodeURIComponent()代替了BOM的escape()和unescape()方法. ...
- Android中解决图像解码导致的OOM问题
Android中解决图像解码导致的OOM问题 原文链接:http://blog.csdn.net/zjl5211314/article/details/7042017
- Java 字符编码(二)Java 中的编解码
Java 字符编码(二)Java 中的编解码 java.nio.charset 包中提供了一套处理字符编码的工具类,主要有 Charset.CharsetDecoder.CharsetEncoder. ...
- Java 字符编码(三)Reader 中的编解码
Java 字符编码(三)Reader 中的编解码 我们知道 BufferedReader 可以将字节流转化为字符流,那它是如何编解码的呢? try (BufferedReader reader = n ...
- python3中的编解码
#一个知识点是:python3中有两种字符串数据类型:str类型和 bytes类型:sty类型存储unicode数据,bytes类型存储bytes数据 #当我们在word上编辑文件的时候,数据保存之前 ...
- Javascript中Base64编码解码的使用实例
Javascript为我们提供了一个简单的方法来实现字符串的Base64编码和解码,分别是window.btoa()函数和window.atob()函数. 1 var encodedStr = win ...
- python3中编码与解码的问题
python3中编码与解码的问题 ASCII .Unicode.UTF-8 ASCII 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此 ...
- JS高级面试题思路(装箱和拆箱、栈和堆、js中sort()方法、.js中Date对象中的getMounth() 需要注意的、开发中编码和解码使用场景有哪些)
1.装箱和拆箱: 装箱:把基本数据类型转化为对应的引用数据类型的操作: var num = 123 // num var objNum = new Num(123) // object console ...
随机推荐
- nginx的access log按小时生成
1.在server或location段进行配置 if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})&q ...
- 005_FreeRTOS任务挂起和恢复
(一) (二)使用,参数是任务句柄 //key任务函数 void key_task(void *pvParameters) { u8 key; ) { key=KEY_Scan(); switch(k ...
- Excel2013下拉框选择自动填充颜色
图一写判断公式,图二选择应用范围.
- 06_检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员(使用子串对比版本)
#!/bin/bashif [ $USER == "root" ];then #或者 if [ $UID -eq 0 ];then yum -y install vsftpd ...
- 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)
原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解 By 岩之痕 目录: 一:综述 ...
- c++ 数据类型长度
#include <iostream> using namespace std; int main() { cout << "char: " << ...
- python3安装web.py
今天准备测试代理池IPProxyPool获取到ip的质量,在安装web.py的时候遇到了些问题,在此记录一下. 1.安装资料 web.py官网:http://webpy.org/ web.py的git ...
- 如何确定哪个SMB客户端/会话在Server 2008R2 Windows文件服务器上打开了特定文件?
参考: http://www.kbase101.com/question/54969.html NetworkOpenedFiles v1.25 https://www.nirsoft.net/ut ...
- 《你不知道的JavaScript(上)》笔记——作用域闭包
当函数可以记住并访问所在的词法作用域时, 就产生了闭包, 即使函数是在当前词法作用域之外执行. function wait(message) { setTimeout( function timer( ...
- python笔记2小数据池,深浅copy,文件操作及函数初级
小数据池就是在内存中已经开辟了一些特定的数据,经一些变量名直接指向这个内存,多个变量间公用一个内存的数据. int: -5 ~ 256 范围之内 str: 满足一定得规则的字符串. 小数据池: 1,节 ...