Set WindowsMediaPlayer to autorun C# Windows Form - c#

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

Related

creating media player button from user control in main form c#

I want to play media player in main form, from user control that media player is in there. How can C call media player with buttons that I put them in main form?
private void player1_Load(object sender, EventArgs e)
{
}
private void bunifuImageButton7_Click(object sender, EventArgs e)
{
}
private void bunifuImageButton1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.bunifuMaterialTextbox1.Text = ofd.FileName;
}
}
}
Presumably, you have added a media player object to your form, and it's the COM object, axWindowsMediaPlayer (from your tags). Let's assume it has the default name of axWindowsMediaPlayer1.
I am also assuming that your bunifuImageButton7 is the Play button.
You need to load the path of your media file into the mediaplayer's URL property, and then activate its Play control, like this:
private void bunifuImageButton7_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = bunifuMaterialTextbox1.Text;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
This was easily found on the Microsoft site. You might want to visit their site and bookmark it, as it contains everything you need to know about the media player:
https://learn.microsoft.com/en-us/windows/desktop/wmp/axwindowsmediaplayer-object--vb-and-c
Documentation for all the other player controls is here:
https://learn.microsoft.com/en-us/windows/desktop/wmp/iwmpcontrols--vb-and-c

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 next file automatically using MediaPlayer Control(AxWindowsMediaPlayer)

When changing AxWindowsMediaPlayer URL in PlayStateChange Event, it doesn't start playing automatically, just changes to "Ready" state.
I have an "AxWindowsMediaPlayer" Control in my C# WinForms program. when I normally change the URL property of WindowsMediaPlayer1, it works fine and plays new mp3 file automatically.
When the song ended WindowsMediaPlayer1 State changes to Stopped and I Want next URL automatically start Playing.
I used PlayStatChange event, so when player state is Stopped, URL Will change, but Not playing automatically!
The player goes to Ready State until I press the play button on the WindowsMediaPlayer1.
Here is the Code:
private void Form1_Load(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "6.mp3"; //Works fine
}
private void button1_Click(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "4.mp3"; //Works fine. It changes the music.
}
private void WindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1) //1 is for "Stopped" State
WindowsMediaPlayer1.URL = "5.mp3";
// Here is the problem.
// URL Will change but player goes to "Ready" State
// But not in "playing" until I press the play button in control.
}
Any help would be appreciated.
As mentioned in media player documentations, you should not set the Url from event handler code. Instead you can play next file this way:
private void axWindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1)
{
this.BeginInvoke(new Action(() => {
this.axWindowsMediaPlayer1.URL = #"address of nextfile";
}));
}
}
Also as another option you can consider using a playlist.
I found this note on msdn about player.URL:
"Do not call this method from event handler code. Calling URL from an event handler may yield unexpected results."
so I tried another way to solve it and its worked.
added a timer and a bool varible to check if WindowsMediaPlayer1 is "Stopped"
Here is the solution:
public partial class Form1 : Form
{
bool nextURL = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "5.mp3";
}
private void WindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1) // 1 is consider for "Stopped" State
{
nextURL = true; // if the song ended "nextURL" flag sets to true
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (nextURL)
{
WindowsMediaPlayer1.URL = "6.mp3";
nextURL = false;
}
}

playing and stopping audio from two different buttons

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.

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);

Categories

Resources