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
Related
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;
}
}
I have embedded a media player (axWindowsMediaPlayer) in my c# winform application. It loops and plays fine. The problem is when the form where it was located will be closed or hide. It still plays the video even the form is not active. How can I stop it when the form where it is placed is not active anymore?
If you want to stop the Player when the form is minimized, you can do it by
private void Form_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
//Do your stuff
}
}
Or, you want to stop the player when ever the form is not active you may try the following code
private void Form_Deactivate(object sender, EventArgs e)
{
//Do your stuff
}
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
I need to detect whenever the MenuStrip (NOT a contextmenu) is opened and closed. The Enter and Leave events do not seem to work.
Why I need it:
I use a MenuStrip with options like "File", "Edit", "About", etc. But when the menu strip is active, and the user navigates through it using either the mouse or keyboard the other controls on the Windows Form also respond to it.
For example: I click the "Edit > Paste Special.. > Unformatted text" in my application using the mouse. But below the expanded menu item is a XNA 2d-rendering control that also detects the mouse input and does something that I don't want it to do. Now if I could just detect whenever the menu is opened I could disable/enable the appropriate controls.
Without knowing a little more about what you are doing, you can try the code below:
private void Form1_Load(object sender, EventArgs e)
{
menuStrip1.GotFocus += new EventHandler(MenuStrip1_GotFocus);
menuStrip1.LostFocus += new EventHandler(MenuStrip1_LostFocus);
}
private void MenuStrip1_GotFocus(object sender, EventArgs e)
{
textBox1.Text = "Has Focus";
}
private void MenuStrip1_LostFocus(object sender, EventArgs e)
{
textBox1.Text = "Lost Focus";
}
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
textBox1.Text = "Has Focus";
}
private void menuStrip1_MenuDeactivate(object sender, EventArgs e)
{
textBox1.Text = "Lost Focus";
}
This seems to be working for what you have described above. If you have the menu strip with tab stop on (true) then the got focus events will handle this case. If you only need to make changes if the mouse is used then Menu Active events should work.
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