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);
Related
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();
}
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();
}
I'm running this code for pausing the song but it obliviously doesn't work.
I can't access the variables of the first button from the second one, so I can't pause the song.
How do you think I could do that?
I'm using the naudio library cause I wanted to put the audio file as resource.
private void button3_Click(object sender, EventArgs e)
{
MemoryStream mp3file = new MemoryStream(Properties.Resources.musica1);
Mp3FileReader mp3reader = new Mp3FileReader(mp3file);
var waveOut = new WaveOut();
waveOut.Init(mp3reader);
waveOut.Play();
if (pausa)
{
waveOut.Pause();
}
}
private void button2_Click(object sender, EventArgs e)
{
pausa = true;
}
You will need to restructure your code. The problem is that in your button3_Click method, you are loading and playing your MP3, but then the function immediately terminates. The if statement won't be continuously checked, which is what I think you assume will happen. Therefore, clicking button2 will simply change the state of pausa but this doesn't affect anything.
One way would be to make all the variables (mp3file, mp3reader, and waveOut) declared at the class level, then put the rest of the code inside button3_Click into, say, your form's Load event handler.
// These variables are declared at the class level
MemoryStream mp3file;
Mp3FileReader mp3reader;
WaveOut waveOut;
...
private void Form1_Load(object sender, EventArgs e)
{
mp3file = new MemoryStream(Properties.Resources.musica1);
mp3reader = new Mp3FileReader(mp3file);
waveOut = new WaveOut();
waveOut.Init(mp3reader);
}
Now, your buttonX_Click functions can look like this (assuming button3 is your Play button and button2 is your Pause button):
private void button3_Click(object sender, EventArgs e)
{
waveOut.Play();
}
private void button2_Click(object sender, EventArgs e)
{
waveOut.Pause();
}
Because waveOut is declared at the class level, both buttons have access to it, and can therefore play and pause at will. You don't need pausa anymore, unless of course you need to know the playing state elsewhere in the program.
I am trying to create a series of buttons, each play a sound. This sound is retrieved from an OpenFileDialog function. However, I have encountered the issue of one sound being assigned to all of the buttons. I know why this occurring, but I am unsure of how to resolve the issue. Basically, I began by assigning the same algorithms to each button:
openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = openFileDialog.FileName;
}
And:
soundPlayer = new SoundPlayer(fileName);
soundPlayer.Play();
Unfortunately, this was extremely ugly and so I decided to put each algorithm in to a method and just call the methods to their respective buttons. Like so:
public void openDialog()
{
openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = openFileDialog.FileName;
}
}
private void button27_Click(object sender, EventArgs e)
{
openDialog();
}
public void playDialog()
{
soundPlayer = new SoundPlayer(fileName);
soundPlayer.Play();
}
private void button1_Click(object sender, EventArgs e)
{
playDialog();
}
However, because openDialog() calls the same variable which receives the file name, each of the buttons calling openDialog() is using the same variable and so playing the same sound.
You have to make the fileName "part" of the Button. You can do it by either:
Using the Tag property of a button and cast to string when retrieving
Create a subclass of a Button called SoundButton and add FileName property of type string
Make a pick.
For example, using a Tag:
public void playDialog(string fileName)
{
soundPlayer = new SoundPlayer(fileName);
soundPlayer.Play();
}
private void button1_Click(object sender, EventArgs e)
{
playDialog((sender as Button).Tag as string);
}
You can make a list of sounds and then play it in a loop one by one:
Creating the list:
List<string> soundsList = new List<string>();
Adding to the list:
sounds.Add(openFileDialog.FileName);
Playing sounds:
foreach(string sound in soundsList)
{
soundPlayer = new SoundPlayer(sound);
soundPlayer.Play();
}
My answer of course is assuming you keep an order of first adding all the sounds you want and then playing them all. You should of course also need to add validation to check that the user has given you a correct sound to add to the list.
EDIT:
After reading your comment, you can also add a sound to Tag property of the button. Then when you want to play a sound of a specific button, you can just play whatever is inside that property of the button.
For example you can override the Click event like this:
private void button_Click(object sender, EventArgs e)
{
string soundFile = (sender as Button).Tag as string;
playDialog(soundFile);
}
This way all sounds are a "part" of the button
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