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}"));
}
}
}
}