ios - Cordova 3.3 使用 Media API 暂停和恢复录制

标签 ios cordova phonegap-plugins audio-recording cordova-3

我正在使用 Cordova 的媒体 API 为 iOS 5.0+ 制作一个简单的录音机。我让用户能够开始-暂停-恢复-停止录制音频。

我定义的三个按钮是

开始录制

停止录制

暂停/恢复录制

我能够成功开始和停止录制。我无法做的是暂停录音,然后再次恢复。

我提到了 Media API examples Cordova 并在我的代码中也使用了一些。

请帮忙!!!

最佳答案

通过扩展 Media API 插件,我能够使用 Media API 调用记录器的暂停和恢复功能。

同样的解决方法如下:

媒体 API 插件的原生部分

将以下方法添加到CDVSound.m

CDVSound.m

- (void)resumeRecordingAudio:(CDVInvokedUrlCommand*)command
 {
    NSString* mediaId = [command.arguments objectAtIndex:0];

    CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
    NSString* jsString = nil;

    if ((audioFile != nil) && (audioFile.recorder != nil)) {
        NSLog(@"Resumed recording audio sample '%@'", audioFile.resourcePath);
        [audioFile.recorder record];
        // no callback - that will happen in audioRecorderDidFinishRecording
    }
    // ignore if no media recording
    if (jsString) {
        [self.commandDelegate evalJs:jsString];
    }
}

- (void)pauseRecordingAudio:(CDVInvokedUrlCommand*)command
 {
    NSString* mediaId = [command.arguments objectAtIndex:0];

    CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
    NSString* jsString = nil;

    if ((audioFile != nil) && (audioFile.recorder != nil)) {
        NSLog(@"Paused recording audio sample '%@'", audioFile.resourcePath);
        [audioFile.recorder pause];
        // no callback - that will happen in audioRecorderDidFinishRecording
    }
    // ignore if no media recording
    if (jsString) {
        [self.commandDelegate evalJs:jsString];
    }
}

媒体 API 插件的 JavaScript 部分

将以下代码添加到 org.apache.cordova.mediawww 文件夹下的 Media.js 文件中。

确保在 Media.prototype.startRecord 函数下方添加代码。

/**
 * Pause recording audio file.
 */
Media.prototype.pauseRecord = function() {
exec(null, this.errorCallback, "Media", "pauseRecordingAudio", [this.id]);
};

/**
* Resume recording audio file.
*/
Media.prototype.resumeRecord = function() {
exec(null, this.errorCallback, "Media", "resumeRecordingAudio", [this.id]);
};

扩展插件后,只需调用nameOfRecorder.pauseRecord();和nameOfRecorder.resumeRecord();根据您的需要和要求。

PS:我使用的是 Cordova 3.3 和 XCode 5.0.2

希望这对您有所帮助。

关于ios - Cordova 3.3 使用 Media API 暂停和恢复录制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21380234/

相关文章:

ios - 使用 PhoneGap Build 3 时隐藏 iOS 7 中的状态栏?

ios - 从 Swift 中的背景颜色确定文本颜色?

ios - 在静态库中使用弱引用

ios - Swift 在 modalPresentationStyle 中隐藏 UIView

android - 创建/打开 Cordova 项目时,Visual Studio 2015 挂起

android - Cordova : Command failed with exit code EACCES

ios - Xamarin iOS自定义通知声音

android - Cordova 无法使用 cordova-plugin-local-notifications 构建

java - 从 Android Phonegap 插件访问应用程序布局

android - ionic/cordova - 我的应用程序只支持 297 台设备