I have a quick question.
I am using the "video" tag on my web page to display video from my server say "/videos/myvideo.mp4
Video is playing fine.
The issues is when I try to delete the video from some other code (server side)
via File.Delete(physical path) I am getting "File in use" error.
Is this a known issue?
How can I delete the video physically if someone is playing that video on his page at the same time?
I have had a similar problem when trying to rename a file, using C# on Windows 7. I do have a feeling that the problem is not as severe now as it was when I wrote that code. I don't know if some Windows 7 patch has fixed it.
Some things to try:
Ensure your OS is fully patched.
Ensure the file is Close'd before attempting the delete.
Set any variables that use the file to null.
Dispose any variables that use the file.
If you are absolutely sure nothing is holding the file open, you
might need to write a loop to keep trying the Delete, say every few
seconds. (Horrible fudge, it was the only thing that worked for me.)
In order to intercept external audio that comes in I tried
InPort ="3756"
call.set_OutputDevice(TCallIoDeviceType.callIoDeviceTypePort, InPort.ToString());
// initialized inServer
inServer = new TcpServer(InPort);
inServer.DataReceived += inServer_DataReceived;
// In dataReceived event I tried playing a wav file
args.Buffer = File.ReadAllBytes("C:\\check.wav");
inStream.Write(args.Buffer, 0, args.Buffer.Length);
Neither able to hear check.wav nor someway manipulate the incoming audio.
Any help is appreciated.
You can start from this article that implements a solution for an old version. With small changes it could work:
DirectSound Capture Using Deviare.
IIRIC, skype requires a very specific wav file format and so just trying "standard" wav files will not work - sorry, I am not a sound expert :(
I remember finding a good explanation in this forum post, but unfortunately it looks like the interesting link is broken.
Anyway, since I need this information I'll keep trying to find it elsewhere.
Best
you should see how skypefx does it, capturing stream directly from the memory buffer
here you will find skypefx source
http://skypefx.codeplex.com/SourceControl/changeset/view/c1fa20a91a97
from the source you will notice there are various effects that work directly with the memory buffer.
I'm trying to index every music file in a folder and play them at random (without repeating) with the default application on my system. For example:
ogg - itunes
mp3 - WMP
mp4 - quicktime
Are there tutorials that will help me with this? Thanks!
Well, once you have a list of files, if you want to come up with a (pseudo)-random permutation of them, what you want is a shuffle algorithm, like Knuth-Fisher-Yates.
If you have a single filename and you want to open it with the default application, you can probably use:
System.Diagnostics.Process.Start(mp3FileName);
See also the MSDN documentation on Process.Start.
This will behave equivalently to just double-clicking on the files in whatever order the shuffle comes up with. Depending on your media player, that may be good enough. However, unless you somehow know how long each media file is, you won't know how long to wait before starting the next one, and I don't think there's any way to wait for the music player to finish playing the media file without having special-case code specific to each player.
You may be able to use the Verb property of the ProcessStartInfo object to select "Enqueue" or something similar, which may do exactly what you want. Again, MSDN has some additional information on ProcessStartInfo.Verb that may be helpful to you.
I'm using System.Windows.Media.MediaPlayer to play some sounds, and I would like to load these sounds from a ZIP file. It would be nice to be able to load these files as a stream directly from the zip file instead of having to unzip to a temp directory. However, MediaPlayer.open only accepts an URI.
So, is there a way to create a URI that will reference the contents of a stream? Something like an in-memory localhost? Is there any connector from Stream to URI?
System.Media.SoundPlayer should do what you need (you can skip MediaPlayer and URIs altogether). It has a constructor that takes a stream.
Since you need to support polyphony, one approach is to PInvoke into the waveOutXXXX Windows API in order to play the sounds. Here is a good example of how to do this in C#:
https://www.codeproject.com/KB/audio-video/cswavplay.aspx
This example also has code for reading info like duration, sample rate, bits per sample etc.
If you search, you may find claims that the waveOutXXXX API can only play one sound at a time. This was true in Windows 95/98, but is not true any longer.
By the way, SoundPlayer is probably the most frustrating .NET class. The .Play() method automatically stops playback of any other sound played by your process (it might be any other sound played by any .NET app) before starting, which is why you can't do polyphony with it. It would have taken Microsoft's worst intern less than a minute to add a .DontStopJustPlay() method or a StopFirst bool parameter to the .Play() method. It might have taken him until lunch time to add a Duration property.
Although waveOutXXXX is trickier than you would want from a well-designed modern API (and using it in .NET introduces additional problems), the one unmatched advantage it has is that it's come pre-installed on every single Windows computer since Windows 95, including Windows Mobile devices. Every other option (including MediaPlayer) means somebody will always have to install something.
Is there a portable, not patent-restricted way to play compressed sound files in C# / .Net? I want to play short "jingle" sounds on various events occuring in the program.
System.Media.SoundPlayer can handle only WAV, but those are typically to big to embed in a downloadable apllication. MP3 is protected with patents, so even if there was a fully managed decoder/player it wouldn't be free to redistribute. The best format available would seem to be OGG Vorbis, but I had no luck getting any C# Vorbis libraries to work (I managed to extract a raw PCM with csvorbis but I don't know how to play it afterwards).
I neither want to distribute any binaries with my application nor depend on P/Invoke, as the project should run at least on Windows and Linux. I'm fine with bundling .Net assemblies as long as they are license-compatible with GPL.
[this question is a follow up to a mailing list discussion on mono-dev mailing list a year ago]
I finally revisited this topic, and, using help from BrokenGlass on writing WAVE header, updated csvorbis. I've added an OggDecodeStream that can be passed to System.Media.SoundPlayer to simply play any (compatible) Ogg Vorbis stream. Example usage:
using (var file = new FileStream(oggFilename, FileMode.Open, FileAccess.Read))
{
var player = new SoundPlayer(new OggDecodeStream(file));
player.PlaySync();
}
'Compatible' in this case means 'it worked when I tried it out'. The decoder is fully managed, works fine on Microsoft .Net - at the moment, there seems to be a regression in Mono's SoundPlayer that causes distortion.
Outdated:
System.Diagnostics.Process.Start("fullPath.mp3");
I am surprised but the method Dinah mentioned actually works. However, I was thinking about playing short "jingle" sounds on various events occurring in the program, I don't want to launch user's media player each time I need to do a 'ping!' sound.
As for the code project link - this is unfortunately only a P/Invoke wrapper.
I neither want to distribute any
binaries with my application nor
depend on P/Invoke, as the project
should run at least on Windows and
Linux. I'm fine with bundling .Net
assemblies as long as they are
license-compatible with GPL.
Unfortunatly its going to be impossible to avoid distributing binaries, or avoid P/Invoke. The .net class libraries use P/Invoke underneath anyway, the managed code has to communicate with the unmanage operating system API at some point, in order to do anything.
Converting the OGG file to PCM should be possible in Managed code, but because there is no Native Support for Audio in .net, you really have 3 options:
Call an external program to play the sound (as suggested earlier)
P/Invoke a C module to play the sound
P/Invoke the OS APIs to play the sound.
(4.) If you're only running this code on windows you could probably just use DirectShow.
P/Invoke can be used in a cross platform way
http://www.mono-project.com/Interop_with_Native_Libraries#Library_Names
Once you have your PCM data (using a OGG C Lib or Managed Code, something like this http://www.robburke.net/mle/mp3sharp/ of course there are licencing issues with MP3), you will need a way to play it, unfortunatly .net does not provide any direct assess to your sound card or methods to play streaming audio. You could convert the ogg files to PCM at startup, and then use System.Media.SoundPlayer, to play the wav files generated. The current method Microsoft suggests uses P/Invoke to access Sound playing API in the OS http://msdn.microsoft.com/en-us/library/ms229685.aspx
A cross platform API to play PCM sound is OpenAL and you should be able to play (PCM) sound using the c# bindings for OpenAL at www.taoframework.com, you will unfortunatly need to copy a number of DLL and .so files with your application in order for it to work when distributed, but this is, as i've explained earlier unavoidable.
Calling something which is located in 'System.Diagnostics' to play a sound looks like a pretty bad idea to me. Here is what that function is meant for:
//
// Summary:
// Starts a process resource by specifying the name of a document or application
// file and associates the resource with a new System.Diagnostics.Process component.
//
// Parameters:
// fileName:
// The name of a document or application file to run in the process.
//
// Returns:
// A new System.Diagnostics.Process component that is associated with the process
// resource, or null, if no process resource is started (for example, if an
// existing process is reused).
//
// Exceptions:
// System.ComponentModel.Win32Exception:
// There was an error in opening the associated file.
//
// System.ObjectDisposedException:
// The process object has already been disposed.
//
// System.IO.FileNotFoundException:
// The PATH environment variable has a string containing quotes.
i think you should have a look a fmod, which is the mother of all audio api
please feel free to dream about http://www.fmod.org/index.php/download#FMODExProgrammersAPI
The XNA Audio APIs work well in .net/c# applications, and work beautifully for this application. Event-based triggering, along with concurent playback of multiple sounds. Exactly what you want. Oh, and compression as well.
Well, it depends on a patent-related laws in a given country, but there is no way to write a mp3 decoder without violating patents, as far as i know. I think the best cross-platform, open source solution for your problem is GStreamer. It has c# bindings, which evolve rapidly. Using and building GStreamer on Windows is not an easy task however. Here is a good starting point. Banshee project uses this approach, but it is not really usable on windows yet (however, there are some almost-working nightly builds). FMOD is also a good alternative. Unfortunately, it is not open source and i find that its API is somehow C-styled.
There is a pure C# vorbis decoder available that is open source:
http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/
Not sure if this is still relevant. Simplest solution would be to use NAudio, which is a managed open source audio API written in C#. Another thing to try would be utilizing ffmpeg, and creating a process to ffplay.exe (the right binaries are under shared builds).
There is no way for you to do this without using something else for your play handling.
Using the System.Diagnostic will launch an external software and I doubt you want that, right? You just want X sound file to play in the background when Y happens in your program, right?
Voted up because it looks like an interesting question. :D