itaied
itaied

Reputation: 7107

Ionic how can I create an mp3 file?

I'm writing a web app with Ionic framework and I'm trying to manage a record and play sounds mechanism. I'm using the following snippet as a service:

.factory('MediaSrv', function ($q, $ionicPlatform, $window) {
    var service = {
        loadMedia: loadMedia,
        getStatusMessage: getStatusMessage,
        getErrorMessage: getErrorMessage
    };

    function loadMedia (src, onError, onStatus, onStop) {
        var defer = $q.defer();

        $ionicPlatform.ready(function () {
            var mediaSuccess = function () {
                if (onStop) { onStop(); }
            };

            var mediaError = function (err) {
                _logError(src, err);
                if (onError) { onError(err); }
            };

            var mediaStatus = function (status) {
                if (onStatus) { onStatus(status); }
            };

            if ($ionicPlatform.is('android')) {
                src = '/android_asset/www/' + src;
            }

            defer.resolve(new $window.Media(src, mediaSuccess, mediaError, mediaStatus));
        });

        return defer.promise;
    }

    ...

    return service;
});

I'm able to play an existing .mp3 file, but I cannot record on a non-existing file. I thought it would create the file by itself if the file wasn't found. How can I create an empty .mp3 file for recording?

Upvotes: 4

Views: 630

Answers (2)

Phil Hannent
Phil Hannent

Reputation: 12317

Ionic can use the Cordova plugins since its built on top of it.

You can use the media-capture plugin to capture audio, however I have found these record as AMR files.

From the [documentation][1]:

// capture callback
var captureSuccess = function(mediaFiles) {
    var i, path, len;
    for (i = 0, len = mediaFiles.length; i < len; i += 1) {
        path = mediaFiles[i].fullPath;
        // do something interesting with the file
    }
};

// capture error callback
var captureError = function(error) {
    navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error');
};

// start audio capture
navigator.device.capture.captureAudio(captureSuccess, captureError, {limit:2});


  [1]: http://docs.phonegap.com/en/edge/cordova_media_capture_capture.md.html

Upvotes: 0

BenH
BenH

Reputation: 436

Creating a file is a server function. You would need a node server using fs to create a file.

From Ionic's website:

Think of Ionic as the front-end UI framework that handles all of the look and feel and UI interactions your app needs in order to be compelling. Kind of like "Bootstrap for Native," but with support for a broad range of common native mobile components, slick animations, and beautiful design.

Upvotes: 1

Related Questions