1

i have web form ,in which when user click a button and select file from local device and upload it to folder that is in my project now,then this file pass audio tag of html5,and ready to play i tried following code

<div class="col-sm-5">
 <asp:Button ID="btnbrowse" runat="server" Text="Upload File"
 CssClass="btn btn-success btn-block"
  OnClick="btnbrowse_Click" />
 </div>

and code behind

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();               
}
Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
sa227
  • 37
  • 1
  • 8

1 Answers1

0

This is how I played the .wav file, I first saved the .wav file then passed that .wav file to the SoundPlayer class object to play the file.

string path = System.IO.Path.Combine(Server.MapPath("uploads/"), FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(path);
SoundPlayer s = new SoundPlayer(path);
s.Play();

Please notice, this might not work for audio formats other than .wav, see this article from Microsoft.

The SoundPlayer class cannot play other file types, such as .wma or .mp3. If you want to play other file types, you can use the Windows Media Player control. For more information, see Using the Windows Media Player Control in a .NET Framework Solution and Windows Media Player Object Model Reference for Visual Basic .NET and C# in the Windows Media Player SDK.

Plus, I would not suggest the server side code to play audio because the audio might just run on the server and client may have no idea if something is being played or not. It is highly recommended that you use html audio player and pass the path to it. See this SO thread for a reference.

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
  • File is uploaded ,but this is an empty file of that file which i have uploaded – sa227 Feb 10 '16 at 09:56
  • bro, try testing your code on a newly created web page, further more, can you tell me why you are writing these line of codes? string Path = Server.MapPath(FileUpload5.FileName); and playthewavfile.SoundLocation = Path.ToString(); – Jamshaid K. Feb 10 '16 at 10:23
  • i thought that there is my location of file that cause exception,but when i debug i found that file is uploaded but this just an empty file that cause exception?how i can do this if i save this file in db then get back to play it? or directly pass upload file to play? – sa227 Feb 10 '16 at 10:31
  • bro, before/after saving the file in some specific location, save it's path in the Database so that you can get the file directly by just reading the path from database – Jamshaid K. Feb 10 '16 at 10:42