ios 自带文字转语音

今天有个这个需求,这东西很简单,网上一搜一大片只是记录一下!上代码

//
//  PPSoundPlayer.h
//
//  Created by pipixia on 2017/9/13.
//  Copyright © 2017年 pipixia. All rights reserved.
//



#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface PPSoundPlayer : NSObject
{
    ///声音设置
    NSMutableDictionary *soundSet;
    ///配置文件路径
    NSString *path;
}
///语速
@property (nonatomic, assign) float rate;
///音量
@property (nonatomic, assign) float volume;
///音调
@property (nonatomic, assign) float pitchMultiplier;
///自动播放
@property (nonatomic, assign) BOOL autoPlay;

+ (PPSoundPlayer *)soundPlayerInstance;

- (void)play:(NSString *)text;

- (void)writeSoundSet;

- (void)stopSpeaking;

@end
//
//  PPSoundPlayer.m
//
//  Created by pipixia on 2017/9/13.
//  Copyright © 2017年 pipixia. All rights reserved.
//


#import "PPSoundPlayer.h"

static PPSoundPlayer *soundPlayer = nil;
static AVSpeechSynthesizer *player = nil;
static dispatch_queue_t queue = NULL;

@implementation PPSoundPlayer

+ (PPSoundPlayer *)soundPlayerInstance
{
    if (soundPlayer == nil)
    {
        soundPlayer = [[PPSoundPlayer alloc] init];
        player = [[AVSpeechSynthesizer alloc] init];
        queue = dispatch_queue_create("text", NULL);
        [soundPlayer initSoundSet];
        [soundPlayer setDefault];
    }
    return soundPlayer;
}

//播放声音
- (void)play:(NSString *)text
{
    if(text)
    {
        dispatch_async(queue, ^{
            AVSpeechUtterance *u=[[AVSpeechUtterance alloc]initWithString:text];//设置要朗读的字符串
            u.voice=[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置语言
            u.volume = self.volume;  //设置音量(0.0~1.0)默认为1.0
            u.rate = self.rate;  //设置语速
            u.pitchMultiplier = self.pitchMultiplier;  //设置语调
            
            
            [player speakUtterance:u];
        });
    }
}

//初始化配置
- (void)initSoundSet
{
    path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"SoundSet.plist"];
    soundSet=[NSMutableDictionary dictionaryWithContentsOfFile:path];
    
    if(soundSet==nil)
    {
        soundSet=[NSMutableDictionary dictionary];
        [soundPlayer setDefault];
        [soundPlayer writeSoundSet];
    }
    else
    {
        self.autoPlay = [[soundSet valueForKeyPath:@"autoPlay"] boolValue];
        self.volume = [[soundSet valueForKeyPath:@"volume"] floatValue];
        self.rate = [[soundSet valueForKeyPath:@"rate"] floatValue];
        self.pitchMultiplier = [[soundSet valueForKeyPath:@"pitchMultiplier"] floatValue];
    }
}

 //恢复默认设置
- (void)setDefault
{
    self.volume = 0.7;
    self.rate = 0.5;
    self.pitchMultiplier = 1.0;
}

 //将设置写入配置文件
-(void)writeSoundSet
{
    [soundSet setValue:[NSNumber numberWithBool:self.autoPlay] forKey:@"autoPlay"];
    [soundSet setValue:[NSNumber numberWithFloat:self.volume] forKey:@"volume"];
    [soundSet setValue:[NSNumber numberWithFloat:self.rate] forKey:@"rate"];
    [soundSet setValue:[NSNumber numberWithFloat:self.pitchMultiplier] forKey:@"pitchMultiplier"];
    [soundSet writeToFile:path atomically:YES];
}

- (void)stopSpeaking{
    [player stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
@end

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 之前自己的项目中曾经使用过讯飞的文字转语音技术,但是通过实际测试,发现它的免费在线转语音不是很好,受网络...
    孤独雪域阅读 20,683评论 25 55
  • 因新项目需求,需要接入类似支付宝收款提示声----“支付宝到账xxxx元”,就查看了一些文章,苹果还是想的很周全,...
    Hyperion_J阅读 12,597评论 8 4
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,725评论 25 709
  • 手机里下载了个百度网盘的App,平时拍摄或下载的图片,在网络环境下实时上传云端,手机进水或主版烧坏,再也不担心历史...
    初刻杰阅读 263评论 0 0
  • 在客户端处理金额的时候,通常会涉及小数点后两位有效数字,如果使用float和double的时候都会因为精度...
    献国阅读 2,104评论 0 1