Playing Video in WPF

Prasanth R A 5 Reputation points
2025-01-21T05:57:40.91+00:00

Description :

I have a WPF Project with one Main Window under main window multiple tab item each tab item i binded with user control(Separate xaml)In user control i tried to play one video using MediaElement control...

Project Setup:

Project that target Multiple frameworks like(<TargetFrameworks>net8.0-windows;net48</TargetFrameworks>)

Problem:

In net48 video is playing but with net8.0-windows it throws Uri format error/Uri is empty

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,075 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,819 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 4,200 Reputation points Microsoft Vendor
    2025-01-21T07:56:36.55+00:00

    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

    Picture1

    3.Configuring Video Files

    Picture2

    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

    Picture3

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.