ios 自帶文字轉(zhuǎn)語音

今天有個(gè)這個(gè)需求,這東西很簡(jiǎn)單,網(wǎng)上一搜一大片只是記錄一下!上代碼

//
//  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
{
    ///聲音設(shè)置
    NSMutableDictionary *soundSet;
    ///配置文件路徑
    NSString *path;
}
///語速
@property (nonatomic, assign) float rate;
///音量
@property (nonatomic, assign) float volume;
///音調(diào)
@property (nonatomic, assign) float pitchMultiplier;
///自動(dòng)播放
@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];//設(shè)置要朗讀的字符串
            u.voice=[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//設(shè)置語言
            u.volume = self.volume;  //設(shè)置音量(0.0~1.0)默認(rèn)為1.0
            u.rate = self.rate;  //設(shè)置語速
            u.pitchMultiplier = self.pitchMultiplier;  //設(shè)置語調(diào)
            
            
            [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];
    }
}

 //恢復(fù)默認(rèn)設(shè)置
- (void)setDefault
{
    self.volume = 0.7;
    self.rate = 0.5;
    self.pitchMultiplier = 1.0;
}

 //將設(shè)置寫入配置文件
-(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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 前言 之前自己的項(xiàng)目中曾經(jīng)使用過訊飛的文字轉(zhuǎn)語音技術(shù),但是通過實(shí)際測(cè)試,發(fā)現(xiàn)它的免費(fèi)在線轉(zhuǎn)語音不是很好,受網(wǎng)絡(luò)...
    孤獨(dú)雪域閱讀 20,561評(píng)論 25 55
  • 因新項(xiàng)目需求,需要接入類似支付寶收款提示聲----“支付寶到賬xxxx元”,就查看了一些文章,蘋果還是想的很周全,...
    Hyperion_J閱讀 12,546評(píng)論 8 4
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,809評(píng)論 25 708
  • 手機(jī)里下載了個(gè)百度網(wǎng)盤的App,平時(shí)拍攝或下載的圖片,在網(wǎng)絡(luò)環(huán)境下實(shí)時(shí)上傳云端,手機(jī)進(jìn)水或主版燒壞,再也不擔(dān)心歷史...
    初刻杰閱讀 259評(píng)論 0 0
  • 在客戶端處理金額的時(shí)候,通常會(huì)涉及小數(shù)點(diǎn)后兩位有效數(shù)字,如果使用float和double的時(shí)候都會(huì)因?yàn)榫?..
    獻(xiàn)國(guó)閱讀 2,083評(píng)論 0 1