ios - 如何重新保存(复制)文档目录中的音频文件并发送重新保存的音频文件?

标签 ios objective-c avaudioplayer audio-recording

我正在录制音频文件并发送到服务器。录音、停止、播放和发送工作正常。但我的问题是,如果我单击停止按钮,我想在文档目录中使用新名称重新保存该音频文件并将该音频文件发送到服务器。我的代码是...

// File path where the recording will be saved on the iOS device
audioFilePath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"RecordAudio.wav"];
NSLog(@"%@", audioFilePath);

NSURL *outputFileURL = [NSURL fileURLWithPath:audioFilePath];

// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];

// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:[NSNumber numberWithInt: 16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue:[NSNumber numberWithBool: NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue:[NSNumber numberWithBool: NO] forKey:AVLinearPCMIsFloatKey];
[recordSetting setValue:[NSNumber numberWithInt: AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];

// Initiate and prepare the recorder
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:&error];
audioRecorder.delegate = self;
audioRecorder.meteringEnabled = YES;

// Deal with any errors
if (error)
NSLog(@"error: %@", [error localizedDescription]);
else
[audioRecorder prepareToRecord];

// Check to see if we have permission to use the microphone.
if ([session respondsToSelector:@selector(requestRecordPermission:)]) {
    [session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
        if (granted) {
            NSLog(@"Mic access granted");
        }
        else {
            NSLog(@"Mic access NOT granted");
        }
    }];
}

-(NSURL *)applicationDocumentsDirectory {

NSLog(@"%@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                     inDomains:NSUserDomainMask] lastObject]);
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                               inDomains:NSUserDomainMask] lastObject];
}

- (IBAction)record:(id)sender {

NSLog(@"Started Recording");
if (!audioRecorder.recording)
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryRecord error:nil];
    [session setActive:YES error:nil];

    // Start recording
    [audioRecorder record];
}
}

- (IBAction)stop:(id)sender {

NSLog(@"Stop Recording");
if ([audioRecorder isRecording])
{
    [audioRecorder stop];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    [audioSession setActive:NO error:nil];

}
else if (audioPlayer.playing)
[audioPlayer stop];
}

如果我在停止方法中编写这段代码..

NSLog(@"%@", audioRecorder.url); // RecordAudio.wav
NSString *urlString = [audioRecorder.url.absoluteString stringByReplacingOccurrencesOfString:@"RecordAudio.wav" withString:@"RecordAudio2.wav"];
self.sendingURL = [NSURL URLWithString:urlString];
NSLog(@"%@", self.sendingURL);

当我点击停止按钮时出现错误 错误:操作无法完成。 (操作系统状态错误 2003334207。)

最佳答案

您无法更改该音频文件的名称。您可以做的是使用 NSFileManager 类使用您的新名称创建该音频文件的副本

 NSFileManager *fManager = [NSFileManager defaultManager];
    [fManager copyItemAtURL:oldURL toURL:newURL error:error];

关于ios - 如何重新保存(复制)文档目录中的音频文件并发送重新保存的音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45769606/

相关文章:

android - 通过浏览器的 native Facebook 登录体验

objective-c - 在结构中传递指向指针的指针时出现 EXC_BAD_ACCESS?

iphone - 使用 NSAttributedString 为 UILabel 设置突出显示的文本颜色。?

ios - AVAudioPlayer:简单的声音不播放

ios - 如何上传用户个人资料图片以及如何从其他用户设备获取该个人资料图片?

iphone - iPad:如何在不替换后退按钮的情况下向导航栏添加标签?

ios - 当玩家点击屏幕开始游戏时如何开始基于时间的计分

objective-c - 为什么这个 NSMutableArray 在初始化一个 NSMutableDictionary 时会丢失一个成员?

ios - Cordova 媒体插件未实现audioPlayerEndInterruption

ios - 只播放一次声音,条件