-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e10b05
commit 44035f5
Showing
12 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.7.34003.232 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VlcAvaloniaDemo", "VlcAvaloniaDemo\VlcAvaloniaDemo.csproj", "{29EC4618-6A89-4A18-8237-D76A8464978F}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{29EC4618-6A89-4A18-8237-D76A8464978F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{29EC4618-6A89-4A18-8237-D76A8464978F}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{29EC4618-6A89-4A18-8237-D76A8464978F}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{29EC4618-6A89-4A18-8237-D76A8464978F}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {74A59734-984F-4633-B02C-9FB84C189E5C} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Application xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
x:Class="VlcAvaloniaDemo.App" | ||
RequestedThemeVariant="Default"> | ||
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. --> | ||
|
||
<Application.Styles> | ||
<FluentTheme /> | ||
</Application.Styles> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Avalonia; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using Avalonia.Data.Core.Plugins; | ||
using Avalonia.Markup.Xaml; | ||
using VlcAvaloniaDemo.ViewModels; | ||
using VlcAvaloniaDemo.Views; | ||
|
||
namespace VlcAvaloniaDemo | ||
{ | ||
public partial class App : Application | ||
{ | ||
public override void Initialize() | ||
{ | ||
AvaloniaXamlLoader.Load(this); | ||
} | ||
|
||
public override void OnFrameworkInitializationCompleted() | ||
{ | ||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) | ||
{ | ||
// Line below is needed to remove Avalonia data validation. | ||
// Without this line you will get duplicate validations from both Avalonia and CT | ||
BindingPlugins.DataValidators.RemoveAt(0); | ||
desktop.MainWindow = new MainWindow | ||
{ | ||
DataContext = new MainWindowViewModel(), | ||
}; | ||
} | ||
|
||
base.OnFrameworkInitializationCompleted(); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Avalonia; | ||
using System; | ||
using VlcAvaloniaDemo; | ||
|
||
namespace VlcDemo | ||
{ | ||
internal class Program | ||
{ | ||
// Initialization code. Don't use any Avalonia, third-party APIs or any | ||
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized | ||
// yet and stuff might break. | ||
[STAThread] | ||
public static void Main(string[] args) => BuildAvaloniaApp() | ||
.StartWithClassicDesktopLifetime(args); | ||
|
||
// Avalonia configuration, don't remove; also used by visual designer. | ||
public static AppBuilder BuildAvaloniaApp() | ||
=> AppBuilder.Configure<App>() | ||
.UsePlatformDetect() | ||
.WithInterFont() | ||
.LogToTrace(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using CommunityToolkit.Mvvm.Input; | ||
using LibVLCSharp.Shared; | ||
|
||
namespace VlcAvaloniaDemo.ViewModels | ||
{ | ||
public partial class MainWindowViewModel : ViewModelBase | ||
{ | ||
//Linux使用方法 | ||
//Ubuntu下运行一下这两个命令安装包 | ||
//sudo apt install libvlc-dev | ||
//sudo apt install vlc | ||
|
||
private readonly LibVLC _libVlc = new LibVLC(); | ||
public MainWindowViewModel() | ||
{ | ||
MyMediaPlayer = new MediaPlayer(_libVlc); | ||
} | ||
|
||
private MediaPlayer? myMediaPlayer; | ||
public MediaPlayer? MyMediaPlayer | ||
{ | ||
get { return myMediaPlayer; } | ||
set { SetProperty(ref myMediaPlayer, value); } | ||
} | ||
|
||
|
||
[RelayCommand] | ||
public void OptionsCommand(string content) | ||
{ | ||
switch (content) | ||
{ | ||
case "播放": | ||
{ | ||
if (MyMediaPlayer?.State == VLCState.Paused) | ||
{ | ||
MyMediaPlayer?.Play(); | ||
} | ||
else | ||
{ | ||
using var media = new Media(_libVlc, "Avalonia XPF.mp4"); | ||
MyMediaPlayer?.Play(media); | ||
} | ||
} | ||
break; | ||
case "停止": | ||
MyMediaPlayer?.Stop(); | ||
break; | ||
case "暂停": | ||
MyMediaPlayer?.Pause(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
|
||
namespace VlcAvaloniaDemo.ViewModels | ||
{ | ||
public class ViewModelBase : ObservableObject | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Window | ||
x:Class="VlcAvaloniaDemo.Views.MainWindow" | ||
xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:vlc="using:VlcAvaloniaDemo.VlcPlayControl" | ||
xmlns:vm="using:VlcAvaloniaDemo.ViewModels" | ||
Title="VlcDemo" | ||
d:DesignHeight="450" | ||
d:DesignWidth="800" | ||
x:DataType="vm:MainWindowViewModel" | ||
Icon="/Assets/avalonia-logo.ico" | ||
mc:Ignorable="d"> | ||
|
||
<Design.DataContext> | ||
<vm:MainWindowViewModel /> | ||
</Design.DataContext> | ||
|
||
<Grid Margin="5" RowDefinitions="auto,*"> | ||
<StackPanel Margin="0,0,0,5" Orientation="Horizontal"> | ||
<Button | ||
Command="{Binding OptionsCommand}" | ||
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}" | ||
Content="播放" /> | ||
<Button | ||
Margin="5,0" | ||
Command="{Binding OptionsCommand}" | ||
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}" | ||
Content="暂停" /> | ||
<Button | ||
Command="{Binding OptionsCommand}" | ||
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}" | ||
Content="停止" /> | ||
</StackPanel> | ||
|
||
<vlc:VideoView Grid.Row="1" MediaPlayer="{Binding MyMediaPlayer}" /> | ||
|
||
</Grid> | ||
|
||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Avalonia.Controls; | ||
|
||
namespace VlcAvaloniaDemo.Views | ||
{ | ||
public partial class MainWindow : Window | ||
{ | ||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<BuiltInComInteropSupport>true</BuiltInComInteropSupport> | ||
<ApplicationManifest>app.manifest</ApplicationManifest> | ||
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<AvaloniaResource Include="Assets\**" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<PackageReference Include="Avalonia" Version="11.0.4" /> | ||
<PackageReference Include="Avalonia.Desktop" Version="11.0.4" /> | ||
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.4" /> | ||
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.4" /> | ||
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.--> | ||
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.4" /> | ||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" /> | ||
<PackageReference Include="LibVLCSharp" Version="3.7.0" /> | ||
<PackageReference Include="VideoLAN.LibVLC.Windows" Version="3.0.18" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using Avalonia; | ||
using Avalonia.Controls; | ||
using Avalonia.Data; | ||
using Avalonia.Platform; | ||
using LibVLCSharp.Shared; | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace VlcAvaloniaDemo.VlcPlayControl | ||
{ | ||
/// <summary> | ||
/// Avalonia VideoView for Windows, Linux and Mac. | ||
/// </summary> | ||
public class VideoView : NativeControlHost | ||
{ | ||
private IPlatformHandle? _platformHandle = null; | ||
private MediaPlayer? _mediaPlayer = null; | ||
|
||
/// <summary> | ||
/// MediaPlayer Data Bound property | ||
/// </summary> | ||
/// <summary> | ||
/// Defines the <see cref="MediaPlayer"/> property. | ||
/// </summary> | ||
public static readonly DirectProperty<VideoView, MediaPlayer?> MediaPlayerProperty = | ||
AvaloniaProperty.RegisterDirect<VideoView, MediaPlayer?>( | ||
nameof(MediaPlayer), | ||
o => o.MediaPlayer, | ||
(o, v) => o.MediaPlayer = v, | ||
defaultBindingMode: BindingMode.TwoWay); | ||
|
||
/// <summary> | ||
/// Gets or sets the MediaPlayer that will be displayed. | ||
/// </summary> | ||
public MediaPlayer? MediaPlayer | ||
{ | ||
get { return _mediaPlayer; } | ||
set | ||
{ | ||
if (ReferenceEquals(_mediaPlayer, value)) | ||
{ | ||
return; | ||
} | ||
Detach(); | ||
_mediaPlayer = value; | ||
Attach(); | ||
} | ||
} | ||
private void Attach() | ||
{ | ||
if (_mediaPlayer == null || _platformHandle == null || !IsInitialized) | ||
return; | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
_mediaPlayer.Hwnd = _platformHandle.Handle; | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
{ | ||
_mediaPlayer.XWindow = (uint)_platformHandle.Handle; | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
_mediaPlayer.NsObject = _platformHandle.Handle; | ||
} | ||
} | ||
private void Detach() | ||
{ | ||
if (_mediaPlayer == null) | ||
return; | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
_mediaPlayer.Hwnd = IntPtr.Zero; | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
{ | ||
_mediaPlayer.XWindow = 0; | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
_mediaPlayer.NsObject = IntPtr.Zero; | ||
} | ||
} | ||
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent) | ||
{ | ||
_platformHandle = base.CreateNativeControlCore(parent); | ||
|
||
if (_mediaPlayer == null) | ||
return _platformHandle; | ||
|
||
Attach(); | ||
|
||
return _platformHandle; | ||
} | ||
protected override void DestroyNativeControlCore(IPlatformHandle control) | ||
{ | ||
Detach(); | ||
|
||
base.DestroyNativeControlCore(control); | ||
|
||
if (_platformHandle != null) | ||
{ | ||
_platformHandle = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<!-- This manifest is used on Windows only. | ||
Don't remove it as it might cause problems with window transparency and embeded controls. | ||
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests --> | ||
<assemblyIdentity version="1.0.0.0" name="VlcDemo.Desktop"/> | ||
|
||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> | ||
<application> | ||
<!-- A list of the Windows versions that this application has been tested on | ||
and is designed to work with. Uncomment the appropriate elements | ||
and Windows will automatically select the most compatible environment. --> | ||
|
||
<!-- Windows 10 --> | ||
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" /> | ||
</application> | ||
</compatibility> | ||
</assembly> |