gpt4 book ai didi

objective-c - 保存录制的AVAudioRecorder声音文件:现在呢? (iOS,Xcode 4)

转载 作者:行者123 更新时间:2023-12-02 22:35:17 24 4
gpt4 key购买 nike

在我的应用程序中,我希望用户能够录制一个声音文件并进行播放,然后保存声音文件以备后用。我使用this tutorial设置了播放和录制界面,但是本教程省略了关键部分:如何将声音永久保存到磁盘上?

另外,当您在这里时,如何设置声音文件的最长录制时间?我不希望声音文件的长度超过30秒。

谢谢,希望我能解决所有问题。

最佳答案

Voice Record Demo是一个很好的例子。它使用Cocos2D作为UI,但是如果您只看一下HelloWorldScene.m类,它将包含所需的所有代码:

  • 创建一个新的音频 session
  • 检查麦克风是否已连接
  • 开始录制
  • 停止记录
  • 保存录制的音频文件
  • 播放保存的语音文件

  • 初始化音频 session 后,可以使用以下方法将录音保存到给定的文件名:
    -(void) saveAudioFileNamed:(NSString *)filename {

    destinationString = [[self documentsPath] stringByAppendingPathComponent:filename];
    NSLog(@"%@", destinationString);
    NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];

    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
    [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
    [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
    [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
    nil];

    NSError *error;

    recorder = [[AVAudioRecorder alloc] initWithURL:destinationURL settings:settings error:&error];
    recorder.delegate = self;
    }

    关于objective-c - 保存录制的AVAudioRecorder声音文件:现在呢? (iOS,Xcode 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9358295/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com