JavaScript Safari canPlayType() marks .ogg as “probably” & HTML5 <audio><source> does not play

I want to create a html5 audioplayer for my band's website.

The audiofiles are stored in the database as mp3, ogg and wav files to support all major browsers on their current versions.

First, all audiofileversions for one audiofile are loaded into the html:

<audio class="song-preview" preload="none">
    <source type="audio/ogg" src="/preview/trees/trees/OG/DT/OGG"></source>
    <source type="audio/mpeg" src="/preview/trees/trees/OG/DT/MP3"></source>
    <source type="audio/wav" src="/preview/trees/trees/OG/DT/WAV"></source>
</audio>

Method I: Javascript checks with audio.canPlayType(type) for browser support and removes all unsupported <source> elements and adds a "probably" source to the <audio>'s src attribute.

Example I: (not working on my iphone).

Problem I: Nested <source> elements in an <audio> element doesn't work well. (Altough Safari normally plays a given mp3 from an <audio>'s src, it does not if it is in the <source>'s src. Having big wav files in the sources prevents mobile devices from playing the audio, too.)

Method II: Javascript checks with audio.canPlayType(type) for browser support and adds the first "probably"-supported <source>'s src to the <audio>'s src attribute. After that all <source> elements are removes.

Example II: (I am no JS expert but jsbin seems not to handle canPlayType() properly).

Problem II: Safari classifys my ogg-files as "probably" and then does not play them.

  • Why is the audio from <source> elements not playing?
  • Any ideas on how to better check for audiotype browsersupport?
  • Is having the same src in the <audio> element and the <source> child normally working?
Answer:1

My working "solution" so far:

Detect safari on desktops, if true, remove all ogg sources.

var isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);

if (isSafari) {
    $(this).children("source[type!='audio/mpeg']").remove();
}

code above found here

This solution is not recommended, since it depends on the send user-agent, which can be faked, etc.

Combined with the following code, the probably playable source file is merged into the src attribute of the audio element. This lets safari play your html audio properly:

$(this).children("source").each(function(){
    switch ($audio_preview[0].canPlayType($(this).attr("type"))) {
        case "":
            $(this).remove();
            break;
        case "maybe":
            $audio_preview.attr("src", $(this).attr("src"));
            break;
        case "probably":
            $audio_preview.attr("src", $(this).attr("src"));
            $audio_preview.children("source").remove();
            break;
        default:
            $audio_preview.attr("src", $(this).attr("src"));
    }
});
Answer:2

I have two static pages from a user which is deployed on their servers. Currently I am making a call to their server from code behind and loading them. But now I don't want to make a code behind call. ...

I have two static pages from a user which is deployed on their servers. Currently I am making a call to their server from code behind and loading them. But now I don't want to make a code behind call. ...

  1. load website inside iframe
  2. load webpage into iframe
  3. html load webpage in iframe
  4. load external webpage in iframe

I read this blog post on how to potentially pass a primitive by reference to a function in JavaScript. At first glance it seems like it could work but I'm wondering what potential pitfalls could come ...

I read this blog post on how to potentially pass a primitive by reference to a function in JavaScript. At first glance it seems like it could work but I'm wondering what potential pitfalls could come ...

I have problem with one native HTML5 canvas function getImageData() on Mac OS. I run test for this function in jsperf with a similar power processors and result is: Ubuntu 14.04 - chrome/opera: 300k ...

I have problem with one native HTML5 canvas function getImageData() on Mac OS. I run test for this function in jsperf with a similar power processors and result is: Ubuntu 14.04 - chrome/opera: 300k ...

  1. html5 canvas slow
  2. html5 canvas performance
  3. html5 canvas performance test
  4. html5 canvas performance tips
  5. html5 canvas performance benchmark
  6. html5 canvas performance android
  7. html5 canvas drawimage slow
  8. firefox html5 canvas slow
  9. html5 canvas is slow

I am making a fullscreen bootstrap carousel site. I use scroll for changing slides, but it goes to fast. How can I slow scroll speed down? I use this code: $('#mycarousel').bind('mousewheel', ...

I am making a fullscreen bootstrap carousel site. I use scroll for changing slides, but it goes to fast. How can I slow scroll speed down? I use this code: $('#mycarousel').bind('mousewheel', ...

  1. slow down scrolling in chrome
  2. slow down scrolling credits in imovie
  3. slow down scrolling excel
  4. slow down scrolling credits in imovie 11
  5. slow down scrolling javascript
  6. slow down scrolling
  7. slow down scrolling on iphone
  8. slow down scrolling windows
  9. slow down scrolling credits
  10. recyclerview slow down scrolling
  11. jquery slow down scrolling
  12. linux slow down scrolling
  13. slow down the scrolling speed of text
  14. how to slow down scrolling text in imovie
  15. how to slow down scrolling speed
  16. computer slow scrolling down
  17. how to slow down scrolling title in imovie