C# MediaElement Video , how to add video from resources? - c#

i have the MediaElement and i want to play a video,
as i know the only way of doing so is to set this item Source
mediaElement1.Source = new Uri(fileName);
but now i have the resources file which i wanna play but i cant do so because it doesnt have a path.
so to make a long story short, i'm looking for a way to play video in the madiaElement from the resources file(without writing its byte first).

From first line in the "Remarks" section of the MediaElement docs: "When distributing media with your application, you cannot use a media file as a project resource. In your project file, you must instead set the media type to Content and set CopyToOutputDirectory to PreserveNewest or Always."
So you can't play video from a resource. You can play a file attached to a project as content.
Some alternate ways described here..
media-element-wont-play-a-video
Accessing the resources in the published application
Play embedded video resource as stream
How to play a video from WinForms resources in a axWindowsMediaPlayer?

Related

Open local files in MediaElement without using FileOpenPicker in C#

I want to play few specific files in my app using MediaElement. The files are stored in the assets folder. So I dont want to use the FileOpenPicker. I just want that when a button is clicked a file from the assets folder must be played. How can I set the source of the MediaElement to the file.
Use a Uri as the source which points to the file. Like, "Assets/MyMovie.mp4". This feature is described in better detail in the MediaElement documentation.

Copying video file from Project package to Isolated Storage during run-time

I require to play a short video in my WP8 application. Unfortunately, there is no API available for playback from MediaLibrary.
The only API I know of is as per this link:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394041(v=vs.105).aspx
The above method works only if the VideoFile is stored in Isolated Storage.
While I understand that I can upload files to Isolated Storage using some tools, I would rather prefer to package the Video file inside the xap, and then copy the file to Isolated Storage during run-time.
Can someone please guide me on copying a VideoFile included in Application project to Isolated Storage during run-time.
You can add a video as an app asset the same way as you add an image. Add video to your project and mark it as Content. The video will be packaged with the app in xap.
You accomplish this with the MediaElement control. Here is a great sample on how to use it. The basics is to add it to your xaml and set the source to a file that is has the build action set to content.
<MediaElement x:Name="VidoPlayer" Source="/Assests/MyVideo.wma" AutoPlay="True" Volume="1"/>
So as long as you have a file MyVideo.wma located in the Assests folder and the Build Action is set to Content, you're golden!

Playing music from a link to resourses

I wanted to make looped background music for my game. I'm working in Microsoft Visual Studio 2010. In C# I wrote this:
InitializeComponent();
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(#"MCStratV1.Properties.Resources.loop1.mp3");
//sp.PlayLooping();
sp.Play();
And I don't know how to do a link to resources. I'm new at C# so I don't know much about it.
Also I wrote // because I'm not sure it is correct.
There are two problems here.
First, if you look at the MSDN page for SoundPlayer you'll find that it says "Controls playback of a sound from a .wav file." So as a start you'll have to convert your mp3 to wav format.
Second, the constructor that takes a string wants the path to the wav file on disk, not the path of a resource. You'll either have to give the path to the file, or stream the file and pass the stream to another form of the constructor.

need help playing mp3 files within .NET application?

My requirements are:
- play a mp3 file on button click
- provide a option to seek to a particular time
Almost all of the tutorials/resources that I came through, used ".wav" files to play the sound. But I need to play a ".mp3" file within the application, preferably without using any 3rd party library(NAudio, BASS, etc). I expect, it can be done using the available classes of the System.Media namespace.
What is the difference between a .wav file and .mp3 file? Are they encoded in different manner, and need different media plugins to be played?
So any help regarding mp3 files would be appreciated. Thanks.
I don't believe System.Media supports MP3 files. Assuming you are using Windows, a possible approach could be to use the Windows Media Player API by adding a COM reference to "Windows Media Player" in the Add Reference dialog. Then add a using directive:
using MP = MediaPlayer;
and then instantiate the class:
MP.MediaPlayer mediaPlayer = new MP.MediaPlayer();
You can then play music files by calling:
mediaPlayer.Open("filename.mp3");
This open method seems to automatically play the media file once it is opened, however there are explicit Play() and Stop() etc methods you can use to customise this behaviour to your liking.
The XNA framework also has ability to play music files using isn't allowed to contain spaces for some annoying reason so I recommend you use the Windows Media Player API if this is intended to be used on a PC.
I hope this has put you in the right direction!

How can I play any audio file(e.g. mp3,wav) in C#?

I have a question regarding with audio files.My question is:How can I play any audio file(e.g. mp3,wav) in C#? So if I press a button,I want to play to an audio file.
How can I do that? Can you tell me how it is done with a sample code?Lastly,I have used .Net Framework 4.0.
for that first you need to load wmp.dll from your system32 folder
Then you need to add COM component
Now you can do other things(creating player)
I have a tiny project (cutted NAudio version, only playback is left and only MP3 and WAV is supported) to play MP3 and WAV files. Do you still need it? I can upload it if you need to.
A full code example on how to play waves in c# forms apps can be found here
http://msdn.microsoft.com/en-us/library/ms173187%28v=VS.100%29.aspx
You can also play wave files using C# and the XNA Framework. But that may not serve your purpose, since that is primarily for games development.

Categories