Web Audio With Multiple Streams

Viewed 2087

I am attempting to stream audio via a RESTful based web service. My audio file contains two distinct AAC 228 Kbps stereo tracks. One is in English and the other Spanish.

My GET request returns the audio file no problem. However, it defaults to the first source on the file (in this case spanish).

Ultimately, I would like the end user to be able to use the browser and switch between the audio sources as needed. I already am aware that most browsers do not currently support the audioTracks API available through HTML5 and I would prefer not to limit my end users to IE or Edge.

This brings me to using the Web Audio API to try and manipulate the streams manually.

For testing my current HTML is just a play button.

<md-button class="md-raised" ng-click="audioActions()">Audio options</md-button>

My current .js is as follows:

var context;

window.addEventListener('load', init, false);
function init() {
    try {
        // Fix up for prefixing
        window.AudioContext = window.AudioContext||window.webkitAudioContext;
        context = new AudioContext();
    }
    catch(e) {
        alert('Web Audio API is not supported in this browser');
    }
}

var onError = function() {
    $log.info("ERROR");
}

$scope.audioActions = function() {
    var request = new XMLHttpRequest();
    request.open('GET', 'http://localhost...', true);
    request.responseType = 'arraybuffer';

    // Decode asynchronously
    request.onload = function() {
        context.decodeAudioData(request.response, function(buffer) {
            $log.info(buffer)

            var source = context.createBufferSource();
            source.buffer = buffer;
            source.connect(context.destination);
            source.start(0);
        }, onError);
    }

    request.send();
};

This works for streaming the initial audio and works the same as if I were to use the <audio> tag.

Is there a way to be able to switch between the audio streams within the buffer?

Below is dump from ffmpeg -i on my file:

[mov,mp4,m4a,3gp,3g2,mj2 @ 0000000000ea6920] stream 0, timescale not set
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'E:\ServerFiles\tmp\audioExample.m4a':
  Metadata:
    major_brand     : M4A
    minor_version   : 0
    compatible_brands: M4A mp42isom
    creation_time   : 1982-08-11T07:52:37.000000Z
    title           : Test
    artist          : Audio Example
    album_artist    : Audio Example
    album           : Example
    genre           : Test
    track           : 1/20
    disc            : 1/1
    compilation     : 0
    gapless_playback: 0
    date            : 2017-08-16T08:00:00Z
    account_id      : 
    rating          : 0
    account_type    : 0
    season_number   : 0
    episode_sort    : 0
    media_type      : 1
    purchase_date   : 2017-08-16 15:59:27
    sort_album      : Audio Example
    composer        : Example
  Duration: 00:02:59.00, start: 0.000000, bitrate: 160 kb/s
    Stream #0:0: Audio: aac (HE-AAC), 48000 Hz, stereo, fltp (default)
    Stream #0:1: Audio: aac (HE-AAC), 48000 Hz, stereo, fltp
1 Answers
Related