2 songs playing at the same time - c#

I have a project I'm working on. music playing platform. i use windows media player for this but i have a problem. The music comes as a list from the database. There is a play button at the top of the lists. When I press the button, the music plays smoothly. but when I play another song, the other music doesn't stop and they both play at the same time. I need your help. I think the problem is caused by the following codes
private bool durum = false;
public void btn_koynat_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.FindForm();
frm1.player = player;
if (durum == true)
{
durum = false;
frm1.sarkislider.Value = 0;
}
if (durum==false)
{
durum = true;
int sec = Convert.ToInt32(label_sec.Text);
frm1.btn_Oynat.Enabled = true;
frm1.durum = true;
player.URL = label_sarki.Text + ".MP3";
btn_koynat.BackgroundImage = Properties.Resources.sesacik;
frm1.btn_Oynat.BackgroundImage = Properties.Resources.durdurbutonu;
frm1.sarki_isim.Text = label_sarki.Text;
frm1.sarki_sanatci.Text = label_sanatci.Text;
frm1.label_timer2.Text = label3.Text;
frm1.sarkislider.Maximum = sec;
frm1.timer1.Enabled = true;
frm1.sarkislider.Value = 0;
player.controls.play();
}
}

You can use wplayer.playState to check if your player is running.
Here is a code example, which can avoid playing two songs at the same time.
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
private void button1_Click(object sender, EventArgs e)
{
if (wplayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
wplayer.controls.stop();
wplayer.URL = comboBox1.SelectedItem.ToString();
wplayer.controls.play();
}
else
{
wplayer.URL = comboBox1.SelectedItem.ToString();
wplayer.controls.play();
}
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("1.mp3");
comboBox1.Items.Add("2.mp3");
comboBox1.Items.Add("3.mp3");
comboBox1.Items.Add("4.mp3");
comboBox1.Items.Add("5.mp3");
}
Note: I used combobox to store mp3 files. Based on my test, when I play a mp3 file and click button to play another mp3 file, the initial mp3 file was stopped and play another file.

Related

Making music stop after form is closed and play when a form is loaded

For the application that i am creating i am trying to add in music which plays when form2 is opened. which seems to work just fine, the only issue i have is when i close the form it immediately starts playing the music again. Here is my code:
private void Form2_Load(object sender, EventArgs e)
{
music(true);
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
music(false);
}
private void music(bool placeholder)
{
System.IO.Stream str = Properties.Resources.Music;
System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
if (placeholder = true)
{
snd.Play();
} else
{
snd.Stop();
}
}
if i start my application it doesnt play any music which it should since it opens on form1. When i open form2 it plays the music as it should but when i close it, it restarts the music and it just keeps playing.
It was a simple error of judgment.
if (placeholder = true)
{
snd.Play();
} else
{
snd.Stop();
}
I suggest that you modify the above code to:
if (placeholder == true)
{
snd.Play();
} else
{
snd.Stop();
}
Please comment below if it doesn't work.

C# WinForms how to use a non-static string from one void to another?

As a small beginner-ish project i'm making a simple program that can play .wav files, I've encountered an issue however where in stuck in a situation where the program gets the file path and name from one void but I need to use that string in another void, example code here:
public void ChooseFile_Click(object sender, EventArgs e)
{
OpenFile.InitialDirectory = #"C:\";
OpenFile.RestoreDirectory = true;
OpenFile.FileName = "";
OpenFile.Title = "Open .wav file";
OpenFile.Filter = "wav files (*.wav)|*.wav";
OpenFile.ShowDialog();
string fileName = OpenFile.FileName;
ChosenFileText.Text = fileName;
}
public void PlayButton_Click(object sender, EventArgs e)
{
SoundPlayer sound = new SoundPlayer(fileName);
sound.Play();
}
As you can see I need to use the fileName string with the SoundPlayer but currently I get the error:
"The name 'fileName' does not exist in the current context".
I've tried making it public and static but all I get is errors, does anyone know how I can work around this?
You have to define the variable inside the class so that everyone can access it.
string fileName="";
public void ChooseFile_Click(object sender, EventArgs e)
{
OpenFile.InitialDirectory = #"C:\";
OpenFile.RestoreDirectory = true;
OpenFile.FileName = "";
OpenFile.Title = "Open .wav file";
OpenFile.Filter = "wav files (*.wav)|*.wav";
OpenFile.ShowDialog();
fileName = OpenFile.FileName;
ChosenFileText.Text = fileName;
}
public void PlayButton_Click(object sender, EventArgs e)
{
SoundPlayer sound = new SoundPlayer(fileName);
sound.Play();
}

Change the selection in the ListBox

I add audio files to the ListBox by clicking on the Button. Then OpenFileDialog (the code below) works. I use the added files ListBox to play (the code below). I click on button_play. There is a problem. When I click on the Button for sorting using listBox1.Sorted = true. After sorting, the audio file that should be played is not played. The audio file that was in this place before sorting is played. That is, if all songs had a number, then after sorting only the names were changed, but the numbers have not changed. And when you click button_play is played by the number.
private void button_add_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
label_load.Text = list_catalog.Items.Count.ToString();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
foreach (String file in openFileDialog1.FileNames)
{
if (list_catalog.Items.Contains(Vars.GetFileName(file)))
{
}
else
{
Vars.Files.Add(file);
list_catalog.Items.Add(Vars.GetFileName(file));
hello.Visible = false;
}
}
}
private void button_play_Click(object sender, EventArgs e)
{
string current = Vars.Files[list_catalog.SelectedIndex];
Vars.CurrentTrackNumber = list_catalog.SelectedIndex;
BassLike.Play(current, BassLike.Volume);
label_time1.Text = TimeSpan.FromSeconds(BassLike.GetPosOfStream(BassLike.Stream)).ToString();
label_time2.Text = TimeSpan.FromSeconds(BassLike.GetTimeOfStream(BassLike.Stream)).ToString();
xrewind.Maximum = BassLike.GetTimeOfStream(BassLike.Stream);
xrewind.Value = BassLike.GetPosOfStream(BassLike.Stream);
timer1.Enabled = true;
}
I think this will work...
string current = list_catalog.SelectedItem as string;
Vars.CurrentTrackNumber = Vars.Files.IndexOf(current);

wplayer.controls.stop(); doesnt work

I set an int called playercount as 1 at the start and did the following..
private void button2_Click(object sender, EventArgs e)
{
playercount++;
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "Super Mario Bros.mp3";
if (playercount % 2 == 0)
{
button2.Text = "Music Toggle: ON";
wplayer.controls.play();
}
else
{
wplayer.controls.stop();
button2.Text = "Music Toggle: OFF";
}
}
For some reason the music stop doesn't seem to work. Is there something im doing wrongly ?
You are creating a new instance of WindowsMediaPlayer each time the button is clicked. This means that your stop() call will not affect the player you create on the first click which you play().
You need to create the player as a class-level variable and act on that.
You playercount logic is a bit dubious too as InvernoMuto pointed out. Is a simple bool not sufficient?
private bool musicPlaying = false;
private WMPLib.WindowsMediaPlayer wplayer = null;
private void button2_Click(object sender, EventArgs e)
{
// create player only if necessary
if(wplayer == null)
{
wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "Super Mario Bros.mp3";
}
// toggle musicPlaying
musicPlaying = !musicPlaying;
if (musicPlaying)
{
button2.Text = "Music Toggle: ON";
wplayer.controls.play();
}
else
{
wplayer.controls.stop();
button2.Text = "Music Toggle: OFF";
}
}

How to pop up camera using picture box when button click?

I make a program in C# windows form I have tons of function in my form including two datagrid view that connected to dabase and including a camera that direcly connected to my PC I use AForge dll reference to connect to the camera device I just found the tutorial on youtube and it works perfecly for me, as I stated earlier I have too many programs in one form including that camera and it went out that the camera was need to be resized to a small resolution, so I decided to make a popup button that must show the wider resolution when I click the button on my form.
this is the code for my camera.
//Camera
// get the devices name
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
comboBox1.Items.Clear();
if (videoDevices.Count == 0)
throw new ApplicationException();
DeviceExist = true;
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0; //make dafault to first cam
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("No capture device on your system");
}
}
//refresh button
private void refresh_Click(object sender, EventArgs e)
{
getCamList();
}
//toggle start and stop button
private void start_Click(object sender, EventArgs e)
{
if (start.Text == "&Start")
{
if (DeviceExist)
{
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
CloseVideoSource();
videoSource.DesiredFrameSize = new Size(160, 120);
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
lblCam.Text = "Device running...";
start.Text = "&Stop";
}
else
{
lblCam.Text = "Error: No Device selected.";
}
}
else
{
if (videoSource.IsRunning)
{
CloseVideoSource();
lblCam.Text = "Device stopped.";
start.Text = "&Start";
}
}
}
//eventhandler if new frame is ready
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
//do processing here
pictureBox1.Image = img;
}
//close the device safely
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
//prevent sudden close while device is running
private void Form1_FormClosed(object sender, FormClosingEventArgs e)
{
CloseVideoSource();
}
} }
I also posted a picture so that you have further understanding what I am talking.
as you can see at the lower right corner I have a pop up button there honestly telling you I already tried different methods but nothing works unfotunately I cannot post what I've tried because I created it yesterday and can no longer undo the codes I tried. Any Idea?
Create a new Form
Place a PictureBox on this Form
Add all methods to initialize and get the current frame to the Form (should be refactored to an own class providing an event like FrameChanged giving you the current image)
add a method like to the Form
public void ShowCamPopup(string deviceName)
{
InitializeDevice(string deviceName, int width, int height);
this.Show();
this.BringToTop();
}
You should the really consider to refactor the communication with your cam to an own class which reduces duplicate code and allows you to do performance tweaks (which you will need for sure later) at a single position of your solution.

Categories

Resources