语音合成:

例如:

  • 现在语音功能在各个应用中很常见,很多语音开发的SDK,选择用那个语音SDK,成了很多样选择,其中ios提供了一个语音合成,好处是不花钱,其他的就是耗性能等等。

AVFoundation:

AVFoundation 是 iOS 中多媒体框架之一,基于 Object-C/Swift 接口,可以用来播放,编辑,重新编码音视频文件

1.播放
2. 暂停
3. 继续播放
4. 停止播放


#import <AVFoundation/AVFoundation.h>
#import "TtsCtr.h"

#pragma Delegate;
@protocol TtsCtrDelegate <NSObject>
@optional
- (void)didStartSpeechUtterance:(AVSpeechUtterance*)utterance;
- (void)didFinishSpeechUtterance:(AVSpeechUtterance*)utterance;
- (void)didPauseSpeechUtterance:(AVSpeechUtterance*)utterance;
- (void)didCancelSpeechUtterance:(AVSpeechUtterance*)utterance;
- (void)willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;

- (void)needRepeatSpeech:(AVSpeechUtterance *)utterance;
@end

@interface TtsCtr : NSObject <AVSpeechSynthesizerDelegate>


@property (nonatomic, strong) AVSpeechSynthesizer *synthesize;
@property (nonatomic, strong) AVSpeechUtterance *speechUtt;

@property (nonatomic, assign) id <TtsCtrDelegate> delegate;

//@property (nonatomic, assign) id <TtsCtr> delegate;
@property (nonatomic,readwrite) NSString *str;
@property(nonatomic,readwrite) NSString *language;


- (void)setSpeechContent:(NSString *)content;
-(void)readContenAction;

- (void)pauseSpeech;


- (void)beginSpeech;

- (void)continueSpeech;

- (void)endSpeech;

@end
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import "TtsCtr.h"
//https://blog.csdn.net/songming654/article/details/118724807
@implementation TtsCtr

static TtsCtr* tc=nil;
-(id)init
{
    self=[super init];
    
    return self;
}


+(TtsCtr *)instatanceTts
{
    if (tc==nil)
    {
        tc=[[TtsCtr alloc]init];
    }

    return tc;
}


-(void)readContenAction
{
   
    if(self.str==nil)
    {
        self.str=@"播报内容不能为空,请加入播报内容";
    }
   
   
    AVSpeechUtterance *utterance=[AVSpeechUtterance speechUtteranceWithString:self.str];
    utterance.voice=[AVSpeechSynthesisVoice  voiceWithLanguage:self.language];//zh-CN
    utterance.rate=0.4;
    utterance.pitchMultiplier=0.5;//[UserSetting ];
  
    self.synthesize =[[AVSpeechSynthesizer alloc]init];
    self.synthesize.delegate=self;
    [self.synthesize speakUtterance:utterance];
   
}
-(void) pauseSpeech
{
    [self.synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}

-(void)continueSpeech
{
    if(self.synthesize.isPaused)
    {
        [self.synthesize continueSpeaking];
        [NSThread sleepForTimeInterval:0.25f];
    }
}

-(void)endSpeech
{
    if(self.synthesize.isSpeaking)
    {
        [self.synthesize stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        [NSThread sleepForTimeInterval:0.25f];
    }
}

#pragma mark - AVSpeechSynthesizerDelegate;

//开始朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
   // NSLog(@"开始朗读->%@",utterance.speechString);
  
}
//结束朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
    //NSLog(@"结束朗读->%@",utterance.speechString);
    
}
//暂停朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{
  // NSLog(@"暂停朗读->%@",utterance.speechString);
   [self OSCallUnity:@"暂停朗读"];
}
//继续朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{
  // NSLog(@"继续朗读->%@",utterance.speechString);
    [self OSCallUnity:@"继续朗读"];
}
//取消朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{
  // NSLog(@"取消朗读->%@",utterance.speechString);
 
}
//将要播放的语音文字
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{
   // NSLog(@"Str->C = %zd->L = %zd->%@",characterRange.location,characterRange.length,utterance.speechString);
    //self.speechLabel.text = utterance.speechString;

}

@end

补充:

例如:

  • ios提供了很多语言 中文 zh-CN(中国内地),zh-TW(中国台湾),zh_HK(粤语)
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐