簡體   English   中英

無法使用AVAudioRecorder永久保存音頻

[英]Can't permanently save audio with AVAudioRecorder

我正在制作的應用程序有多個視圖,每個視圖都有一個記錄,停止和播放按鈕。 這個想法是用戶可以為每個視圖錄制到不同的聲音文件。

我可以在每個視圖上記錄和播放聲音,但是當我離開視圖導航然后向后導航時,聲音消失了。

很抱歉在下面包含這么多代碼,但這是我需要深入了解的內容。

代表人

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>
#import <AVFoundation/AVFoundation.h>

@interface humptyDumptyAppDelegate : UIResponder <UIApplicationDelegate>

{
    NSArray *dirPaths;
    NSString *docsDir;
    NSString *soundFilePathPage1;                               
    NSString *soundFilePathPage2;
    NSString *soundFilePathPage3;
    NSString *soundFilePathPage4;
    NSString *soundFilePathPage5;
    NSString *soundFilePathPage6;

}

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) AVAudioRecorder *audioRecorder;
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

//example getter and setter functions
- (NSArray*) getDirPaths;
- (void) setDirPaths:(NSArray*)myDirPath;
- (NSString*) getDocsDir;
- (NSString*) soundFilePathForPageNumber:(int)pageNumber;

@end

代表

#import "humptyDumptyAppDelegate.h"

@implementation humptyDumptyAppDelegate

@synthesize window = _window;
@synthesize audioPlayer;
@synthesize audioRecorder;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];

    soundFilePathPage1 = [docsDir
                          stringByAppendingPathComponent:@"audiopage1.caf"];
    soundFilePathPage2 = [docsDir
                          stringByAppendingPathComponent:@"page2.caf"];
    soundFilePathPage3 = [docsDir
                          stringByAppendingPathComponent:@"page3.caf"];
    soundFilePathPage4 = [docsDir
                          stringByAppendingPathComponent:@"page4.caf"];
    soundFilePathPage5 = [docsDir
                          stringByAppendingPathComponent:@"page5.caf"];
    soundFilePathPage6 = [docsDir
                          stringByAppendingPathComponent:@"page6.caf"];
    return YES;

}
//getter function
- (NSArray*) getDirPaths{    
    return dirPaths;
}
//setter function
- (void) setDirPaths:(NSArray*)myDirPath{
    dirPaths = myDirPath;
}
// get docs directory
-(NSString*) getDocsDir{
    return docsDir;
}
// get sound file for page, passing the page number as an argument
-(NSString*) soundFilePathForPageNumber:(int)pageNumber{
    switch (pageNumber) {
        case 1:
            return soundFilePathPage1;
            break;
        case 2:
            return soundFilePathPage2;
            break;
        case 3:
            return soundFilePathPage3;
            break;
        case 4:
            return soundFilePathPage4;
            break;
        case 5:
            return soundFilePathPage5;
            break;
        case 6:
            return soundFilePathPage6;
            break;
    }
    return nil;
}

第1頁

//this is called in viewDidLoad
-(void) prepareForAudioRecording
{
    btnPlay.enabled = NO;
    btnStop.enabled = NO;

    int page = 1;
    NSString *audioFilePath = [appDelegate soundFilePathForPageNumber:page]; 
    NSURL *soundFileURL = [NSURL fileURLWithPath:audioFilePath];
    NSError *error;
    NSDictionary *recordSettings = [NSDictionary 
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16], 
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2], 
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0], 
                                    AVSampleRateKey,
                                    nil];
    appDelegate.audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:recordSettings
                     error:&error];

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);

    } else {
        [appDelegate.audioRecorder prepareToRecord];
    }
}

- (IBAction)recordAudio:(id)sender {
    if (!appDelegate.audioRecorder.recording)
    {
        btnPlay.enabled = NO;
        btnStop.enabled = YES;
        [appDelegate.audioRecorder record];

    }
}

- (IBAction)stopAudio:(id)sender {
    btnStop.enabled = NO;
    btnPlay.enabled = YES;
    btnRecord.enabled = YES;


    if (appDelegate.audioRecorder.recording)
    {
        [appDelegate.audioRecorder stop];
        [self audioRecorderDidFinishRecording:appDelegate.audioRecorder successfully:YES];
    } else if (appDelegate.audioPlayer.playing) {
        [appDelegate.audioPlayer stop];

    }
}

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
    if (flag == YES){
        NSLog(@"finished recording");
        [appDelegate.audioPlayer.data writeToFile:[appDelegate soundFilePathForPageNumber:1] atomically:YES];
    }
}

就像我說的那樣,我對所包含的代碼數量感到抱歉,但是我不確定問題出在哪里。 我打電話writeToFile的方法audioRecorderDidFinishRecording:方法。 我不知道這是否正確,但是我感覺這不是問題的根源。

請幫忙!!

此代碼保存到音頻文件

將文件復制到文檔目錄怎么辦

BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@" sound.caf"];
    success = [fileManager fileExistsAtPath:writableDBPath];

    if (!success){
        // The writable database does not exist, so copy the default to the appropriate location.
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sound.caf"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

        NSError *attributesError;
        NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:writableDBPath error:&attributesError];

        NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
        long long fileSize = [fileSizeNumber longLongValue];
        NSLog(@"file size: %lld",fileSize);

        if (!success) {
            NSLog(@"Failed to create writable database file with message: %@", [error localizedDescription]);
        }

    }

事實證明,問題出在我的其中一個頁面中,我的viewDidAppearprepareForAudioRecording ,它會自動覆蓋保存的音頻。 將其移動到viewDidLoad

暫無
暫無

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

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