Click here to Skip to main content
15,907,493 members
Please Sign up or sign in to vote.
1.00/5 (1 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
C#
SoundPlayer playthewavfile = new SoundPlayer(@"G:\backup\LTM.CTP.WEBFORM\LTM.CTP.WEBFORM\Audio\q1.wav");

but i want to give the path like this
C#
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
Sergey Alexandrovich Kryukov 10-Feb-16 2:14am    
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
Sergey Alexandrovich Kryukov 10-Feb-16 2:25am    
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?

1 solution

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
 
Share this answer
 
v3

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