Hi, @Prasanth R A. Welcome to Microsoft Q&A
You could refer to my example of successfully playing the video.
1.Create a WPF Application (.Net 8) project.
2.Adding video files and UserControl
3.Configuring Video Files
4.Configure in .csproj
file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net8.0-windows;net48</TargetFrameworks>
<UseWPF>true</UseWPF>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0-windows'">
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net48'">
<Nullable>disable</Nullable>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<Content Include="media\Test1.mp4">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="media\Test2.mp4">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
5.Writing Code
MainWindow.xaml
<Grid>
<TabControl>
<TabItem Header="Video">
<local:UserControl1></local:UserControl1>
</TabItem>
<TabItem Header="OtherTab"></TabItem>
</TabControl>
</Grid>
UserControl1.xaml
<Grid Loaded="Grid_Loaded">
<MediaElement x:Name="MyMediaElement" Width="450" Height="250" />
</Grid>
UserControl1.xaml.cs
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
#if NET8_0
MyMediaElement.Source = new Uri("media/Test1.mp4", UriKind.Relative);
//MyMediaElement.Source = new Uri("The absolute URL of your video", UriKind.Absolute);
//MyMediaElement.Source = new Uri("The web address of your video");
#else
MyMediaElement.Source = new Uri("media/Test2.mp4",UriKind.Relative);
#endif
}
}
6.Switching Environments
In my test, .NET8, relative address, absolute address, and URL could all be played
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.