美文网首页iOS-移动架构师ios
ios wav格式语音文件转文字

ios wav格式语音文件转文字

作者: 瑷岚 | 来源:发表于2018-01-24 14:33 被阅读457次

一、录音设置

    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];

    //设置录音格式  AVFormatIDKey==kAudioFormatLinearPCM

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];

    //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)

    [recordSetting setValue:[NSNumber numberWithFloat:16000] forKey:AVSampleRateKey];

    //录音通道数  1 或 2

    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

    //线性采样位数  8、16、24、32

    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];

    //录音的质量

    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];


    NSString *strUrl = [NSString stringWithFormat:@"%@/%@.wav", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], [NSString uuid]];

    NSURL *url = [NSURL fileURLWithPath:strUrl];

    self.recordUrlKey = strUrl;

//    self.audioRecord.filePath = strUrl;


    NSError *error;

    //初始化

    _recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];

    //开启音量检测

    self.recorder.meteringEnabled = YES;


    if ([self.recorder prepareToRecord]){

        return YES;

    }

二、wav 转 m4a 格式

NSURL *audioPath = [NSURL fileURLWithPath:path];

    AVURLAsset* audioAsset = [[AVURLAsset alloc] initWithURL:audioPath options:nil];

    AVAssetExportSession* exportSession = [[AVAssetExportSession alloc] initWithAsset:audioAsset presetName:AVAssetExportPresetAppleM4A];

    NSURL* exportURL = [NSURL fileURLWithPath:[[path componentsSeparatedByString:@".wav"] objectAtIndex:0]];

    NSURL* destinationURL = [exportURL URLByAppendingPathExtension:@"m4a"];

    exportSession.outputURL = destinationURL;

    exportSession.outputFileType = AVFileTypeAppleM4A;

    [exportSession exportAsynchronouslyWithCompletionHandler:^{

        if (AVAssetExportSessionStatusCompleted == exportSession.status) {

        } else if (AVAssetExportSessionStatusFailed == exportSession.status) {

        } else {

        }

        didFinish();

    }];

三、m4a 翻译

NSURL *exportURL = [NSURL fileURLWithPath:[[path componentsSeparatedByString:@".wav"] objectAtIndex:0]];

            NSURL *destinationURL = [exportURL URLByAppendingPathExtension:@"m4a"];

            NSLocale *local =[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

            SFSpeechRecognizer *localRecognizer =[[SFSpeechRecognizer alloc] initWithLocale:local];

            NSURL *url = destinationURL;

            if (!url) { [AlertUtil showAlertWithText:@"转换失败"]; return;}

            SFSpeechURLRecognitionRequest *res =[[SFSpeechURLRecognitionRequest alloc] initWithURL:url];

            __weak typeof(self) weakSelf = self;

            [localRecognizer recognitionTaskWithRequest:res resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {

                if (error) {

                    NSLog(@"------------------------语音识别解析======失败,%@",error);

                    [AlertUtil showAlertWithText:@"转换失败"];

                } else {

                    NSLog(@"------------------------语音识别解析======成功,%@",result.bestTranscription.formattedString);

                    [weakSelf showWord:result.bestTranscription.formattedString];

                }

            }];

相关文章

  • ios wav格式语音文件转文字

    一、录音设置 NSMutableDictionary *recordSetting = [[NSMutableD...

  • 转码

    ios 不能播放amr格式。ios 录制的音频位wav格式。 wav转amr可以使用opencore框架

  • vue 移动端ios语音播放WAV格式语音文件

  • 使用 iTunes转换音频文件格式

    wav转mp3 1. 选择需要转换的wav格式的音频文件,使用iTunes打开 2. 音频文件会默认导入资料库...

  • [kalid] pcm2wav

    20180827 qzd pcm文件转wav文件时,主要是在pcm文件加入wav的头。wav的文件头包含wav标示...

  • 音乐播放学习

    一、常见音频格式 WAV文件,WAV文件格式是一种由微软和IBM联合开发的用于音频 数字存储的标准,WAV文件...

  • 音乐播放学习

    一、常见音频格式 WAV文件,WAV文件格式是一种由微软和IBM联合开发的用于音频 数字存储的标准,WAV文件...

  • Window文字转语音,转wav

    老旧玩意,突然要用,写个小工具整理成wav .net framework 引用COMMicrosoft Speec...

  • [转]iOS

    iOS的一些有用的文章,统一搜集到这里,方便查阅。 音频 iOS音频播放 系列 WAV文件格式终极解析 pcm文件...

  • ios 使用AVFoundation从视频中提取音频

    ios 使用AVFoundation从视频中提取音频 在导出音频文件时候,使用 m4a格式或者wav格式,总会遇到...

网友评论

    本文标题:ios wav格式语音文件转文字

    本文链接:https://www.haomeiwen.com/subject/imnrdttx.html