0

I am working on a simple mediaplayer application. It works great but I want to add some extra features. I have added a trackbar control.How can i set trackbar length the same as the music's length ? Like if the song is halfways the trackbars halfways.This is what I have so far

 string[] files, indexed_files;
   private void button3_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Multiselect = true;
    if (ofd.ShowDialog() == DialogResult.OK) {

        files = ofd.SafeFileNames;
        indexed_files = ofd.FileNames;
        for (int i = 0; i < files.Length; i++)
        {
            listBox1.Items.Add(files[i]);
        }

    }
    button4.Enabled = true;
}



private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = indexed_files[listBox1.SelectedIndex];
    progressBar1.Maximum =(int) axWindowsMediaPlayer1.currentMedia.duration;
    axWindowsMediaPlayer1.PlayStateChange += axWindowsMediaPlayer1_PlayStateChange;



}

void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
    trackBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;

}
int index = 0;
private void button4_Click(object sender, EventArgs e)
{
    if (listBox1.Items.Count != 0) {

        axWindowsMediaPlayer1.URL = indexed_files[index];
        trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;

        index++;
        index = (index % listBox1.Items.Count);

    }

}
MRebai
  • 5,344
  • 3
  • 33
  • 52
user3733078
  • 249
  • 2
  • 11
  • How is it not working as expected? – Rodrigo Silva Jun 18 '14 at 15:24
  • Your main problem, I think, is setting the maximum value to the duration of the song. Maximum is 100 and you're most likely going way over 100 for the duration which is the number of seconds of the song. – Dean.DePue Jun 18 '14 at 15:24
  • It seems that you are also wrong to set the trackbar value to the currentPosition for the play state change event. You should use probably divide that by the total number of seconds to get the percentage (* 100) to an integer then set the value. – Dean.DePue Jun 18 '14 at 15:31

2 Answers2

0

This will bring you the desired outcome.In my example i just placed the url in the form load for demonstration purposes.The openstatechanged event its to set the trackbar maximum since you need to wait for the player to load the file,after that the code its pretty self-explanatory:

    public partial class Form1 : Form
    {
        Timer t;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = "YourUrlHere";
            t = new Timer();
            t.Interval = 1000;
            t.Tick += new EventHandler(t_Tick);
        }

        void t_Tick(object sender, EventArgs e)
        {
            trackBar1.Value = (int)this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
        }

        private void axWindowsMediaPlayer1_OpenStateChange(object sender, AxWMPLib._WMPOCXEvents_OpenStateChangeEvent e)
        {
            if (axWindowsMediaPlayer1.openState == WMPLib.WMPOpenState.wmposMediaOpen)
            {
                trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
                t.Start();
            }
        }
    }

Yes its a timer:),and probably it is best to set it bellow 1000 for reasons of delay.

terrybozzio
  • 4,424
  • 1
  • 19
  • 25
0

So you should now add a timer and insert the following code in timer Tick event handler:

trackbar.Value = this.axWindowsMediaPlayer1.ctlControls.CurrentPosition;
betrice mpalanzi
  • 1,436
  • 12
  • 16