简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM