0

i am currently work on mp3 player application, but every time the song ended, this error always pops out..

Value of '282' is not valid for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'.
Parameter name: Value

here's my code:

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.Media;
using System.IO;
using WMPLib;

namespace Thesis
{
    public partial class MainBody : Form
    {
        //Media Player
        bool finished = false;
        string opendir = "";
        double playedTime = 0;
        private bool currentSongProgressSlider { get; set; }

        //Form to Form property
        Form formParent = null;

        public MainBody(Form par)
        {
            //Form to Form Connector
            formParent = par;
            InitializeComponent();

            //Media Player Program
            tbVolume.Value = 5;
            lblVolume.Text = "Vol:5";
        }

        private void MainBody_FormClosed(object sender, FormClosedEventArgs e)
        {
            formParent.Show();
        }

        private void btnPlay1_Click(object sender, EventArgs e)
        {
            play();
        }

        private void btnStop2_Click(object sender, EventArgs e)
        {
            finished = false;
            mediaPlayer.Ctlcontrols.stop();
            StatusText.Text = "[Ended]";
            mediaPlayer.Ctlcontrols.next();
            currentSongProgress.Value = 0;
            timer2.Stop();
            btnPlay1.Enabled = true;
            currentSong.Text = mediaPlayer.currentMedia.name + "ended";
        }

        private void btnPause3_Click(object sender, EventArgs e)
        {
            finished = false;
            mediaPlayer.Ctlcontrols.pause();
            StatusText.Text = "[Paused]";
            timer2.Stop();
            btnPlay1.Enabled = true;
            currentSong.Text = mediaPlayer.currentMedia.name + "paused";
        }

        private void mediaPlayer_MediaError(object sender, AxWMPLib._WMPOCXEvents_MediaErrorEvent e)
        {
            MessageBox.Show("Error occured reading file");
        }

        private void mediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if (e.newState == (int)WMPPlayState.wmppsPlaying)
            {
                StatusText.Text = "[Playing]";
                timer2.Start();
                btnPlay1.Enabled = false;
                currentSong.Text = mediaPlayer.currentMedia.name + " playing";
            }

            else if (e.newState == (int)WMPPlayState.wmppsPaused)
            {
                if (!finished)
                {
                    StatusText.Text = "[Paused]";
                    timer2.Stop();
                    btnPlay1.Enabled = true;
                    currentSong.Text = mediaPlayer.currentMedia.name + " paused";
                }
            }

            else if (e.newState == (int)WMPPlayState.wmppsStopped)
            {
                StatusText.Text = "[Ended]";
                timer2.Stop();
                btnPlay1.Enabled = true;
                currentSong.Text = mediaPlayer.currentMedia.name + " ended";
            }

            else if (e.newState == (int)WMPPlayState.wmppsMediaEnded)
            {
                StatusText.Text = "[Ended]";
                finished = true;
                mediaPlayer.Ctlcontrols.next();
                timer2.Stop();
                btnPlay1.Enabled = true;
                currentSong.Text = mediaPlayer.currentMedia.name + " ended.";
            }
        }

        private void playButton_Click_1(object sender, EventArgs e)
        {
            finished = false;
            mediaPlayer.Ctlcontrols.play();

        }

        private void btnForward_MouseDown(object sender, MouseEventArgs e)
        {
            mediaPlayer.Ctlcontrols.fastForward();
            StatusText.Text = "[Fast Forwarding]";
        }

        public void open()
        {
            if (loadSong.ShowDialog() == DialogResult.OK)
            {
                //OpenFileDialog settings
                loadSong.InitialDirectory = "c:\\";
                loadSong.FilterIndex = 1;
                loadSong.RestoreDirectory = false;
                loadSong.CheckFileExists = false;
                //Send information to mediaplayer
                finished = false;
                mediaPlayer.URL = loadSong.FileName;
                hiddenSongDirectory.Text = loadSong.FileName;
                StatusText.Text = "[Playing]";
                fileDestination.Text = hiddenSongDirectory.Text;
                FileInfo info = new FileInfo(mediaPlayer.URL);
                PopulateTree(info.Directory.FullName);
                //Set new propertys to mediaplayer
                playinfo(mediaPlayer.URL);
                timer2.Start();
                currentSong.Text = "The current song: " + mediaPlayer.currentMedia.name + " is being played.";
                //If you open set new propertys on colors
                //Enable all buttons after song select
                btnStop2.Enabled = true;
                btnPause3.Enabled = true;
                btnPlay1.Enabled = true;
            }
            else
            {
                MessageBox.Show("Error occured");
            }
        }

        public void show()
        {
            StatusText.Visible = true;
        }

        private void btnForward_MouseHover(object sender, EventArgs e)
        {
            //Fast forward the song 
            //De-activated for crash risc
            mediaPlayer.Ctlcontrols.fastForward();
            StatusText.Text = "[Fast Forwarding]";
            timer2.Start();
        }

        public void play()
        {
            //try to play the song
            try
            {

                finished = false;
                mediaPlayer.Ctlcontrols.play();
                timer2.Start();
                StatusText.Text = "[Playing]";
                currentSong.Text = "The current song: " + mediaPlayer.currentMedia.name + " is being played.";
            }
            //if it doesnt work send error again
            catch (Exception)
            {
                MessageBox.Show("Cannot Play Music");
            }
        }

        public void PopulateTree(string dir)
        {
            opendir = dir;
            // get the information of the directory
            DirectoryInfo directory = new DirectoryInfo(dir);

            // lastly, loop through each file in the directory, and add these as nodes
            foreach (FileInfo f in directory.GetFiles())
            {

                if ((f.Name.EndsWith(".mp3") || (f.Name.EndsWith(".wma") || (f.Name.EndsWith(".wav")))))
                {
                    TreeNode t = new TreeNode(f.Name);
                    playlist2.Nodes.Add(t);
                }
            }
        }

        public void playinfo(string f)
        {
            //
            //Get information from MP3Header.cs
            //Send it to mediaplayer
            //
            MP3Header header = new MP3Header();
            header.ReadMP3Information(f);
            currentSongProgress.Minimum = 0;
            currentSongProgress.Maximum = header.intLength;
            currentSongProgress.Value = 0;
            playedTime = 0;
        }

        private void play2(string file)
        {
            //
            //If playfunction throught treeview is activated
            //Start song with play2 instead of play. Since 
            //play wont work (gives no statements).
            //
            mediaPlayer.URL = file;
            finished = false;
            mediaPlayer.Ctlcontrols.play();
            currentSongProgress.Value = 0;

            FileInfo info2 = new FileInfo(mediaPlayer.URL);

            playinfo(file);
            timer2.Start();
            currentSong.Text = "Current song: " + mediaPlayer.currentMedia.name;
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            play2(opendir + "/" + e.Node.Text);
        }

        private void trackBar1_LocationChanged(object sender, EventArgs e)
        {
            mediaPlayer.Update();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            open();
        }

        private void tbVolume_Scroll_1(object sender, EventArgs e)
        {
            //
            //If we change value of volume change function
            //
            tbVolume.Maximum = 10;
            tbVolume.Minimum = 0;


            if (tbVolume.Value == tbVolume.Minimum)
            {
                mediaPlayer.settings.mute = true;
            }

            else if (tbVolume.Value == 10)
            {
                mediaPlayer.settings.volume = 100;
            }
            else if (tbVolume.Value == 1)
            {
                mediaPlayer.settings.volume = 10;
            }
            else if (tbVolume.Value == 2)
            {
                mediaPlayer.settings.volume = 20;
            }
            else if (tbVolume.Value == 3)
            {
                mediaPlayer.settings.volume = 30;
            }
            else if (tbVolume.Value == 4)
            {
                mediaPlayer.settings.volume = 40;
            }
            else if (tbVolume.Value == 5)
            {
                mediaPlayer.settings.volume = 50;
            }
            else if (tbVolume.Value == 6)
            {
                mediaPlayer.settings.volume = 60;
            }
            else if (tbVolume.Value == 7)
            {
                mediaPlayer.settings.volume = 70;
            }
            else if (tbVolume.Value == 8)
            {
                mediaPlayer.settings.volume = 80;
            }
            else if (tbVolume.Value == 9)
            {
                mediaPlayer.settings.volume = 90;
            }

            lblVolume.Text = "Vol: " + tbVolume.Value;
        }

        private void playlist2_NodeMouseDoubleClick_1(object sender, TreeNodeMouseClickEventArgs e)
        {
            play2(opendir + "/" + e.Node.Text);
        }

        private void currentSongProgress_KeyUp(object sender, KeyEventArgs e)
        {
            play();
        }

        private void currentSongProgress_LocationChanged(object sender, EventArgs e)
        {
            mediaPlayer.Update();
        }

        private void currentSongProgress_MouseDown(object sender, MouseEventArgs e)
        {
            int SliderValue = (int)currentSongProgress.Value;
            // Overloaded constructor takes the arguments days, hours, minutes, seconds, miniseconds.
            // Create a TimeSpan with miliseconds equal to the slider value.
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            playedTime++;
            currentSongProgress.Value = (int)playedTime;
        }

        }

    }

The warning always occur in this code

private void timer2_Tick(object sender, EventArgs e)
        {
            playedTime++;
            currentSongProgress.Value = (int)playedTime;
        }

And how to play the next song right after the song ended?
Thanks for your help.. Appreciate it..

Edited by DioRani: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Momerath
0

I'll assume that currentSongProgress is a progress bar? If so, you are setting it beyond the specified maximum in your code there. When the song ends, you should shut down the timer. You should also check to make sure that the value you are setting for playedTime is within the min/max of the progress bar.

Also this code is ugly:

private void tbVolume_Scroll_1(object sender, EventArgs e)
{
    //
    //If we change value of volume change function
    //
    tbVolume.Maximum = 10;
    tbVolume.Minimum = 0;
 
 
    if (tbVolume.Value == tbVolume.Minimum)
    {
        mediaPlayer.settings.mute = true;
    }
 
    else if (tbVolume.Value == 10)
    {
        mediaPlayer.settings.volume = 100;
    }
    else if (tbVolume.Value == 1)
    {
        mediaPlayer.settings.volume = 10;
    }
    else if (tbVolume.Value == 2)
    {
        mediaPlayer.settings.volume = 20;
    }
    else if (tbVolume.Value == 3)
    {
        mediaPlayer.settings.volume = 30;
    }
    else if (tbVolume.Value == 4)
    {
        mediaPlayer.settings.volume = 40;
    }
    else if (tbVolume.Value == 5)
    {
        mediaPlayer.settings.volume = 50;
    }
    else if (tbVolume.Value == 6)
    {
        mediaPlayer.settings.volume = 60;
    }
    else if (tbVolume.Value == 7)
    {
        mediaPlayer.settings.volume = 70;
    }
        else if (tbVolume.Value == 8)
    {
        mediaPlayer.settings.volume = 80;
    }
    else if (tbVolume.Value == 9)
    {
        mediaPlayer.settings.volume = 90;
    }
 
    lblVolume.Text = "Vol: " + tbVolume.Value;
}

Change it to:

private void tbVolume_Scroll_1(object sender, EventArgs e) {
    //
    //If we change value of volume change function
    //
    tbVolume.Maximum = 10;
    tbVolume.Minimum = 0;
 
 
    if (tbVolume.Value == tbVolume.Minimum) {
        mediaPlayer.settings.mute = true;
    } else if (tbVolume.Value <= tbVolume.Maximum) {
	mediaPlayer.settings.volume = 10 * tbVolume.Value;
    }
 
    lblVolume.Text = "Vol: " + tbVolume.Value;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.