通过扩展 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.media的www文件夹下的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:我正在使用带有 XCode 5.0.2 的 Cordova 3.3
希望这会有所帮助。