soundplayer

SoundPlayer causing Memory Leaks?

别来无恙 提交于 2019-12-04 03:16:14
I'm writing a basic writing app in C# and I wanted to have the program make typewriter sounds as you typed. I've hooked the KeyPress event on my RichTextBox to a function that uses a SoundPlayer to play a short wav file every time a key is pressed, however I've noticed after a while my computer slows to a crawl and checking my processes, audiodlg.exe was using 5 GIGABYTES of RAM. The code I'm using is as follows: I initialise the SoundPlayer as a global variable on program start with SoundPlayer sp = new SoundPlayer("typewriter.wav") Then on the KeyPress event I simply call sp.Play(); Does

C# - Get time from soundplayer

拥有回忆 提交于 2019-12-02 13:09:33
Is there any way to get the number of milliseconds that have passed since the soundplayer started playing? So far I'm using Environment 's tick counter by getting the start time and then the current time every frame, but sometimes that's inaccurate and sometimes it's really off. At the beginning of the code I used startTime = Environment.TickCount; and then every frame i used long elapsed = Environment.TickCount - startTime; Is there an easier way to do this like HTML5 audio's player.currentTime ? Am I missing something obvious? Can someone explain to me why my question is bad rather than just

How to know when SoundPlayer has finished playing a sound

淺唱寂寞╮ 提交于 2019-12-01 07:12:50
I am using the following code to dynamically create a frequency tone in memory and play the tone asynchronously: public static void PlayTone(UInt16 frequency, int msDuration, UInt16 volume = 16383) { using (var mStrm = new MemoryStream()) { using (var writer = new BinaryWriter(mStrm)) { const double tau = 2*Math.PI; const int formatChunkSize = 16; const int headerSize = 8; const short formatType = 1; const short tracks = 1; const int samplesPerSecond = 44100; const short bitsPerSample = 16; const short frameSize = (short) (tracks*((bitsPerSample + 7)/8)); const int bytesPerSecond =

How to know when SoundPlayer has finished playing a sound

你说的曾经没有我的故事 提交于 2019-12-01 05:17:10
问题 I am using the following code to dynamically create a frequency tone in memory and play the tone asynchronously: public static void PlayTone(UInt16 frequency, int msDuration, UInt16 volume = 16383) { using (var mStrm = new MemoryStream()) { using (var writer = new BinaryWriter(mStrm)) { const double tau = 2*Math.PI; const int formatChunkSize = 16; const int headerSize = 8; const short formatType = 1; const short tracks = 1; const int samplesPerSecond = 44100; const short bitsPerSample = 16;

Playing sounds on Console - C#

强颜欢笑 提交于 2019-11-30 23:48:31
I'm writing a Console application on C# and I want to play a sound when I display texts continuously. This is what I've done : static SoundPlayer typewriter = new SoundPlayer("typewriter"); static public void print(string str, int delay) { Thread skipThread = new Thread(skipText); typewriter.PlayLooping(); textgap = delay; foreach (char c in str) { Console.Write(c); if (textgap != 0) Thread.Sleep(textgap); } typewriter.Stop(); } typewriter.wav is imported to my project next to the .cs files and I've selected copy always . When I run this code, an error pops out when starting playing the sound

Play wav/mp3 from memory

那年仲夏 提交于 2019-11-29 06:28:58
I play mp3/wav from file to create a push effect. However on an Atom CPU based tablet PC, there is a delay when I touch the button. I'll try to play wav/mp3 from memory instead of file system. Can anyone give a code snippet or a clue? System.Media.SoundPlayer player = new System.Media.SoundPlayer(); player.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\beep-7.wav"; player.Play(); Something like this? public class MediaPlayer { System.Media.SoundPlayer soundPlayer; public MediaPlayer(byte[] buffer) { var memoryStream = new MemoryStream(buffer, true); soundPlayer = new System