abod alabhawi
abod alabhawi

Reputation: 19

Redirecting to a new page when audio not supported by the browser

I have JavaScript code for audio. I want to redirect to a URL if the browser has audio support. Also if the browser does not support audio, redirect to another link.

I tried this:

<script>
var audio= document.createElement("audio") 
var media= {audio : (audio.play) ? true : false}

if (media == true) {
    window.location.replace("file1.php");
} else {
    window.location.replace("file2.php");   
}
</script>

It didn't work.

Can anyone help? Thanks.

Upvotes: 0

Views: 73

Answers (1)

user2037979
user2037979

Reputation:

I tried this on a personal site, and using the console as well, and worked like a charm, give it a try and let me know how it goes.

You were close enough creating and audio tag.

var audioTest = document.createElement('audio');
if( !!(audioTest.canPlayType && audioTest.canPlayType('audio/mpeg;')) ) {
  // redirect
} else {
  // redirect
}

Common audio type values (JIC):

video/ogg
video/mp4
video/webm
audio/mpeg
audio/ogg
audio/mp4

Upvotes: 2

Related Questions