Play sound in background - c#

I have timer with an interval of 20 ms in Visual C#.
public class Global
{
//Global Vars
public static System.Media.SoundPlayer sound = new System.Media.SoundPlayer(Project.Properties.Resources.tock);
}
private void timer1_Tick(object sender, EventArgs e)
{
Global.sound.PlaySync();
}
My tock.wav plays 70 ms. My Problem is that timer1_tick is waiting till the sound is played and I don't want to short my wav file. Is there any option to play the sound in the background? It might sounds not good, but I will give it a try.

To start the SoundPlayer in a separate thread, simply call Play() instead of PlaySync().
See the documentation for reference.

Related

Play .wav file without crashing the program

I'm currently using this method to play a .wav file:
private void button5_Click(object sender, System.EventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.Song1);
player.PlaySync();
}
It's playing the .wav file for like a second, then it stops and a few seconds later after stopping, the program freezes and closes itself without showing any errors.
Did anyone experience this too and if so, (how) did you solve it?

How to play background music in C# Form FROM properties.resources simultaneously with other sounds

I'm new to C# and I'm making a mini-game. Playing sound effects is no problem, I can just use the System.Media.SoundPlayer class object to play a wav file streaming from, for example Properties.Resources.attack.wav, this is good but the background music will stop after I play the sound effect. Yes, I know there's Windows Media Player, but that uses URI, which I can't seem to add the resources directory there. I don't want to use local directory like #"D:\MyGame\backgroundmusic.wav" because I want to send it to a friend and can still hear the music. Is there any class or external sdk's that allow sound to be played simultaneously with other sounds FROM Properties.Resources? If none, what is the URI of the Resources folder inside a solution? Any help or advice would be awesome! Thank you.
edit: I already opened those links and tried them all several times. My main question is "How to play sound FROM the Resources Folder of the solution without other sounds interrupting it"
This is the concept:
private void Form_Load(object sender, EventArgs e)
{
var player = new System.Windows.Media.MediaPlayer();
player.Open(Properties.Resources.sfx_background);
player.Play();
}
private void attack()
{
SoundPlayer p1 = new SoundPlayer();
p1.Stream = Properties.Resources.sfx_sword;
p1.Play();
}
Where p1 plays synchronous with player, in which player gets the sound file from the Resources of the solution, not from a local storage of a computer.

axWindowsMediaPlayer playlist pause (c#)

I'm using axWindowsMediaPlayer playlist, and have some problem - after one video end's - next playing without any pause. I cant pause or stop it even with
private void axWindowsMediaPlayer1_MediaChange(object sender, AxWMPLib._WMPOCXEvents_MediaChangeEvent e)
{
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{axWindowsMediaPlayer1.Ctlcontrols.pause(); }}
It doesn't do anything, but must stop the player. What can I do with this? Or even disable AUTOplaying next playlist file, that's would be even better.
Thx a lot for help!
I believe you should be working with the object passed into the method and additionally the PlayStateChange event like so:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
e.Ctlcontrols.pause();
}
}
}
UPDATE: Also I would suggest that you use the axWindowsMediaPlayer1_PlayStateChanged() event rather than Media change as this may be where the issue is occurring.
The '8' is to signify "Media Ended" which if you know how to access it another way then you of course can. If you want to use WMPLib.WMPPlayState.wmppsMediaEnded I'm sure it would yeild the same result.

c# WindowsMediaPlayer End of Audio

I am playing an Audio use Windows Media Player in c# WinForms. I want to display a message at the end of the audio play back.
I have a separate Audio class for playing audio and in the play method I have written:
Player = new WMPLib.WindowsMediaPlayer();
public static void play()
{
Player.controls.play();
Player.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
}
private void Player_PlayStateChange(int NewState)
{
if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
isComplete=true;
}
public static boolean hasCompleted()
{
return isComplete;
}
here isComplete is a boolean variable initialized to false
In my form, the code for my play button is:
//Play my audio
while(!hasCompleted());
//display message
The problem is that when i click the play button, my application goes into an infinite loop.
However when i do this:
while(!hasCompleted())
MessageBox.Show("Playing");
//display message
It works fine.
Why is this happening?
I dont want to display a message while it's playing.
I tried using:
Player.currentMedia.duration
and duration string property for a timer or Thread.sleep application but the value returned by both is always 0.
I also tried giving a delay of 1-2 seconds before calling the duration property but this doesn't work as some of my audio tracks are just 2 seconds long.
It is not the best way to do so since you are in a busy waiting situation which waste a lot of CPU cycles and may not work as you wish. I would recommend you to use "Event". Here you can find a very basic example:
http://www.codeproject.com/Articles/11541/The-Simplest-C-Events-Example-Imaginable
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

How to Console.Beep while (during) mouse click down?

So, I'm trying to do a simple GUI to make music with Beeps on C#. I've been trying but I'm not sure if it's even possible to make a Console.Beep play as I hold down a button for example.
There's the normal Beep() method that just plays a short beep with a medium frequency and there's an overload Beep(int frequency, int duration). What I want to do is actually play it the whole time I'm holding the button, but obviously I can't previously state the duration.
I'm thinking this isn't possible but maybe there is a way?
This is also my first question on the site, so, hey.
You could do it like this, i just tested it and it works, and does not lock up the form while running.
private void Window_MouseDown_1(object sender, MouseButtonEventArgs e)
{
// Starts beep on background thread
Thread beepThread = new Thread(new ThreadStart(PlayBeep));
beepThread.IsBackground = true;
beepThread.Start();
}
private void PlayBeep()
{
// Play 1000 Hz for max amount of time possible
// So as long as you dont hold the mouse down for 2,147,483,647 milliseconds it should work.
Console.Beep(1000, int.MaxValue);
}
private void Window_MouseUp_1(object sender, MouseButtonEventArgs e)
{
//Aborts the beep with a new 1ms beep on mouse up which finishes the task.
Console.Beep(1000, 1);
}

Categories

Resources