Using Ajax to embed audio file in HTML?

Discussion in 'Javascript' started by abrtlt@yahoo.com, Oct 11, 2006.

  1. Guest

    I would like to have a web page in which, when the user clicks on any
    of several specific elements, a specific audio file is played, without
    reloading the page.
    The specific audio file name is obtained from a PHP script (accessing
    a MySQL database) that could be called using XMLHttpRequest and
    JavaScript would write in a "div" tag an "embed and play" HTML tag
    containing the audio file name.
    I am not sure this would work, as the embed tag is HTML and will not be
    executed as the page has already been loaded. In any case, unless I am
    mistaken, JavaScript in the web page can handle only a text or XML
    response, and thus I can get the audio file name, but not the file
    itself. I can't pre-embed all audio files in the page because there are
    too many (that is why I use MySQL to index them).
    Is there any way to get round this?
    Thanks!

    Andrew
     
    , Oct 11, 2006
    #1
    1. Advertising

  2. Jeremy Guest

    wrote:
    > I would like to have a web page in which, when the user clicks on any
    > of several specific elements, a specific audio file is played, without
    > reloading the page.
    > The specific audio file name is obtained from a PHP script (accessing
    > a MySQL database) that could be called using XMLHttpRequest and
    > JavaScript would write in a "div" tag an "embed and play" HTML tag
    > containing the audio file name.
    > I am not sure this would work, as the embed tag is HTML and will not be
    > executed as the page has already been loaded. In any case, unless I am
    > mistaken, JavaScript in the web page can handle only a text or XML
    > response, and thus I can get the audio file name, but not the file
    > itself. I can't pre-embed all audio files in the page because there are
    > too many (that is why I use MySQL to index them).
    > Is there any way to get round this?
    > Thanks!
    >
    > Andrew
    >


    According to specifications, there is no reason why a dynamically
    created <object> element shouldn't work:

    var obj = document.createElement("object");
    obj.type = "audio/mpeg3";
    obj.data = "http://my.audio.url";
    document.body.appendChild(obj);

    In practice, I've no idea how well that would work. It seems to work
    flawlessly in Firefox, although no controls are presented (or if they
    are I can't see them).

    Jeremy
     
    Jeremy, Oct 11, 2006
    #2
    1. Advertising

  3. Guest

    Jeremy wrote:
    > wrote:
    > > I would like to have a web page in which, when the user clicks on any
    > > of several specific elements, a specific audio file is played, without
    > > reloading the page.
    > > The specific audio file name is obtained from a PHP script (accessing
    > > a MySQL database) that could be called using XMLHttpRequest and
    > > JavaScript would write in a "div" tag an "embed and play" HTML tag
    > > containing the audio file name.
    > > I am not sure this would work, as the embed tag is HTML and will not be
    > > executed as the page has already been loaded. In any case, unless I am
    > > mistaken, JavaScript in the web page can handle only a text or XML
    > > response, and thus I can get the audio file name, but not the file
    > > itself. I can't pre-embed all audio files in the page because there are
    > > too many (that is why I use MySQL to index them).
    > > Is there any way to get round this?
    > > Thanks!
    > >
    > > Andrew
    > >

    >
    > According to specifications, there is no reason why a dynamically
    > created <object> element shouldn't work:
    >
    > var obj = document.createElement("object");
    > obj.type = "audio/mpeg3";
    > obj.data = "http://my.audio.url";
    > document.body.appendChild(obj);
    >
    > In practice, I've no idea how well that would work. It seems to work
    > flawlessly in Firefox, although no controls are presented (or if they
    > are I can't see them).
    >
    > Jeremy



    Thanks. Yes, you must be right. I have a non-Ajax PHP /Javascript page
    that works fine by reloading and shows (with IE) neither controls nor
    separate mp3 player (I don't want them) if I use <EMBED> but starts a
    WMP window if I use <OBJECT>.
    Andrew
     
    , Oct 11, 2006
    #3
  4. Jeremy Guest

    wrote:
    > Jeremy wrote:
    >> wrote:
    >>> I would like to have a web page in which, when the user clicks on any
    >>> of several specific elements, a specific audio file is played, without
    >>> reloading the page.
    >>> The specific audio file name is obtained from a PHP script (accessing
    >>> a MySQL database) that could be called using XMLHttpRequest and
    >>> JavaScript would write in a "div" tag an "embed and play" HTML tag
    >>> containing the audio file name.
    >>> I am not sure this would work, as the embed tag is HTML and will not be
    >>> executed as the page has already been loaded. In any case, unless I am
    >>> mistaken, JavaScript in the web page can handle only a text or XML
    >>> response, and thus I can get the audio file name, but not the file
    >>> itself. I can't pre-embed all audio files in the page because there are
    >>> too many (that is why I use MySQL to index them).
    >>> Is there any way to get round this?
    >>> Thanks!
    >>>
    >>> Andrew
    >>>

    >> According to specifications, there is no reason why a dynamically
    >> created <object> element shouldn't work:
    >>
    >> var obj = document.createElement("object");
    >> obj.type = "audio/mpeg3";
    >> obj.data = "http://my.audio.url";
    >> document.body.appendChild(obj);
    >>
    >> In practice, I've no idea how well that would work. It seems to work
    >> flawlessly in Firefox, although no controls are presented (or if they
    >> are I can't see them).
    >>
    >> Jeremy

    >
    >
    > Thanks. Yes, you must be right. I have a non-Ajax PHP /Javascript page
    > that works fine by reloading and shows (with IE) neither controls nor
    > separate mp3 player (I don't want them) if I use <EMBED> but starts a
    > WMP window if I use <OBJECT>.
    > Andrew
    >


    You have to be careful with IE and <object> to get it to work properly.
    Some finesse usually does the trick (google Flash Satay for an article
    on getting <object> to work well). In any case, you should avoid
    <embed> as it's not a valid element.

    Jeremy
     
    Jeremy, Oct 12, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aeonsun
    Replies:
    2
    Views:
    447
    Luc The Perverse
    Mar 11, 2007
  2. yukijocelyn

    Embed ".html" file into ".aspx" file

    yukijocelyn, Aug 6, 2007, in forum: .NET
    Replies:
    0
    Views:
    1,969
    yukijocelyn
    Aug 6, 2007
  3. Replies:
    2
    Views:
    751
    cwdjrxyz
    Mar 19, 2008
  4. sick dick
    Replies:
    0
    Views:
    405
    sick dick
    Apr 3, 2006
  5. Manuel

    Embed image in html with ajax

    Manuel, Jun 29, 2007, in forum: Javascript
    Replies:
    6
    Views:
    248
    Manuel
    Jul 2, 2007
Loading...

Share This Page