Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[C#]I need help on something REALLY basic.

Master Summoner
Joined
May 30, 2008
Messages
559
Reaction score
64
I suck at c#, I started yesterday, basically I got this, the problem is commented in, I need to be able to use the Play and Pause buttons :p

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.IO;
using Microsoft.DirectX.AudioVideoPlayback;

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

        //dont know where to put this
        //song = new Audio(mainListBox.SelectedItem.ToString());

        public Audio song;

        private void menuFileOpen_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog open = new FolderBrowserDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                string foldername = open.SelectedPath;
                foreach (string f in Directory.GetFiles(foldername, "*.mp3"))
                {
                    mainListBox.Items.Add(f);
                }

            }
        }

        public void mainListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            song.Play();
        }

        public void audioPlayButton_Click(object sender, EventArgs e)
        {
            song.Play();
        }

        public void audioPauseButton_Click(object sender, EventArgs e)
        {
            song.Pause();
        }
    }
}

Thanks guys, il give thanks +.
 
Experienced Elementalist
Joined
Sep 4, 2009
Messages
248
Reaction score
69
You didn't declare "song" first, before you used "new".
 
Master Summoner
Joined
May 30, 2008
Messages
559
Reaction score
64
Still don't understand, can I get some more explanation?
 
Legendary Battlemage
Loyal Member
Joined
Apr 7, 2009
Messages
647
Reaction score
25
No offense, but you really shouldn't be working on something like this with only a day's worth of C# experience and not even knowing how to troubleshoot/declare a variable.

Anyways, you're getting an error because there isn't a value assigned to song.

By saying

Audio song;

is just saying that a variable song has a type of Audio, but it still doesn't have a value.

Use the new keyword to assign it a new copy of Audio.
 
Legendary Battlemage
Loyal Member
Joined
Apr 7, 2009
Messages
647
Reaction score
25
I could...but I want you to learn. :)

So here:
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Use the new keyword to assign it a new copy of Audio.
No, that's not what new does.. I'll explain what 'public Audio song' does below.. 'new' means, quite simply, a new (instance). It cannot be used on it's own, and it certainly doesn't copy anything. It creates a completely new instance of whatever supplied.

Kreain said:
song = new Audio(mainListBox.SelectedItem.ToString());
public Audio song;
Are you crazy? Switch those!
The problem I see looking at it for about a few seconds is that 'song', isn't a variable, let alone compatible for it's assigned value. Not because of 'new', because it's just not the right type. You need to put 'public Audio song' (which says the type for the 'pubic variable' song, is 'Audio'), BEFORE you can do anything with the variable, 'song'.

Get it?

Even then I'm not sure if it'll work, I don't code c#. I do know enough to say, both of you need more practice. Sorry if I come off as "harsh" or "over-the-top", but it's true.. And FYI, a little research & practice is not frowned upon.
 
Last edited:
Legendary Battlemage
Loyal Member
Joined
Apr 7, 2009
Messages
647
Reaction score
25
No, that's not what new does.. I'll explain what 'public Audio song' does below.. 'new' means, quite simply, a new (instance). It cannot be used on it's own, and it certainly doesn't copy anything.

Yes it does...
It creates a new instance BY copying the data.
 
Master Summoner
Joined
May 30, 2008
Messages
559
Reaction score
64
yeah I still kinda don't get it..., I mean I need to see an example to really understand.
 
Newbie Spellweaver
Joined
May 13, 2009
Messages
10
Reaction score
2
Yes it does...
It creates a new instance BY copying the data.
That is correct.

yeah I still kinda don't get it..., I mean I need to see an example to really understand.
You need to initialize a new instance of the Audio class. Its first parameter takes the location of your audio or video file.

Example:
Code:
Audio song = new Audio(@"C:\Documents and Settings\YourName\Desktop\music.mp3");
 
Legendary Battlemage
Loyal Member
Joined
Apr 7, 2009
Messages
647
Reaction score
25
That is correct.


You need to initialize a new instance of the Audio class. Its first parameter takes the location of your audio or video file.

Example:
Code:
Audio song = new Audio(@"C:\Documents and Settings\YourName\Desktop\music.mp3");

Exactly.
In your code, you're first assigning song a value before you declared it.
 
Master Summoner
Joined
May 30, 2008
Messages
559
Reaction score
64
you dont get it do you?
I first put
Code:
Audio song = new Audio(mainListBox.SelectedItem.ToString());

but nothing worked, play by button or double click or anything.
 
Legendary Battlemage
Loyal Member
Joined
Apr 7, 2009
Messages
647
Reaction score
25
Do you have teamviewer?
I can probably fix it for you in less than 3 minutes.
 
Legendary Battlemage
Loyal Member
Joined
Apr 7, 2009
Messages
647
Reaction score
25
like I haven't tried ^^.

You shouldn't be so rude.

It's not playing, because you're currently only pulling out the names of the files, not the full name.

So...if the song resides in

C:\Audio\blah.mp3

Right now, you're trying to pull
blah.mp3
out of no where.

Use File.FullName.
 
Newbie Spellweaver
Joined
May 13, 2009
Messages
10
Reaction score
2
You need to put them right before you play the songs:

Code:
        public void mainListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            song = new Audio(mainListBox.SelectedItem.ToString());
            song.Play();
        }

        public void audioPlayButton_Click(object sender, EventArgs e)
        {
            song = new Audio(mainListBox.SelectedItem.ToString());
            song.Play();
        }

Also you should note that if you want a full application here, you will need to check if there is a song selected or if the song is already playing in order to pause it, etc.


======================================================================================

*Edit:

You shouldn't be so rude.

It's not playing, because you're currently only pulling out the names of the files, not the full name.

So...if the song resides in

C:\Audio\blah.mp3

Right now, you're trying to pull
blah.mp3
out of no where.

Use File.FullName.


Directory.GetFiles returns a string array containing the full paths of all the matched files, the problem doesn't lie with that.
 
Last edited:
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Yes it does...
It creates a new instance BY copying the data.
Uh, no it doesn't.. Sorry. :huh:

In C#, the 'new' operator is used to create objects on the heap and invoke constructors. It can also be used as a modifier to hide an inherited member from a base class member.


The audio object is what "copies" the audio file, (if that's what it even does, I don't code in .NET framework often, so I don't have experience with this particular class/function). The audio object (what I'm calling it for now..), should actually do more than just "copy" the file, and paste into the variable... That doesn't make much sense.. I'm pretty sure it returns a readable format that can be used in a playback function/object to play the file..

I'd actually suggest looking it up.. :thumbup1:


- - - - - - -

- To the OP -

This person took a different route to audio/video in c#, and wrote a tutorial.

 
Back
Top