Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在文档目录中重新保存(复制)音频文件并发送重新保存的音频文件?_Ios_Objective C_Avaudioplayer_Audio Recording - Fatal编程技术网

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

Ios 如何在文档目录中重新保存(复制)音频文件并发送重新保存的音频文件?,ios,objective-c,avaudioplayer,audio-recording,Ios,Objective C,Avaudioplayer,Audio Recording,我正在录制音频文件并发送到服务器。录制、停止、播放和发送工作正常。但我的问题是,若我单击“停止”按钮,我想在docs dir中用新名称重新保存该音频文件,并将该音频文件发送到服务器。我的代码是 // File path where the recording will be saved on the iOS device audioFilePath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponen

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

// 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];
}
如果我用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);
当我点击停止按钮时,我得到了错误信息
错误:无法完成该操作。(OSStatus错误2003334207。)

您不能更改该音频文件的名称。您可以使用NSFileManager类使用新名称创建该音频文件的副本

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

很好,谢谢你。我确实喜欢这个。。NSLog(@“%@”,audioRecorder.url);//RecordAudio.wav。NSString*urlString=[audioRecorder.url.absoluteString-byreplacingoccurrencesofstring:@“RecordAudio.wav”和string:@.RecordAudio5.wav“];self.sendingURL=[NSURL URLWithString:urlString];NSLog(@“%@”,self.sendingURL);现在我正在发送一个名为“RecordAudio5.wav”的新名称,它正在工作。很好,谢谢你的回复。。。