Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I am making this program that will play some songs when it opens.
But I can't get it to play the next song after the first is done.
Can any one help me?
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        string[] fileEntries = null;  //to get all the things in a folder
        bool nextsong = false; //to see when the next song should be played


        private void button1_Click_1(object sender, EventArgs e)
        {
            timer1.Start();//start the timer to see when the next song should be played

            comboBox1.Items.Clear();  

            folderBrowserDialog1.ShowDialog();
          fileEntries = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
          foreach (string filename in fileEntries)
          {
              string path1 = folderBrowserDialog1.SelectedPath;
              string path2 = "desktop.ini";
              string outpath = Path.Combine(path1, path2);
              if (filename == outpath) // so the desktop.ini don't get counted
              {
                  fileEntries[0] = null;
              }
              else
              {
                  string I = Path.GetFileName(filename);
                  comboBox1.Items.Add(I);
                  WMP.URL = fileEntries[1];
                  
              }
          }
        }

       

        private void button2_Click(object sender, EventArgs e)
        {
            // don't do anything
           
        }
        int playing = 1;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //to get the combobox playing when you press a song
            playing = comboBox1.SelectedIndex;
            playing++;
            WMP.URL = fileEntries[playing];
        }

        private void WMP_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            
            //all this is to get the next song to play but it don't work
            if (WMP.status == "stopped" && nextsong == true)
            {
                
                if (fileEntries.Length > playing)
                {
                    WMP.URL = fileEntries[playing];
                    playing++;
                }
                else
                {
                    playing = 2;
                    WMP.URL = fileEntries[1];
                    
                }
                
                nextsong = false;
                
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        
        {
            nextsong = true;
        }
    }
}

What you see here is that I take the files in a folder and try to play them I don't use the desktop.ini file because it won't work when I try to play it of couse then I put them in a combobox as a play list.
It all work but not the thing were it should play the next song.
Posted
Updated 1-Sep-11 0:12am
v10
Comments
Abhinav S 31-Aug-11 9:02am    
If you can post some code here, someone might be able to help you.

You're doing it wrong.

I know, my answer might seem vague, or even obtuse, but the quality of the question dictates the quality of the answer.
 
Share this answer
 
Comments
jackhold 31-Aug-11 9:14am    
what is it im doing wrong?
Oshtri Deka 31-Aug-11 17:13pm    
Your question doesn't have enough details. We have to guess.
#realJSOP 31-Aug-11 18:43pm    
You haven't told us yet.
jackhold 1-Sep-11 3:15am    
that's becouse i want some diffrent thing i can try. but i am gonner put it in now then :)
hi, i am a fresher of .net but i can able to understood this coding, but i need some screen shots can you send me for my reference.please send me as soon as possible.
 
Share this answer
 
Comments
jackhold 5-Sep-11 5:14am    
i havent added any thing spicial other then what you can see here and then "axewmplib" and "wmplib" thats it.... this is complicatet and if you want the projekt you can download it here http://dl.dropbox.com/u/34045248/media%20player.zip
First you should use some global flag which will indicate whether player should continue playing.

You should handle PlayStateChange event to know when to play next song.

Next is easy, use IWMPControls::next() and IWMPControls::play() methods to play next song in the list.

Play with this, you shouldn't have problems.
 
Share this answer
 
Comments
jackhold 1-Sep-11 5:03am    
what "list" are you talking about?
Oshtri Deka 2-Sep-11 18:51pm    
playlist
i got it my self.....
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        string[] fileEntries = null;
        bool nextsong;


        private void button1_Click_1(object sender, EventArgs e)
        {
            timer1.Start();
            comboBox1.Items.Clear();
            folderBrowserDialog1.ShowDialog();
          fileEntries = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
          foreach (string filename in fileEntries)
          {
              string path1 = folderBrowserDialog1.SelectedPath;
              string path2 = "desktop.ini";
              string outpath = Path.Combine(path1, path2);
              if (filename == outpath)
              {
                  fileEntries[0] = null;
              }
              else
              {
                  string I = Path.GetFileName(filename);
                  
                  comboBox1.Items.Add(I);
                  WMP.URL = fileEntries[1];
                  comboBox1.Text = Path.GetFileName(fileEntries[1]); ;
              }
              
          }

         
    

        }

       

        private void button2_Click(object sender, EventArgs e)
        {
            
           
        }
        int playing = 2;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            playing = comboBox1.SelectedIndex;
            playing++;
            WMP.URL = fileEntries[playing];
            nextsong = false;
        }


        int sikkerhed_for_musik_skrift = 1;
        private void WMP_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            
            //WMP.currentMedia.duration;

            
            if (nextsong == true && fileEntries.Length > (playing + 1) && sikkerhed_for_musik_skrift == 1 )
                {
                    
                    
                    playing++;
                    nextsong = false;
                    
                    WMP.URL = fileEntries[playing];

                }
            if (nextsong == true )
                {
                    
                    nextsong = false;
                    sikkerhed_for_musik_skrift = 0;
                    playing = 2;
                    
                    WMP.URL = fileEntries[1];
                    
                    
                }
                if (WMP.status == "Ready")
                {
                    try
                    {
                        WMPLib.IWMPControls3 controls = (WMPLib.IWMPControls3)WMP.Ctlcontrols;
                        controls.play();
                    }
                    catch
                    {

                    }
                }
                comboBox1.Text = Path.GetFileName(fileEntries[playing]);
                
                
                
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        
        {
            nextsong = true;
            sikkerhed_for_musik_skrift = 1;
        }

       

       
    }
}
 
Share this answer
 
Comments
codemonkey_007 28-Mar-19 14:46pm    
After much trial and error and much searching solved this issue with a timer... I also saw this solution posted elsewhere. It seems to be the best solution...

Does anyone know how to manage the libraries in code.. Media player has Libraries/organize/manage libraries... Is there a way to add and delete folders that appear there progamatically
Mike Bledig 16-Nov-21 4:48am    
Did you ever get this code to work? Because when I try and duplicate it, it doesn't work. Also trying to find a answer to this problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900