I'm trying to play a mp4 video in my media element in WPF.
The mp4 video is in a folder in my solution.
I tried diffrent things but the only way it works is when i put the full path to the video in the uri. What am i doing wrong?
XAML
<MediaElement x:Name="VideoDice" Grid.Row="2" Grid.Column="2" LoadedBehavior="Manual" MediaEnded="VideoDice_MediaEnded" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
C#
VideoDice.Source = new Uri("DiceMovies/Dice_2.mp4", UriKind.Relative);
VideoDice.Height = 500;
VideoDice.Width = 500;
VideoDice.Play();
In your project for the Dice_2.mp4 file it's necessary to set Copy to Output Directory property to Copy if newer. Looks like your .mp4 file does not copied to the output directory automatically.
Related
is there a nice way to do the following. Get a WPF button and a Windows.Forms.ImageList and use them together. Here is the code:
<Button Name="SOMETHING" Content="Button" Height="23" VerticalAlignment="Top" Width="75"/>
System.Windows.Forms.ImageList imgList = new System.Windows.Forms.ImageList();
string str_directory = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
this.imgList.Images.Add(System.Drawing.Image.FromFile(str_directory + "\\Resources\\AboutImage.png"));
WPFbutton.Background = imgList.Images[0];
I am tyring to get the Windows.Forms ImageList and use it on a WPF button. Is there a nice way to fix it?
There is no need for an ImageList. Just do it as shown in the code below, which assumes that the path "Resources\AboutImage.png" is relative to the application's current working directory.
Apparently you've called Path.GetDirectoryName two times to cut off the "bin\Debug\" or "bin\Release\" part of the path and thus access the image file directly from the Visual Studio project structure. This will not work when the application is deployed somewhere else. Instead, set the Build Action of the image file to Content, and Copy to Output Directory to Copy always or Copy if newer.
var path = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "AboutImage.png");
var bitmap = new BitmapImage(new Uri(path));
WPFbutton.Background = new ImageBrush(bitmap);
However, a far better approach would be to load an image resource directly. Set the Build Action to Resource and load the image by a WPF Pack URI:
var bitmap = new BitmapImage(
new Uri("pack://application:,,,/Resources/AboutImage.png"));
WPFbutton.Background = new ImageBrush(bitmap);
Besides that, you would usually set the Background in XAML, like:
<Button ...>
<Button.Background>
<ImageBrush ImageSource="/Resources/AboutImage.png"/>
</Button.Background>
</Button>
The audio is playing well when giving the local audio file as the source for the MediaElement.
<MediaElement Grid.Row="1" Stretch="Uniform" Name="Player" Margin="0,93,0,0" Source="Assets/test.mp3" />
But it is not working when try to play the remote audio file.
In the MainPage.xaml.cs
Player.Source = new Uri("http://fileraja.com/tamil/A/Alaipayuthey/Pachchai_Nirame-VmusiQ.Com.mp3", UriKind.RelativeOrAbsolute);
Please give me a solution. How to set the source for the MediaElement for a remote audio?
I Used to add MediaElement to for ex. MainPage.xaml as:
<MediaElement Name="MediaContent" AutoPlay="True"/>
and in MainPage.cs Constructor or OnNavigateTo i Use:
MediaContent.Source = new Uri("Ur Uri String Here", UriKind.Absolute);
if you don't want to auto play it in page loading just set AutoPlay to False, and play it on button click -for ex.- or any other event like that:
MediaContent.Play();
if it doesn't work try to use:
MediaFailed, MediaOpened, MediaEnded ,CurrentStateChanged .. events to keep track on why it doesn't work.
In a legacy project there are absolute path to the images,
for example:
C:/projects/LegacyProject/Project/Client/UserInterface/Images/arrow.png
Now I want to use relative path, so that every developer can use that
project, no matter where he has his copy of the sourcecode.
Is there an easy way to find out the (Resource) relative path?
How can I use it then?
At the moment I have for example:
<Image Source="C:/projects/LegacyProject/Project/Client/UserInterface/Images/arrow.png" Stretch="Fill" />
What I want is something like:
<Image Source="arrow.png" Stretch="Fill" />
Tried around with
<Image Source="pack:,,, arrow.png" Stretch="Fill" />
<Image Source="/WPF1;arrow.png"></Image>
and similar things
Put the image files into a folder (named let's say Images) in your Visual Studio project and set their build action to Resource.
Now you can simply use them in XAML like this:
<Image Source="Images/arrow.png" ... />
In code-behind you would have to write
var uri = new Uri("pack://application:,,,/Images/arrow.png");
image.Source = new BitmapImage(uri);
Add image using Source property of the Image control by clicking
then the path will be something like this:
/[project name];component/[folder name: e.g. Images]/[file name]
I have a metro app and I have added an existing file into a folder inside solution explorer. I have read the resources docs on msdn but can't get it figured out.
My sound file is located in Assets\SFX\Standard.wav, so how would I locate and play this file using MediaElement in my app from codebehind?
I am not sure why you would want to use MediaElement, perhaps you could try SoundPlayer?
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "\Assets\SFX\Standard.wav";
player.Play();
Edit with MediaElement:
<MediaElement Name="TehSoundz" IsLooping="False" AutoPlay="False" Height="0" Width="0" Source="\Assets\SFX\Standard.wav" />
Codebehind:
TehSoundz.Play();
TehSoundz.Stop();
In my c#/WPF project, I added a jpg to the resources to embed into the exe file.
Now I want to use this jpg in an image tag, something like
<xmlns:prop="clr-namespace:MyProgram.Properties"
<Image Source="{Binding Source={StaticResource prop:LogoJpg}}"
My problem: it does not work. I got no idea, how to use the image. I could use files from the hdd, but I need the image to be embedded in the exe file.
First, add the image to your project, with a Build Action of "Resource" (not EmbeddedResource). For instance, I've added an image called "logo.jpg" to a folder called Images in my main project.
Then, in XAML, you use just use that resource as follows:
<Image Source="Images\logo.jpg" />
You can also use the pack syntax for the source:
<Image Source="pack://siteoforigin:,,,/Images/logo.jpg" />
Hope this helps.