我如何提供声音播放器的路径 [英] How do i give path to soundplayer

查看:84
本文介绍了我如何提供声音播放器的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页表单,其中我要求使用文件上传控件浏览音频文件,然后按钮上传并将此文件播放到服务器上的文件夹,从那里我获得声音播放器的路径,

我的问题是,当我以这种方式传递路径时它的工作很好

 SoundPlayer playthewavfile =  SoundPlayer( @  G:\ backup\LTM.CTP.WEBFORM\LTM.CTP .WEBFORM\Audio\q1.wav); 



但我想给出这样的路径

 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();



这里给出了无效路径的例外情况?



< b>我尝试了什么:



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.Pl AY();

}

}

解决方案

让我试着解释一下:这个问题完全没有意义,没有如果你找到正确的路径,那就很重要。只是为了解决这个问题:您可以在调试器下轻松运行代码并查看计算出的路径名,并将其与文件所在的位置进行比较。也许您需要了解 MapPath 使用的路径的'〜'部分对应于设置到您站点的根目录(而不是任何磁盘的根目录)体积!)。这太简单了;只有你能做到,但在这种情况下它不会帮助你



主要问题不同:你正试图在服务器端播放声音。但没有人愿意听到你的声音。让我们看看:你在@G:\ backup\LTM.CTP.WEBFORM\LTM.CTP.WEBFORM\Audio\q1上说你的声音。 wav工作正常。它只能意味着一件事:您听到了声音,因为您的服务器站点与客户端在同一台计算机上,或者,假设不太可能,在与开发计算机位于同一房间的某台计算机上。您可能只是使用开发HTTP服务器。严格来说,你根本就不做任何Web开发。是的,很有可能在开发服务器上或仅使用本地主机的任何其他方式进行完全成熟的Web开发,但是如果您了解自己在做什么,就有可能。请注意,没有硬编码路径名可能有用的情况,对于任何Web开发尤其如此。这应该是显而易见的。你的文件只能意外地工作正常



另外,使用未压缩的WAV格式非常糟糕。



怎么办?严格地说,只有当你解释你想要清楚地实现什么时,才能解释它。如果您要上传文件并播放,应将其下载回客户端,然后使用JavaScript或HTML5本地播放音频元素:

让我为你辩护 [ ^ ],

Web Audio API - Web API | MDN [ ^ ],

HTML5音频 - 维基百科,免费的百科全书 [ ^ ]。



-SA

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();
}
}

解决方案

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


这篇关于我如何提供声音播放器的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆