iOS开发播放文本

iOS7以后AVSpeechSynthesizer苹果开放了这个类   它可以将文本转化成声音并播放;

下面的是我的封装使用起来特别简单;

//

//  TTSSpeak.h

//  SayLoveYou

//

//  Created by zyyt on 16/7/1.

//

//

 

/*

 

 汉语:             zh-CN      (普通话)

 粤语:             zh-HK

 英语:                en-US

 俄语:              ru-RU

 日语:              ja-JP

 泰语:                 th-TH

 德语:                 de-DE

 韩语:                  ko-KO

 法语:                  fr-FR

 希腊语:              gr-GR

 意大利语:          it-IT

 西班牙语:       es-ES

 阿拉伯语:       ar-Ar

 葡萄牙语:       pt-PT

 

 */

 

#import <Foundation/Foundation.h>

 

@interface TTSSpeak : NSObject

 

//所需的源语言

@property (nonatomic,copy)NSString * languageCode;

 

//播放的速度

@property (nonatomic,readonly)NSString * playRate;

 

//当前播放的源语言

@property (nonatomic,readonly)NSString * currentLanguageCode;

 

//声音大小

@property(nonatomic) NSString * volume;           // [0-1] Default = 1

 

 

+ (instancetype)shareInstance;

 

/*

 * playText:播放的文本

 *languageCode:播放的语言

 *rate:播放的速度 默认为0.7

 */

- (void)playText:(NSString*)playText withNativeLanguage:(NSString*)languageCode rate:(NSString *)rate volume:(NSString*)volume ;

 

//停止播放

- (BOOL)stopSpeaking;

 

//暂停播放

- (BOOL)pauseSpeaking;

 

//继续播放

- (BOOL)continueSpeaking;

 

@end

 

//

//  TTSSpeak.m

//  SayLoveYou

//

//  Created by zyyt on 16/7/1.

//

//

 

#import "TTSSpeak.h"

#import <AVFoundation/AVFoundation.h>

 

@interface TTSSpeak ()

 

@property (nonatomic,copy)NSString * rate;

 

@property (nonatomic,strong)AVSpeechSynthesizer * speechSynthesizer;

 

@property (nonatomic,copy)NSString * LanguageCode;

 

@end

 

 

 

@implementation TTSSpeak

 

- (AVSpeechSynthesizer *)speechSynthesizer

{

    if (_speechSynthesizer == nil) {

        _speechSynthesizer = [[AVSpeechSynthesizer alloc] init];

    }

    return _speechSynthesizer;

}

 

+ (instancetype)shareInstance

{

    

    static TTSSpeak * sharedAccountManagerInstance = nil;

    static dispatch_once_t predicate;

    dispatch_once(&predicate, ^{

        sharedAccountManagerInstance = [[TTSSpeak alloc] init];

    });

    

    return sharedAccountManagerInstance;

    

    }

 

- (void)playText:(NSString*)playText withNativeLanguage:(NSString*)languageCode rate:(NSString *)rate volume:(NSString*)volume {

    

    self.rate = rate;

    [self setLanguageCode:languageCode];

    

    AVSpeechSynthesisVoice * voice = [AVSpeechSynthesisVoice voiceWithLanguage:languageCode];

    

    AVSpeechUtterance *  utterance = [[AVSpeechUtterance alloc] initWithString:playText];

    utterance.rate *= [rate floatValue];

    

    utterance.volume = volume?[volume floatValue]:[self.volume floatValue];

    self.volume = volume?volume:self.volume;

    

    if (!rate) {

        utterance.rate *= 0.7;

 

    }

    

    utterance.voice = voice;

    [self.speechSynthesizer speakUtterance:utterance];

    

}

 

- (BOOL)stopSpeaking

{

     return  [self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];

}

 

- (BOOL)pauseSpeaking{

    

   return     [self.speechSynthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];

}

 

- (BOOL)continueSpeaking

{

    return    [self.speechSynthesizer continueSpeaking];

}

 

- (NSString *)playRate

{

    if (!self.rate) {

        return @"0.7";

    }

    return self.rate;

}

 

- (NSString *)currentLanguageCode

{

    if (!self.languageCode) {

        return @"没有设置源语言";

    }

    

    return self.languageCode;

}

 

 

 

 

@end

 

posted @ 2016-07-01 16:18  sujianbo  阅读(334)  评论(0编辑  收藏  举报