Pop/clicking stop audio play NAudio - c#

I create ease audio player synthesizer (audio sample), at playing audio (note file audio) click key button keyboard, playing audio, key button up event, audio stop(). Audio stop() call pop/clicking audio. I try volume down, don't help and pause() don't help.
IWavePlayer player1;
AudioFileReader audio = new AudioFileReader(
PathSettings.getProgramPath()
+ "/sounds/C4.wav");
player1 = new WaveOut(WaveCallbackInfo.FunctionCallback());
player1.Init(audio);
private void Synthesizer_PreviewKeyUp(object sender, KeyEventArgs e)
{
if (Key.Z == e.Key)
{
player1.Stop();
}
}
private void Synthesizer_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Key.Z == e.Key)
{
audio.Volume = 1;
audio.CurrentTime = new TimeSpan(0);
player1.Play();
}
}

Try using something like System.Audio or sound, when you right click you might be able to see designer references.

Related

Crash when i try to play a sound as loop

I try to play a sound as loop. It starts playing fine, but when the sound ends, and it should started to play again, the program crashes.
this is my code:
private void audio_Load(object sender, EventArgs e)
{
playAudio();
}
private void playAudio()
{
SoundPlayer audio = new SoundPlayer(Properties.Resources.music);
audio.PlayLooping();
}
what am i doing wrong?
Sorry for the late response.
user2864740 that was my problem.
i changed it now so that i have only one SoundPlayer and it works perfect now:
SoundPlayer audio = new SoundPlayer(Properties.Resources.music);
private void audio_Load(object sender, EventArgs e)
{
audio.PlayLooping();
}

Play a sound, when moving over a button

I am wondering, how and if it's possible to play a simple sound, when the user moves over a button (button1) for example. I can imagine something like this:
if(button1.Touched) {// play sound }
Use this:
System.Media.SoundPlayer mediaPlayer = new System.Media.SoundPlayer(#"c:\Sound.wav");
public void button1_MouseHover(object sender, EventArgs e)
{
mediaPlayer.Play();
}

My Mute Button Is Stopping But Not Playing

SoundPlayer StartUpMusic = new SoundPlayer(Resources.Guiles_Theme);
private void MuteButton_Click(object sender, EventArgs e)
{
if (StartUpMusic.IsLoadCompleted == true)
{
StartUpMusic.Stop();
StartUpMusic.Dispose();
}
else
{
StartUpMusic.Load();
StartUpMusic.Play();
}
}
This is an event triggered when the user clicks the play button. I think my condition within the if statement is not good. I basically want the sound to be muted when the button is pressed. Then I want the sound to continue when the button is pressed and the sound is already muted. What is wrong here? You time and effort are greatly appreciated. Thank you!
Just use a boolean flag to indicate if the sound is playing or not. So something like this might work:
private boolean isPlaying = true;
private void MuteButton_Click(object sender, EventArgs e)
{
if (StartUpMusic.IsLoadCompleted)
{
if (isPlaying)
StartUpMusic.Stop();
else
StartUpMusic.Play();
isPlaying = !isPlaying;
}
}

save .waw sound into a variable

i would like to ask how can I save waw sound into a variable. In program i have 3 buttons and when a click I want to play a different sound. Now I got it like that
`SoundPlayer playDeath = new SoundPlayer(Properties.Resources.death);
playDeath.Play();`
I tried to save the audio to a variable and play but it did not work.
SoundPlayer player = new SoundPlayer ();
Bitmap sound;
sound = Properties.Resources.death;
player.Play(sound);
Is there any way to do that by clicking the button to save Variables sound. For example
SoundPlayer player = new SoundPlayer ();
private void button1_Click(object sender, EventArgs e)
{
sound = Properties.Resources.death;
player.Play(sound);
}
private void button2_Click(object sender, EventArgs e)
{
sound = Properties.Resources.levelUp;
player.Play(sound);
}
Thanks
You have to use the appropriate variable type for the resource you are working with. In case of a .wav file a Bitmap is certainly not the correct type. You probably want to use System.IO.Stream as the type:
System.IO.Stream sound = Properties.Resources.death;
player.Play(sound);

Set WindowsMediaPlayer to autorun C# Windows Form

Hi i'm building my first RPG game in windows form.
I'm currently trying to set a default background music that runs on boot and doesn't stop.
If i set the axWindowsMediaPlayer to visible and press play it runs without any problems with that simple line :
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = #"MyMusic\\ff3.mp3";
}
Its the click event but I can find any "On boot event".
I've read somewhere that the default axWindowsMediaPlayer.settings.autorun was true but just to make sure I added that line into my load event :
private void Form1_Load(object sender, EventArgs e)
axWindowsMediaPlayer1.settings.autoStart = true;
But still no sound on boot any ideas?
Why don't you use SoundPlayer Class? If you are building a game it's better this than your solution. So you can load your sound file writing this code:
using System.Media;
public SoundPlayer LoadSoundFile(string filename)
{
SoundPlayer sound = null;
try
{
sound = new SoundPlayer();
sound.SoundLocation = filename;
sound.Load();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error loading sound");
}
return sound;
}
Then you can Play() and Stop() your sound when you want.
EDIT:
In your case:
private void Form1_Load(object sender, EventArgs e)
{
LoadSoundFile(filename).Play();
}
PS: Remember that you have to convert your .mp3 files to .wav

Categories

Resources