Ionic如何创建mp3文件?

时间:2021-05-05 15:02:25

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:

我正在用Ionic框架编写一个Web应用程序,我正在尝试管理一个记录和播放声音机制。我使用以下代码段作为服务:

.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?

我可以播放现有的.mp3文件,但我无法在不存在的文件上录制。我认为如果找不到文件,它会自己创建文件。如何创建一个空的.mp3文件进行录制?

2 个解决方案

#1


1  

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

创建文件是服务器功能。您需要使用fs的节点服务器来创建文件。

From Ionic's website:

来自Ionic的网站:

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.

将Ionic视为前端UI框架,可以处理应用程序所需的所有外观和UI交互,以便引人注目。有点像“Bootstrap for Native”,但支持广泛的常见原生移动组件,光滑的动画和漂亮的设计。

#2


0  

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

Ionic可以使用Cordova插件,因为它是建立在它之上的。

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

您可以使用媒体捕获插件捕获音频,但我发现这些记录为AMR文件。

From the [documentation][1]:

来自[文档] [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

#1


1  

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

创建文件是服务器功能。您需要使用fs的节点服务器来创建文件。

From Ionic's website:

来自Ionic的网站:

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.

将Ionic视为前端UI框架,可以处理应用程序所需的所有外观和UI交互,以便引人注目。有点像“Bootstrap for Native”,但支持广泛的常见原生移动组件,光滑的动画和漂亮的设计。

#2


0  

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

Ionic可以使用Cordova插件,因为它是建立在它之上的。

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

您可以使用媒体捕获插件捕获音频,但我发现这些记录为AMR文件。

From the [documentation][1]:

来自[文档] [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