C# - Play MP4 from Embedded Resources in Background - c#

I have added a MP4 file to the Project resources and changed the building to Embedded Resource.
Now I want to load this mp4 file from the embedded resources and play it on the form (for example in a picturebox).
I know it's possible with Windows Media Player for example but I haven't found a way to play the mp4 from embedded resources without having to extract into a folder first.
Something like that would be awesome:
axWindowsMediaPlayer1.URL = global::PlayerUI.Properties.Resources.MP4File;
But that doesn't work of course. Is there a method like that?

Related

Play multiple audio files from embedded resource (resx)

Ok so there are a couple inherent understandings
1) I know the SoundPlayer class, despite being able to load System.IO.Stream interfaces, is 100% incapable of playing more than one sound at once, despite all the multithreading and multiple-property usage.
2) I know WMPLib is the go to answer for playing multiple files. Sadly, it requires a URL, and according to MSDN, that URL cannot point to a resource. WindowsMediaPlayer class is also incapable of loading IO.Stream interfaces.
3) I saw a hack where you write out a temporary audio file to disk and have WMPlib play that, and to that I say "polite pass"
Simply put, I do not want users messing with the source audio files. I intend to distribute just the executable and not a zip containing a tonn of files. This is why I opted for the .resx
Is there not another audio player available to C# that can take an IO.Stream that is capable of playing more than one thing at a time?
My little program is a text adventure, and I intend to have a little ambient loop playing in the background while some sound effects play to reflect what's happening on the screen.

Playing Windows System Sounds with System.Media.SoundPlayer

Is it possible that the System.Media.SoundPlayer can not play Windows System Sounds found in c:\Windows\Media??
I have the code:
using (var soundPlayer =
new SoundPlayer(#"c:\Windows\Media\Landscape\Windows Notify.wav"))
{
soundPlayer.Play();
}
Yet when I run this code I get the error:
Sound API only supports playing PCM wave files.
Am I missing something? Is there a way to play these files from a WPF application? (without converting them to PCM)
The SystemSounds class contains the following predefined system sounds:
Asterisk
Beep
Exclamation
Hand
Question
So for example, to play the Stop:
System.Media.SystemSounds.Hand.Play();
All other sounds require you read the desired sound from the registry and play it with code like this:
SoundPlayer simpleSound = new SoundPlayer(#"c:\Windows\Media\Landscape\Windows Notify.wav");
This was an interesting question, but has a simple answer. After reading about this problem, I tried out your code and got the same problem, so then I searched online for a solution. While not finding an exact solution, I did find the SoundPlayer not playing any bundled windows sounds PCM wav files post here on StackOverflow that showed some code that played an audio file from the Windows\Media folder successfully.
I tried that code and it worked, so then I just had to work out why your example didn't work. I checked for any differences between the audio file that did play and your notify audio file in an audio editor, but they were both definitely WAV files.
I tried playing a different audio file from the Windows\Media\Landscape folder and got the same error. I then tried playing an audio file from a different sub folder in the Windows\Media folder and still got the same error. However, I then noticed that many of the folders in the Windows\Media folder had the same audio files in.
That got me thinking and I eventually realised that all of the audio files that are in these folders are actually in the Windows\Media folder directly. So you can play the sounds, but you just have to ignore the ones in the sub folders and play the ones from the Windows\Media folder. This will work:
SoundPlayer soundPlayer = new SoundPlayer(#"C:\Windows\Media\Windows Notify.wav");
soundPlayer.Play();
However, I can't tell you why we got that strange error, but I can only assume that the files in the folder could perhaps be some kind of links to the actual files in the Windows\Media folder and simply used by the operating system for grouping them into categories... or something similar.
"Landscape" refers to a Windows "Sound Scheme" and the wav files therein are utilized by the Windows OS. As you have already determined, those files cannot be used directly.
Comparison of the filesize of the Windows Notify.wav within the Landscape directory and the Windows Notify.wav file within the base Windows\Media directory is quite sizable; 222KB vs 25.5KB ---
If you go into the 'Sound' control panel applet and browse to 'Sounds' tab, you will see a dropdown list for "Sound Scheme:" and those additional folders within Windows\Media will be displayed in that list.
I don't have links to back this up, but after countless hours in dealing with Control Panel Sounds programmatically, I would venture to guess that the 'Sound Scheme' wav files just contain relevant data (perhaps effect data) that is consumed at runtime to play the modified versions of those sounds ... I could be quite wrong about that last part; it's just a guess. Regardless, you won't be able to use those sound scheme files directly within your code.
If you must play that "Landscape" version of the Notify wav, then I'd suggest playing the sound and saving it into a new wave file in an audio editor. You can add the wave file to your installer/deployment project to play it into the Windows\Media folder and call it directly the same way that you are already.
Since the sound seems to be in a compressed format, it has to be decompressed before you can play it using SoundPlayer. You can use Windows Audio Compression Manager to decompress the sound for playback:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd742945(v=vs.85).aspx
However, that's rather complex to implement, so I discovered a nice library that does all that for you, NAudio:
http://naudio.codeplex.com/
Using NAudio seems to be slightly more involved than using System.Media.SoundPlayer, but it also appears to offer far more functionality.

Include video for playback in xap file

I require to include a video file for playback in xap file, instead of separately uploading it in Isolated Storage.
What are my options?
Originally I had wanted to play a video file from the MediaLibrary.
Video playback of a file stored in Media Library
As I learnt, it is not possible to do so. Hence, I am exploring the option of at least including the video file inside xap file.
Kindly note that the API for video playback supports reading video files only from Isolated storage, hence I can't just include it in the project and expect it to work.
Assuming your video is located inside assets folder (in your .xap file) the following should work:
private void PlayVideo()
{
var mediaPlayerLauncher = new MediaPlayerLauncher
{
Media = new Uri("Assets/video.mp4", UriKind.Relative),
Location = MediaLocationType.Install,
Controls = MediaPlaybackControls.Pause | MediaPlaybackControls.Stop,
Orientation = MediaPlayerOrientation.Landscape
};
mediaPlayerLauncher.Show();
}
Add it as a resource as you add images to your project. Solution explorer.

how to add sound in video with.avi extention in c#.net

I'm developing the project on videowatermarking using c#.net. I'm creating the video from .bmp file.
I'm able to create the video using avi manager in .avi extension but not able to add the sound to it video is created without sound....taking sound of .wav extension
Have a look at this, may be this is useful for you:
http://www.codeproject.com/Articles/7388/A-Simple-C-Wrapper-for-the-AviFile-Library

How to play mp3 file from a different server in .NET 2.0 (c#/vb.net)?

I want to create a window application which will play an mp3 file, which will be on a different server.
For an example,
I have a URL (http://songs.mastemag.com/indian-movie-songs/L/download.php?get=2cdeb5bfb354f297e97ba6b6b3b53203&file=1/love-aaj-kal/01%20~%20Twist%20(MastiMag.com).mp3). I want to play this file from window application on my local machine without downloading this file.(with the help of streaming)
I have code that is using 'winmm.dll' and plays local mp3 file. Is there a way in which I can use this dll to play this kind of file.
You can also suggest me any other approach as well.
Take a look at NAudio
The simple way will be to use the Window Media Player Control (in hidden mode).
It can work with http urls and local (file) urls.
Howto: use it with .NET

Categories

Resources