如何以 .m4a 格式录制语音

发布于 2024-10-04 14:29:13 字数 100 浏览 4 评论 0原文

我已经创建了一个 iPhone 应用程序来录制。它将记录在 .caf 文件中。

但我想以.m4a 格式录制。

请帮助我做到这一点。

谢谢。

Already I have created an iPhone application to record. It will record in .caf file.

But I want to record in .m4a format.

Please help me to do this.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹏雨一样淡蓝的深情 2024-10-11 14:29:18

以下是录制 m4a 音频文件的有效 SWIFT 代码。请记住,在 iOS 中找到正确的格式参数来生成可用的音频文件确实很难找到。经过多次尝试和错误,我发现这种组合是有效的。我希望它可以节省您的时间,祝您愉快!

let recordSettings: [String : AnyObject] = [AVSampleRateKey : NSNumber(float: Float(16000)),
                                                AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)), 
        AVNumberOfChannelsKey : NSNumber(int: 1),
        AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Low.rawValue))]

func initializeAudioSession(){


    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try audioRecorder = AVAudioRecorder(URL: self.directoryURL()!,
                                            settings: recordSettings)
        audioRecorder.delegate = self
        audioRecorder.meteringEnabled = true
        audioRecorder.prepareToRecord()
    } catch let error as NSError{
        print("ERROR Initializing the AudioRecorder - "+error.description)
    }
}

func recordSpeechM4A(){
        if !audioRecorder.recording {
            let audioSession = AVAudioSession.sharedInstance()
            do {
                try audioSession.setActive(true)
                audioRecorder.record()
                print("RECORDING")
            } catch {
            }
        }
    }

func directoryURL() -> NSURL { //filename helper method
        let fileManager = NSFileManager.defaultManager()
        let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
        filepath = urls[0]
        let documentDirectory = urls[0] as NSURL
        print("STORAGE DIR: "+documentDirectory.description)
        //print("---filepath: "+(filepath?.description)!)
        let soundURL = documentDirectory.URLByAppendingPathComponent("recordedAudio.m4a") //.m4a
        print("SAVING FILE: "+soundURL.description)
        return soundURL
    }

Here is the working SWIFT code to record m4a Audio files. Bear in mind that hitting the right format parameters that produce usable audio files in iOS is really painful to find. I found that this combination works, after much trial and error. I hope it saves you time, enjoy!

let recordSettings: [String : AnyObject] = [AVSampleRateKey : NSNumber(float: Float(16000)),
                                                AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)), 
        AVNumberOfChannelsKey : NSNumber(int: 1),
        AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Low.rawValue))]

func initializeAudioSession(){


    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try audioRecorder = AVAudioRecorder(URL: self.directoryURL()!,
                                            settings: recordSettings)
        audioRecorder.delegate = self
        audioRecorder.meteringEnabled = true
        audioRecorder.prepareToRecord()
    } catch let error as NSError{
        print("ERROR Initializing the AudioRecorder - "+error.description)
    }
}

func recordSpeechM4A(){
        if !audioRecorder.recording {
            let audioSession = AVAudioSession.sharedInstance()
            do {
                try audioSession.setActive(true)
                audioRecorder.record()
                print("RECORDING")
            } catch {
            }
        }
    }

func directoryURL() -> NSURL { //filename helper method
        let fileManager = NSFileManager.defaultManager()
        let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
        filepath = urls[0]
        let documentDirectory = urls[0] as NSURL
        print("STORAGE DIR: "+documentDirectory.description)
        //print("---filepath: "+(filepath?.description)!)
        let soundURL = documentDirectory.URLByAppendingPathComponent("recordedAudio.m4a") //.m4a
        print("SAVING FILE: "+soundURL.description)
        return soundURL
    }
瞳孔里扚悲伤 2024-10-11 14:29:17

下面是一个替代代码示例,它将文件编码为 m4a 中的 AAC:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSURL *tmpFileUrl = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"tmp.m4a"]];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                        [NSNumber numberWithFloat:16000.0], AVSampleRateKey,
                        [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                        nil];
NSError *error = nil;
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:tmpFileUrl settings:recordSettings error:&error];
[recorder prepareToRecord];

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];

[recorder record];

然后结束我使用的录制:

[recorder stop];
AVAudioSession *session = [AVAudioSession sharedInstance];
int flags = AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation;
[session setActive:NO withFlags:flags error:nil];

然后可以使用 'tmpFileUrl' 处的文件。

Here is an alternative code sample that will encode the file as AAC inside an m4a:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSURL *tmpFileUrl = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"tmp.m4a"]];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                        [NSNumber numberWithFloat:16000.0], AVSampleRateKey,
                        [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                        nil];
NSError *error = nil;
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:tmpFileUrl settings:recordSettings error:&error];
[recorder prepareToRecord];

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];

[recorder record];

Then to end the recording I used:

[recorder stop];
AVAudioSession *session = [AVAudioSession sharedInstance];
int flags = AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation;
[session setActive:NO withFlags:flags error:nil];

Then the file at 'tmpFileUrl' can be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文