Datagrid Hyperlink field to play file system wave file help.

Discussion in 'ASP .Net' started by Guest, Aug 24, 2006.

  1. Guest

    Guest Guest

    Im working with VS 2005 and trying to use a Hyperlink field in a datagrid to
    play a wave file that is not located in the website folders but is in a plain
    folder on the same machine, windows 2003 server, WMP 10.0 .

    If I type the full path in an IE address field it plays the file in WMP

    When I test my Web page ( running the debugger in VS.)
    The datagrid has a column called "MsgFile" with the full path to the wave
    file e.g. c:\pf\app\audio\3\19.wav. Another Hyperlink column in the grid
    has DataNavigateURLFields pointing to the MsgFile field and that works.

    But for the hyperlink's DataNavigateURLFormatString I've tried:

    1. {0} - which results in the ':' from the path being removed and converts
    backslash to forward slash (c/pf/app/audio/3/19.wav) and hence file not found

    2. http://{0} - also removes the ':' from the path and hence file not found

    3. http://localhost/{0} - leaves the ':' in the path to result in what I
    believe is the correct URL http://localhost/c:/pf/app/audio/3/19.wav but
    results in Page Not Found.

    Hopefully it is possible to play a wave file that is not in the website

    The simple question is what format string needed in the
    DataNavigateURLFormatString to get the file to play?
    Any assistance is appreciated.
    Guest, Aug 24, 2006
    1. Advertisements

  2. Hi Morris,

    From your description, I understand your web application will display some
    links on one page (through grid) and the link will point to some wav sound
    so that the user can play it when click on the link. The problem is that
    the wav files is in a non-virtual directory(accessible through http
    address) on the server, correct?

    As for this problem, I think it is doable and have several means. I'd like
    to confirm following things first:

    1. Are you developing through ASP.NET 1.1/VS 2003 or ASP.NET 2.0/VS 2005?

    2. As for the media file(wav), how do you want to play it to client user,
    among the following options:

    ** let the client user directly link to the media and the browser will
    choose a player on the client to play it.

    ** embed some activex or sound tag in web page and use let the browser to
    play it, thus , the client will not launch a separate player when user
    click the link.

    As for the wav file linking problem, I don't think we can directly use the
    physical path(like c:\media\.....) because this path is not recommend for
    internet application. also, the "c:\media\" physical path is only valid to
    the server machine, since the page is finally displayed in client browser,
    the browser will try locating the file on the client machine's disk and
    won't be able to get the file.

    To resolve this, I suggest you consider the following means:

    1)Create an ASP.NET web page or HttpHandler which will programmtically read
    the file from the non-web folder and then write it out as response stream.

    suppose the page's name is "MediaOutput.aspx", we add the following code in
    its code behind:

    public partial class MediaOutput : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    string fn = Request.QueryString["fn"];

    if (string.IsNullOrEmpty(fn))
    throw new Exception("Invalid Argument...........");


    Response.ContentType = "audio/wav";

    Response.WriteFile(@"D:\temp\media\" + fn);




    Then, in other page which want to reference a certain wav file in the
    "D:\temp\media" folder, we no longer need to care about the physical path,
    but simply use the "MediaOutput.aspx" to get the wav stream. e.g.

    <a href="MediaOutput.aspx?fn=ding.wav" >play ding.wav </>

    if you want to create a custom httphandler to do the work, please refer to
    the following article:

    However, as I've mentioned, if you directly point to the wav stream in the
    hyperlink, the wav will be played in a new player window at
    client-side(maybe media player or other player...). If you do not want to
    let a new launched player to do it, you can have a look at the following
    web pages:

    #Playing Sounds On The Web

    #Demonstration of Different Ways to Play a Sound from a Web Page

    it demonstrate several means to play sound, you can even use javascript to
    make a certain element(like <bgsound> or <embed> to reference the media
    stream output by the "mediaoutput.aspx" page above).

    Hope this helps. If there is anything unclear, please feel free to let me


    Steven Cheng

    Microsoft MSDN Online Support Lead

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Aug 24, 2006
    1. Advertisements

  3. Guest

    Guest Guest

    Thanks for your reply. Your advice and links you sent were very helpfull.

    I implemented your solution using the MediaOutput.aspx page. And that
    works. Thanks

    However if the MsgFile field, which is passed in MediaOutput.aspx?fn={0},
    has a full path e.g. c:\FP\audio\1.wav then the Hyperlink is not active. It
    appears that the ':' is not allowed in the {0} value. If it contains a
    partial path without the c: part (specifically the ':') it works.
    What is wrong with ':' in the QueryString parameter?


    Guest, Aug 24, 2006
  4. Hello Morris,

    The url and ":" char issue is due to the restriction of characters allowed
    in url. If we wan t pass any data in querystring which will contain some
    potential particular characters(not allowed in URL), we'd better Urlencode
    it before send it. For example, we can use the Server.UrlEncode to encode
    our file path before bind it to link. e.g.

    <a href="<%# "TargetPage.aspx?fn=" +
    Server.UrlEncode(Eval("FilePath").ToString()) %>">link to media</a>

    Also, at the TargetPage.aspx, you do not need to explicitly decode it,
    ASP.NET runtime will do it for you, you can simply read out the querystring
    value as normal.

    Hope this helps.


    Steven Cheng

    Microsoft MSDN Online Support Lead

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Aug 25, 2006
  5. Guest

    Guest Guest

    So far what you have helped with is working.
    I am using the response.writefile(@filename) to play the audio .wav file
    with the URLEncode.

    Now I have an issue that when the Media player starts up to play the file
    some time it gives an error that it could not play the file / may not have
    the codecs needed to play the file, which is a .wav file. Error: cood1199.
    Sometime it does play the file fine but thats not consistant and I cant
    reproduce it working or failing with any consistancy.

    If I click on the target file in the Windows explorer to open it the Media
    player always starts up and plays the file.

    Also on the same topic when I click on the file in windows explorer the
    Media Player display the filename that is playing. But when it does play via
    the app the Media Player only displays "Media Output". How can I control
    what is diaplyed in the MP?

    Any direction here is appreciated. Should I post to the group for Media
    Player? If so which one is that?

    And finally a question on Response.Write(). I use it to display an error
    message via a Try/Catch loop around the response.writefile() in the event
    that the file is not found. That works but my question is how can I get
    response.write() to open a new browser window instead of writing to the same
    explorer window that issued the request?
    Guest, Aug 29, 2006
  6. Thanks for your reply Morris,

    As for the occasional cood1199 error, it is likely that the response stream
    contains incorrect binary data (which is not audio/wav stream), then the
    client media player will surely report error against the data.

    You can use some http/tcp trace tools to capture the http response message
    to see whether the response message doesn't contain correct audio/wav data
    at those error requests. The SOAP Toolkit 3.0 contains a trace utility
    which can help capture http request/response message.

    #SOAP Toolkit 3.0

    for your further question:

    And finally a question on Response.Write(). I use it to display an error
    message via a Try/Catch loop around the response.writefile() in the event
    that the file is not found. That works but my question is how can I get
    response.write() to open a new browser window instead of writing to the
    explorer window that issued the request?

    I'm afraid this is not supported, because our MediaOutput page is suppose
    to return binary audo/wav data only and we should not use response.Write to
    flush out any non-wav data(text, xml ....), this will violate the
    "audio/wav" contenttype we set in the code. For such scenario, I suggest
    you consider the following approach:



    Response.ContentType = "audio/wav";

    Response.WriteFile(@"D:\temp\media\" + fn);


    catch (Exception ex)


    instead of writeout text, you can redirect the request to an existing wav
    file in the web application folder which contains the sound about error so
    that the client will get the error media and play it. Do you think it
    doable in your scenario?


    Steven Cheng

    Microsoft MSDN Online Support Lead

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Aug 29, 2006
  7. Guest

    Guest Guest

    1. With the Media error. The same file will work and then not work so how can
    that be a data error?

    2. With the Error message. What I did was change the context in the catch
    block to "text/HTML" and then write out an error message.
    catch (Exception ex)
    Response.ContentType = "text/HTML";
    Response.Write("Error occurred <br>");
    Response.Write("Click back to try again");
    This works but I want to send the error message to a new IE window and leave
    the original window open (like a pop up message box). so I dont have to
    tell the user to "Click Back" to get to the original page in IE. They can
    just close the error message window and still have the original Page in IE
    that started the process.
    Guest, Aug 29, 2006
  8. Thanks for your reply Morris,

    If the problem will occur against the same file and response stream
    randomly, it's quite hard to determine the actual cause, is it specific to
    a certain client?

    BTW, if as for displaying error message or open in a new browser windows,
    you can consider output some javascript code to show a message box or open
    a new browser window pointing to another error page. for example:

    catch (Exception ex)


    Response.ContentType = "TEXT/HTML";

    Response.Write("<script language='javascript'
    Response.Write("<script language='javascript'

    However, this will work only when the user directly click on the link to
    our MediaOutput page's url.


    Steven Cheng

    Microsoft MSDN Online Support Lead

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Aug 30, 2006
  9. Hi Morris,

    Have you got any further progress on this issue or does the new suggestion
    in my last reply also helps a little?

    If there is anything else we can help, please feel free to post here.


    Steven Cheng

    Microsoft MSDN Online Support Lead

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Sep 1, 2006
  10. Hi Steven,

    I think I've been doing stuff similar to Morris, I got stuck however
    because I seem to have no fastforwarding / rewind functionality in my
    Windows Media Player control! I'm using the OBJECT tag to include the
    WMP control in my web page to play streamed WAV files as illustrated

    * ASP.NET 2.0
    * VS 2005
    * Windows Media Player and


    I want to build a web page with an integrated Audio Player that
    -1- can play (streamed) WAV files
    -2- has Play / Stop / Pause / FastForward / Rewind buttons
    -3- prevents the user from downloading the WAV file
    -4- does not cache the audio file

    My first choice was the Windows Media Player control in internet
    explorer, simply because I don't need support for browsers other than
    IE, and it seems quite easy at first sight to add my own UI to the
    player, controlling the WMP control with some VBScript for instance. It
    should become part of an ASP.NET solution anyhow...

    I tried this:

    An audioPlayer.aspx file that generates a web page with a Windows Media
    Player control. The URL parameter of the WMP control is a
    streamAudio.aspx file. The streamAudio.aspx file streams the WAV file to
    the browser (streamAudio.aspx will pick the file name from the request
    eventually). I thought this already solves -3- and -4- because the
    client has no direct access to the WAV file, only to a binary stream.
    This is the code:

    ===== audioPlayer.aspx =====
    <%@ Page Language="VB" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    <html xmlns="" >
    <head runat="server"><title>audioPlayer</title></head>
    <form id="form1" runat="server">
    <object id="Player" height="45" width="220"
    <param name="autoStart" value="False" />
    <!--param name="uiMode" value="invisible" /> <!-- hide later, when it
    works... -->
    <param name="URL" value="audioStream.aspx" />
    <input type="BUTTON" name="BtnPlay" value="Play" onclick="StartMeUp()"
    <input type="BUTTON" name="BtnStop" value="Stop" onclick="ShutMeDown()"
    <input type="BUTTON" name="BtnForward" value="fwd" onclick="ffw()" />
    <script type="text/vbscript">
    Sub StartMeUp ()
    End Sub
    Sub ShutMeDown ()
    End Sub
    Sub ffw()
    If form1.Player.controls.isAvailable("FastForward") Then
    MsgBox "found fastforward"
    MsgBox "didn't find fastforward"
    End If
    End Sub

    ===== audioStream.aspx =====
    <%@ Page Language="VB" Src="~/audioStream.aspx.vb"

    ===== Code-behind page audioStream.aspx.vb =====
    Imports Microsoft.VisualBasic
    Namespace MyNS
    Partial Public Class audioStream
    Inherits System.Web.UI.Page

    Sub Page_Load(ByVal sender As Object, ByVal e As
    Response.ContentType = "audio/wav"
    End Sub
    End Class
    End Namespace


    When I click the fwd button

    on Windows XP SP2, WMP
    all clicks: "didn't find fastforward"

    on Windows 2000 advanced Server, WMP
    first click: "found fastforward" and fast forwarding really works
    all subsequent clicks: "didn't find fastforward"


    [1] fastforward in WMP
    Does anyone know why the fastforward is disabled? Is this problem caused
    by my specific files (WAV files, but it also fails for mp3, and I heard
    others complain even about WMV!), or is it caused by WMP? I mean, are
    there some characteristics of audio files that make it impossible for
    any audio player to fastforward them, or is this just a shortcoming
    (bug!?) of Windows Media Player?

    This makes me think WMP is unreliable and not the way to go. How can I
    expect my customer to run WMP on
    Windows XP SP2 with this and this and that, and nothing else because
    otherwise his player will not be fully functional? It seemed like quite
    a cool control initially but I'm starting to have some doubts now...

    [2] Caching of streams
    I'm not quite sure about the caching. If I stream the WAV file to the
    client, is the stream cached somehow on the client? Simply put: If the
    client closes all the browsers, and all sessions have expired, is there
    any way that he can find and playback the audio on his machine (from tmp
    internet files or whatever location)?

    [3] What are the alternatives to realize the above in a reasonably
    simple way: An audio player (preferably with customizable UI) that can
    receive streamed WAV files (or possible a converted file format, if WAV
    is not suitable), with play/pause/stop/fastforward/rewind functionality.
    No Quicktime or Realplayer... Using .NET 2.0 and VB 2005 though!

    Many Thanks in advance!

    Guido Helmers, Sep 11, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.