AngularJS - 解码字节数组和播放音频文件(Wav / MP3)

时间:2017-03-14 13:01:28

标签: javascript angularjs web-audio

使用以下服务播放以字节数组格式提供的音频文件(wav / mp3)。

myAudioService.getAudioTone(userid).then(function (data) {

        var context;    // Audio context
        var buf;        // Audio buffer
        $window.AudioContext = $window.webkitAudioContext;
        context = new AudioContext();

        $timeout(function () {

        $scope.playByteArray = function(){

            var arrayBuffer = new ArrayBuffer(data.length);
            var bufferView = new Uint8Array(arrayBuffer);
            for (i = 0; i < data.length; i++) {
              bufferView[i] = data[i];
            }

            context.decodeAudioData(arrayBuffer, function(buffer) {
                buf = buffer;
                play();
            });
        }

        $scope.play = function(audioBuffer){

            // Create a source node from the buffer
            var source = context.createBufferSource();
            source.buffer = buf;
            // Connect to the final output node (the speakers)
            source.connect(context.destination);
            // Play immediately
            source.start(0);

        }    


        if(data.length !== '' || data !== ''){
            $scope.playByteArray();
        }
        }, 3000);

});

调用这些函数,但它会抛出异常。

Uncaught (in promise) DOMException: Unable to decode audio data

如何在Chrome,FF和IE中运行它?

P.S。 $window$timeout已在控制器中定义。

1 个答案:

答案 0 :(得分:0)

根据错误消息input_folder<- "C:/Users/Data_Analysis/R data processing/points_rastervalue" output_folder<- "C:/Users/Data_Analysis/R data processing/output" setwd("C:/Users/Data_Analysis/R data processing/points_rastervalue") shps <- list.files(path=".", pattern="50m.shp", ignore.case = TRUE, recursive = FALSE, include.dirs = FALSE) for( shp in shps ){ mySHP <- readOGR('.', layer = ?? ) } 不包含您认为包含的内容。您应该验证数组中的字节是否与编码的wav / mp3文件相同。