最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Play a binary string with the HTML audio tag - Stack Overflow

programmeradmin6浏览0评论

I have a server that serves ogg encoded audio to a client that requests it with a http GET. If I inject the GET route into the HTML audio src it receives the audio and plays it:

function working(text) {
    var downloadURL = 'http://localhost:8080/nlc/synthesize' +
        '?text=' + encodeURIComponent(text);

    audio.pause();
    audio.src = downloadURL;
    audio.play();

};

If I use a http GET call (in AngularJS) and try to inject the response into the HTML audio src it won't play it:

function not_working(text) {
  $http.get('http://localhost:8080/nlc/synthesize' + '?text=' + encodeURIComponent(text)).then(function(response) {
      console.log(response);
      audio.pause();
      audio.src = encodeURIComponent(response);
      audio.play();

};

The log of the response shows a JSON with a binary string at the 'data' key:

Object {data: "OggS�1�/ڂ OpusHead��]OggS…�xs���������������������������������", status: 200, config: Object, statusText: "OK"}

Is there a way to decode the http GET response into something I can inject into the audio src?

I have a server that serves ogg encoded audio to a client that requests it with a http GET. If I inject the GET route into the HTML audio src it receives the audio and plays it:

function working(text) {
    var downloadURL = 'http://localhost:8080/nlc/synthesize' +
        '?text=' + encodeURIComponent(text);

    audio.pause();
    audio.src = downloadURL;
    audio.play();

};

If I use a http GET call (in AngularJS) and try to inject the response into the HTML audio src it won't play it:

function not_working(text) {
  $http.get('http://localhost:8080/nlc/synthesize' + '?text=' + encodeURIComponent(text)).then(function(response) {
      console.log(response);
      audio.pause();
      audio.src = encodeURIComponent(response);
      audio.play();

};

The log of the response shows a JSON with a binary string at the 'data' key:

Object {data: "OggS�1�/ڂ OpusHead��]OggS…�xs���������������������������������", status: 200, config: Object, statusText: "OK"}

Is there a way to decode the http GET response into something I can inject into the audio src?

Share Improve this question edited Apr 20, 2016 at 13:06 trahloff asked Apr 20, 2016 at 8:29 trahlofftrahloff 6672 gold badges9 silver badges17 bronze badges 2
  • 1 I dont know about angular, but with XMLHttpRequest, your can set its .responseType to "blob", and then create an objectURLfrom this blob response (URL.createObjectURL(this.response)) that you will be able to pass as the src of your audio element. Otherwise, if your targeted browsers do support the AudioContextAPI, you can use the AudioContext.decodeAudioData() method. – Kaiido Commented Apr 20, 2016 at 8:54
  • Thanks, your answer helped me a lot. – trahloff Commented Apr 20, 2016 at 14:13
Add a ment  | 

2 Answers 2

Reset to default 4

I finally managed to do it with this code:

function working(text) {
    var url = 'http://localhost:8080/nlc/synthesize';
    var req = {
        method: 'POST',
        url: url,
        responseType: 'blob',
        data: {
            "text": text
        },
        headers: {
            'Content-Type': 'application/json',
        }
    }

    $http(req).then(function(response) {
        console.log(response);
        audio.pause();
        audio.src = URL.createObjectURL(response.data);
        audio.play();
    })
};

in http.get you can pass a dict with optional config

$http.get(Url, {
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined}
})
.success(function(){
})
.error(function(){
});

https://docs.angularjs/api/ng/service/$http

maybe you can bine this with the ment of @kaiido

发布评论

评论列表(0)

  1. 暂无评论