将音高检测HTML页面转换为Android应用

时间:2018-11-16 11:40:35

标签: javascript android html5-audio

大家好,我有html文件,可以检测到在我的android手机上出色运行的麦克风输入频率,但是,一旦我尝试使用以下 html to apk online converter ({{ 3}})它只是显示屏幕而没有检测到频率。我想将此html文件转换为android完整运行的应用程序。这是我的html代码:

<style>
#frequencybox {
background-color: red;
width: 10px;
height: 30px;
margin: 0;}
</style>
<script>
 navigator.getUserMedia = navigator.getUserMedia
                               || navigator.webkitGetUserMedia
                               || navigator.mozGetUserMedia;
        navigator.getUserMedia({ video : false, audio : true }, callback, 
 console.log);
        function callback(stream) {
            var ctx = new AudioContext();
            var mic = ctx.createMediaStreamSource(stream);
            var analyser = ctx.createAnalyser();
            var osc = ctx.createOscillator();
            mic.connect(analyser); 
             osc.connect(ctx.destination);
           //  osc.start(0);
            var data = new Uint8Array(analyser.frequencyBinCount);
            function play() {
                analyser.getByteFrequencyData(data);
                // get fullest bin
                var idx = 0;
                for (var j=0; j < analyser.frequencyBinCount; j++) {
                    if (data[j] > data[idx]) {
                        idx = j;
                    }
                }
                var frequency = idx * ctx.sampleRate / analyser.fftSize;
                console.log(frequency);

             document.getElementById("freq-h1").innerHTML = frequency;
             document.getElementById("frequencybox").style.width = 
 (frequency/10) + 'px';
            osc.frequency.value = frequency;
               requestAnimationFrame(play);
            }
            play();
        }
 </script>
 <body>
 <div id="frequencybox"><div>
 <h1 id="freq-h1"></h1>
 </body>

0 个答案:

没有答案