2

I use this code to generate a audio element from byte , it create a simple wav sound , I am able to play that in google chrome or firefox of DESKTOP , but when I want to play that in Google Chrome Mobile , it play nothing , where is the problem , I am able to play wav file easly but I am not able to play music from binary data in google chrome mobile :

<!DOCTYPE html>
<html>
    <head>
        <title>Explosion Generator</title>
        <script type = "text/javascript" src="../../js/jquery-1.8.3.js" ></script>
        <script>

            function encodeAudio16bit(data, sampleRate) {
                var n = data.length;
                var integer = 0, i;

                // 16-bit mono WAVE header template
                var header = "RIFF<##>WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00<##><##>\x02\x00\x10\x00data<##>";

                // Helper to insert a 32-bit little endian int.
                function insertLong(value) {
                    var bytes = "";
                    for (i = 0; i < 4; ++i) {
                        bytes += String.fromCharCode(value % 256);
                        value = Math.floor(value / 256);
                    }
                    header = header.replace('<##>', bytes);
                }

                // ChunkSize
                insertLong(36 + n * 2);

                // SampleRate
                insertLong(sampleRate);

                // ByteRate
                insertLong(sampleRate * 2);

                // Subchunk2Size
                insertLong(n * 2);

                // Output sound data
                for (var i = 0; i < n; ++i) {
                    var sample = Math.round(Math.min(1, Math.max(-1, data[i])) * 32767);
                    if (sample < 0) {
                        sample += 65536; // 2's complement signed
                    }
                    header += String.fromCharCode(sample % 256);
                    header += String.fromCharCode(Math.floor(sample / 256));
                }

                return 'data:audio/wav;base64,' + btoa(header);
            }


            function generateTone(freq, sampleRate, duration){
                var tone = []
                for(var i = 0; i < duration * sampleRate; ++i){
                    tone.push(Math.sin(2 * Math.PI * i / (sampleRate/freq)));
                }
                return tone;
            }

            $(document).ready(function(){
                $("#body").html("Sounds...");

                var sampleRate = 44100; // hz
                var freq = 400;   // hz
                var duration = 2; // seconds
                var tone = generateTone(freq, sampleRate, duration);
                var base64EnodedTone = encodeAudio16bit(tone, sampleRate);
                var audioPlayer = $('<audio>').attr({ src: base64EnodedTone,
                    controls:true
                });
                $("#body").append(audioPlayer);

                $("#body").append("<p>done<p>");
            });    
        </script>
    </head>

    <body>
        <div id="body" ></div>
    </body>
</html>
Ata
  • 12,126
  • 19
  • 63
  • 97
  • Are you sure the platform supports the – Ken Wolf Jun 16 '13 at 06:10
  • yes , it supports well , I can play wav file if I use file path , but I am not able to play binary data – Ata Jun 16 '13 at 06:13

1 Answers1

3

This is most certainly a bug. Any time that you have it working on desktop and it fails to work on Chrome for Android that it is 99.5% likely to be a bug.

I have raised the bug here: https://code.google.com/p/chromium/issues/detail?id=253466 with a demo of the error here: http://jsbin.com/ajocid/latest

There is another solution on the desktop to encode the audio into a Blob and play that, unfortunately that does not work either on Chrome for Android and needs to be fixed.

The best you can currently hope for until the two bugs are fixed is to upload this to a server and play it from there.

Kinlan
  • 16,315
  • 5
  • 56
  • 88
  • yes , it seem that really a bug , I have testet that on last android version , and still not working ... – Ata Jun 24 '13 at 13:37
  • If you start the bug in this answer you will see changes to it from the eng team. – Kinlan Jun 24 '13 at 13:58
  • There is nothing that you can do, the other solution of encoding into a blob does not work either on Chrome for Android. – Kinlan Jun 24 '13 at 14:10
  • Do you think Google team fix that soon ? – Ata Jun 24 '13 at 14:41
  • If they fixed it today it would still take about 12 weeks to reach all users. I can't give any other time frame. – Kinlan Jun 24 '13 at 17:25
  • also , are you from chrom team yourslef ? i see your email – Ata Jun 24 '13 at 18:46
  • do you have any feature that user android voice recorder via Google chrome ? also do you think your team fix play problem soon ? – Ata Jun 24 '13 at 19:04
  • I don't think the problem will get fixed amazingly quickly, there are a lot of other features etc to work on. WebKitSpeech API will let you use the recoreder. – Kinlan Jun 25 '13 at 09:08