我正在使用 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.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:我使用的是 Cordova 3.3 和 XCode 5.0.2
希望这对您有所帮助。
关于ios - Cordova 3.3 使用 Media API 暂停和恢复录制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21380234/