how to optimize this random playlist html5 audio script -
<script type="text/javascript"> function random_playlist(){ var myrandom=math.round(math.random()*4); var soundurl='http://dummy.xy/dummy.mp3'; if (myrandom==0){soundurl='http://path_to.mp3';} else if (myrandom==1){soundurl='http://path_to.mp3';} else if (myrandom==2){soundurl='http://path_to.mp3';} else if (myrandom==3){soundurl='http://path_to.mp3';} else if (myrandom==4){soundurl='http://path_to.mp3';} console.log(soundurl); return soundurl; }; </script> <audio id="audioplayer_id" controls="controls" loop> <source id="source_id" src="" type="audio/mp3"/> browser not support audio element </audio> <script type="text/javascript"> var audioload = random_playlist(); console.log(audioload); document.getelementbyid('source_id').src= audioload; var audioplayer_id = document.getelementbyid('audioplayer_id') audioplayer_id.volume = 0.15; audioplayer_id.load(); audioplayer_id.play(); </script>
works me can optimized or ok ?
i hacked various sources seems html5 didnt integrate playlist function in audio tag, seems funny me. greets
you shorted random_playlist
function this:
function random_playlist(){ var paths = ['http://path_to.mp3', 'http://path_to.mp3', 'http://path_to.mp3']; return paths[math.floor(math.random() * paths.length)]; }
or (a better more complicated version):
var random_playlist = (function(){ var paths = ['http://path_to.mp3', 'http://path_to.mp3', 'http://path_to.mp3']; return function(){ return paths[math.floor(math.random() * paths.length)] }; }())
Comments
Post a Comment