9 Replies - 7690 Views - Last Post: 02 November 2010 - 09:18 AM Rate Topic: -----

#1 Grano  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 23-October 10

'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 07:47 AM

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..

This post has been edited by Grano: 02 November 2010 - 08:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: 'Value' should be between 'minimum' and 'maximum&#

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: 'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 07:56 AM

For future reference, you need to point out which line the error is thrown on. Otherwise we are just guessing in the dark.

It's just too hard to guess where it happened out of 350 lines.

I will say that some of your code does literally nothing. For example:
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);
}


You create an int and a timespan, and do nothing with it. This code does nothing.

Also, if I may suggest some simplification...

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 ...


Could be easily simplified to

if (tbVolume.Value == tbVolume.Minimum)
    mediaPlayer.settings.mute = true;
else
    mediaPlayer.settings.volume == tbVolume.Value * 10;

Was This Post Helpful? 1
  • +
  • -

#3 Grano  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 23-October 10

Re: 'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 08:02 AM

i've pointed where is my problem on the very last line of my post.. Thanks for your suggestion.. :)
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: 'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 08:09 AM

Well, I must be missing it, because what I see is a description of the error, and then 350 lines of code. Nothing that says which line the error actually happens on.
Was This Post Helpful? 0
  • +
  • -

#5 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: 'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 08:15 AM

it's this, but yeah I had a hard time seeing it the first time I read it...

Quote

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..



@OP - what do you think the problem is? The error seems to point at something out right. That the Value property must be with in the range of Maximum and Minimum of the object.

So first what class type is 'currentSongProgress'?

What does the documentation for the property 'Value' say?

And what are the maximum and minimum properties of 'currentSongProgress' with respect to the new value you're attempting to set? It's probably out of range, you may want to react accordingly to that, maybe modulo it around the song list (a playlist repeat), or clamp it (repeat last track), or stop (play through list once), etc.

This post has been edited by lordofduct: 02 November 2010 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#6 Grano  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 23-October 10

Re: 'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 08:24 AM

OKay.. it comes with an error in
private void timer2_Tick(object sender, EventArgs e)
{
    playedTime++;
    currentSongProgress.Value = (int)playedTime;
}



everytime the song is fully played, this code comes with error
error code
currentSongProgress.Value = (int)playedTime;

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

Properties
currentSongProgress is a track bar toolbars

OP Wants
And do you know how to make everytime one song is ended, the song next to it will play..

This post has been edited by Grano: 02 November 2010 - 08:27 AM

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: 'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 08:32 AM

The simplest answer is to check before assignment if the value is outside the valid range.

int val = (int)playedTime;
if(val > currentSongProgress.Maximum || val < currentSongProgress.Minimum)
  //error condition. Handle it
else
  //valid condition. perform assignment.


You could even test for max/min separately. You know that once you're past max, you're past play time. That could be your "trigger" to go to the next song.

This post has been edited by insertAlias: 02 November 2010 - 08:33 AM

Was This Post Helpful? 1
  • +
  • -

#8 Grano  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 23-October 10

Re: 'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 08:41 AM

could you show me how to implement it on my code? i am pretty confused.. sorry..

could you show me how to implement it on my code? i am pretty confused.. sorry..
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: 'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 08:49 AM

Not really, because I don't know what you want to happen when the value is greater than or less than the boundary. It's really simple though. First, I assign the value of playedTime to an int for convenience. Then I do a boolean test. If that value is greater than maximum, or less than minimum, then we are outside the bounds of the TrackBar. If not, you can do what you've already done, which is assign the value to the TrackBars's value property.

If, instead of doing one boolean test that involves an "OR" test, you could do two. One to check if the value is too small. This is known as a sanity check. Your program should never allow a number like that in, but you check for it anyway to prevent unhandled exceptions. If that happens, then do whatever you need to do (maybe pop up a message box.)

If that isn't the case, then you could check to see if the value is too big. If this is true, then you know you're at the end of the song, and you can do what you need to do to play the next song.

If that isn't true either, then you can just assign the value to the track bar.
Was This Post Helpful? 1
  • +
  • -

#10 Grano  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 53
  • Joined: 23-October 10

Re: 'Value' should be between 'minimum' and 'maximum&#

Posted 02 November 2010 - 09:18 AM

okay then.. :) thanks in advance..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1