5

I'm trying to write a small Cordova app (mainly on Android 6 for now) which records and saves an audio file using the Cordova Media-Plugin.

var media;
var mediaFile = "mediafile.wav"

[...]

media = new Media('file:///sdcard/' + mediaFile);
media.startRecord();

[...]

media.stopRecord();
media.release;

So far so good - this works just fine. I am able to record, save and play back the file. The next step would be to analyze what main frequencies there are in said audio file. My idea was to use the HTML5 AudioContext and the AnalyserNode to do so.

var context;
var analyser;
var sourceNode; 

[...]

// inside my onDeviceReady-function
var AudioContext = (window.AudioContext || 
                    window.webkitAudioContext ||
                    window.mozAudioContext ||
                    window.oAudioContext ||
                    window.msAudioContext);
if (AudioContext) {
    context = new AudioContext();
}
else {
    alert('Browser does not support Web-Audio API');
}

analyser = context.createAnalyser();
sourceNode = context.createBufferSource();
sourceNode.connect(analyser);

For accessing the file, I'm using the Cordova File-Plugin.

function onFileSystemSuccess(fs) {
    fileSystem = fs;
    [...]
}

// inside my onDeviceReady-function
window.resolveLocalFileSystemURL('file:///sdcard/', onFileSystemSuccess, errorHandler);

[...]

fileSystem.getFile(mediaFile, { create: false }, readFile, errorHandler);

But now I'm having a bit of an issue when it comes to reading and analyzing the file.

function readFile(f) {
    f.file(function (file) {
        var reader = new FileReader();
        reader.onloadend = function (e) {
            context.decodeAudioData(reader.result, function (decodedData) {
                [...]
            }, errorHandler);
        };

        reader.readAsArrayBuffer(file);
    }, errorHandler);
}

The decodeAudioData throws an Error "unable to decode audio file". When I just load a random .wav-file it works just fine, but not with my self recorded files.

I'm pretty new to this subject, so I don't know what to think of that... If anyone has an idea, that would be so great.

Thank you.

pheuel
  • 51
  • 3

0 Answers0