Click here to Skip to main content
14,326,080 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a web form in which i have requirement to browse an audio file using file upload control,and then a button to upload and play this file to folder on the server,from there i get the path to it soundplayer,
My problem is that when i pass path in this way its work fine
SoundPlayer playthewavfile = new SoundPlayer(@"G:\backup\LTM.CTP.WEBFORM\LTM.CTP.WEBFORM\Audio\q1.wav");

but i want to give the path like this
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Forms/Upload/" + fileName));
                string PATH = System.IO.Path.Combine(Server.MapPath("~/Forms/Upload"), FileUpload5.PostedFile.FileName);
SoundPlayer playthewavfile = new SoundPlayer(PATH);
 playthewavfile.Play();

here its give exception of invalid path?

What I have tried:

protected void btnbrowse_Click(object sender, EventArgs e)
{
string fileName = System.IO.Path.GetFileName(FileUpload5.PostedFile.FileName);
if (fileName != null)
{
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Forms/Upload/" + fileName));
string PATH = System.IO.Path.Combine(Server.MapPath("~/Forms/Upload"), FileUpload5.PostedFile.FileName);
string Path = Server.MapPath(FileUpload5.FileName);
SoundPlayer playthewavfile = new SoundPlayer(PATH);
playthewavfile.SoundLocation = Path.ToString();
playthewavfile.SoundLocation = PATH.ToString();
playthewavfile.Play();
}
}
Posted
Comments
   
Do you really play sound on the server side? Or do you use local host as a server side? :-)
—SA
Sajid227 10-Feb-16 2:17am
   
i just want to play sound?ahaha give me path
Sajid227 10-Feb-16 2:18am
   
i tried this because as this is web application so file need virtual path
   
I don't have access to your hard drive and server's host. More importantly, you don't answer my questions.
—SA
Sajid227 10-Feb-16 2:45am
   
can you give me answer if u have got any idea?
   
It makes no sense, especially if you don't answer my questions. The idea is simple like hell; I tried to explain it preliminary, all other talking after you start to collaborate...
—SA
Sajid227 10-Feb-16 3:10am
   
my requirement is that when user browse a file ,then this file is ready to play how can i do this
   
Sorry, I don't want to treat it negatively; it's up to you how you formulate your problems and try to get help.

I just have to explain: I cannot imagine that you can understand any help when I see how thoroughly you ignore all of my comments; this is the only reason I cannot continue this conversation. Please understand; sorry...

—SA

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Let my try to explain: the question makes no sense at all, no matter if you find the path correctly or not. Just to close this part of the issue: you can easily run your code under the debugger and see the calculated path name, compare it with the place your file really is. Maybe you need to understand that the '~' part of the path used by MapPathcorresponds to the root directory set up to your site (and not to the root directory of any disk volume!). This is way too simple; only you can do it, but it won't help you in this case.

The major problem is different: You are trying to play sound on the server side. But nobody there wants to hear your sound. Let's see: you say that your sound at @"G:\backup\LTM.CTP.WEBFORM\LTM.CTP.WEBFORM\Audio\q1.wav" "works fine". It can mean only one thing: you hear your sound because your server site is hosted on the same computer as your client side, or, let's assume the unlikely, on some computer in the same room as your development computer. It's likely that you are just using development HTTP server. Strictly speaking, you simply don't do any Web development. Yes, it's quite possible to do fully-fledged Web development on the development server or any other way using only the local host, but it's possible if you understand what you are doing. Note that there are no situations when hard-coded path name can be useful, and this is especially true for any Web development. It should be obvious enough. Your file "works fine" only by accident.

Also, it's pretty bad to use uncompressed WAV format.

What to do? Strictly speaking, it can be explained only if you explain what you want to achieve clearly. If you want to upload a file and them play it, it should be downloaded back to the client side and then played locally with JavaScript or HTML5 audio element:
Let me google that for you[^],
Web Audio API — Web APIs | MDN[^],
HTML5 Audio — Wikipedia, the free encyclopedia[^].

—SA
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100