Skip to content

Commit

Permalink
添加项目文件。
Browse files Browse the repository at this point in the history
  • Loading branch information
LUJIAN2020 committed Aug 20, 2023
1 parent 0e10b05 commit 44035f5
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 0 deletions.
25 changes: 25 additions & 0 deletions VlcAvaloniaDemo.sln
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
10 changes: 10 additions & 0 deletions VlcAvaloniaDemo/App.axaml
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>
33 changes: 33 additions & 0 deletions VlcAvaloniaDemo/App.axaml.cs
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 added VlcAvaloniaDemo/Assets/avalonia-logo.ico
Binary file not shown.
23 changes: 23 additions & 0 deletions VlcAvaloniaDemo/Program.cs
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();
}
}
62 changes: 62 additions & 0 deletions VlcAvaloniaDemo/ViewModels/MainWindowViewModel.cs
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;
}
}






}
}
8 changes: 8 additions & 0 deletions VlcAvaloniaDemo/ViewModels/ViewModelBase.cs
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
{
}
}
41 changes: 41 additions & 0 deletions VlcAvaloniaDemo/Views/MainWindow.axaml
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>
12 changes: 12 additions & 0 deletions VlcAvaloniaDemo/Views/MainWindow.axaml.cs
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();
}
}
}
27 changes: 27 additions & 0 deletions VlcAvaloniaDemo/VlcAvaloniaDemo.csproj
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>
108 changes: 108 additions & 0 deletions VlcAvaloniaDemo/VlcPlayControl/VideoView.cs
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;
}
}
}
}
18 changes: 18 additions & 0 deletions VlcAvaloniaDemo/app.manifest
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>

0 comments on commit 44035f5

Please sign in to comment.