簡體   English   中英

如何將iPhone音頻文件保存在文檔目錄中

[英]how to save the iphone audio file in document directory

我已經在應用程序中實現了錄音,但是我需要知道如何將錄音文件保存在文檔目錄中以及如何在其他類中檢索相同的文件來播放音頻。

這是源代碼。

-(void)startPushed
{
        NSMutableDictionary *rs = [[NSMutableDictionary alloc] init];
        [rs setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
        [rs setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [rs setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
        recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];

        recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:rs error:&error];
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];


        NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
        NSString *docDir = [docPath objectAtIndex:0];
        NSString *fullPath = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", recordedTmpFile]];
        NSLog(@"%@", fullPath);
}

 -(void)playbackPushed
    {
    AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
        NSLog(@"Play Path:%@", recordedTmpFile);
        [avPlayer prepareToPlay];
        [avPlayer play];
    }

請告訴我如何在文檔目錄中讀寫音頻文件(.caf格式),並提供其他班級的音頻播放信息。

提前致謝

使用此代碼訪問iOS中的文檔目錄

-(NSURL *)documentDirectory{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

}

並使用NSFileManager將文件復制到路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM