Click here to Skip to main content
15,888,069 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm making a really simple audio player


There is a button which opens an OpenFileDialog to select an MP3 fileto input into a listBox, which works fine, but when I try to play an audio file in the list using this code on a button:
C#
SoundPlayer sound = new SoundPlayer(listBox1.SelectedIndex.ToString());
sound.Play();


I get an error saying there is not a file in that place, so I think it's because the backslash has to be \\ instead of \ in the Soundplayer, but the FileName from openFileDialog only uses \ because that's how it is in windows explorer.
I tried to string.replace \ with \\ but did not work it still complained (I even tried replace \\ with \\ to stop the exit thing it does)

Basically all I need is:

A way to make a button play a sound file by getting the name from a listbox

If I am unclear please ask me to rephrase, I am learning still so try explain as simple as possible.
Thank you!
Posted
Updated 26-Jan-12 14:11pm
v3

1 solution

In the data fields like the text box or a file dialog, all path names are read as is, with not backslash escaping or other foolishness. Such escaping is only needed in source code string literals, due to C, C++ or C# syntax.

However, in the past I saw a lot of really brain-damage C and C++ software where such escapes present even in raw data. You should not see anything like that in .NET applications, unless… idiocy is hard to cure, you know. :-)
You code is not the case anyway, it looks like.

The reason of your problem is somewhere else. If you still did not find out, you can run it under debugger or reproduce the problem on a really short code sample which you can post here (using "Improve question" above).

—SA
 
Share this answer
 
v3
Comments
Joel Whatley- 26-Jan-12 16:45pm    
Oke =) thanks, but can you help me with making it work?
I'm really new to programming so I don't really understand properly with why it's giving an error.

This is a button to use the OpenFileDialog:
ofd.Filter = "mp3 Files (*.mp3)|*.mp3";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK && ofd.FileName.Length > 0)
{
string name = ofd.FileName;
listBox1.Items.Add(name);
}

then how is it possible (using a button) to play that file with SoundPlayer?

For example the listBox will have the filename as C:\User\Joel\Music\song.mp3

Please help =)
Sergey Alexandrovich Kryukov 26-Jan-12 21:01pm    
Did you first try to play with newSoundPlayer(name), where the name is your variable above? Without the list box in the middle, where you make a mistake pointed out by Marcus?

After all, I told you do use Debugger? Did you do it? This is the first thing you should do in the case of the smallest doubt about your run-time.
--SA
Joel Whatley- 26-Jan-12 16:52pm    
Apparently this doesn't work


SoundPlayer sound = new SoundPlayer(listBox1.SelectedIndex.ToString());
sound.Play();
Sergey Alexandrovich Kryukov 26-Jan-12 21:01pm    
Sigh...
Joel Whatley- 26-Jan-12 17:12pm    
Ok I did what you said, and it works when I read it from a label:
string name;

label3.Text = name;
SoundPlayer sound = new SoundPlayer(name);
sound.Play();

now it works, but not with the listBox1.SelectedIndex.ToString() way, how come?

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