Skip to content

Commit

Permalink
first change
Browse files Browse the repository at this point in the history
  • Loading branch information
wqforever committed Mar 25, 2017
1 parent 526ce96 commit cf66558
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 文字转语音.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
30DCEEF41E86B68A00442696 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 30DCEEF21E86B68A00442696 /* LaunchScreen.storyboard */; };
30DCEEFF1E86B68A00442696 /* _____Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 30DCEEFE1E86B68A00442696 /* _____Tests.m */; };
30DCEF0A1E86B68A00442696 /* _____UITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 30DCEF091E86B68A00442696 /* _____UITests.m */; };
30DCEF1C1E86CA5700442696 /* TTSpeechController.m in Sources */ = {isa = PBXBuildFile; fileRef = 30DCEF1B1E86CA5700442696 /* TTSpeechController.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -51,6 +52,8 @@
30DCEF051E86B68A00442696 /* 文字转语音UITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "文字转语音UITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
30DCEF091E86B68A00442696 /* _____UITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "_____UITests.m"; sourceTree = "<group>"; };
30DCEF0B1E86B68A00442696 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
30DCEF1A1E86CA5700442696 /* TTSpeechController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TTSpeechController.h; sourceTree = "<group>"; };
30DCEF1B1E86CA5700442696 /* TTSpeechController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TTSpeechController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -105,6 +108,8 @@
30DCEEE81E86B68A00442696 /* AppDelegate.m */,
30DCEEEA1E86B68A00442696 /* ViewController.h */,
30DCEEEB1E86B68A00442696 /* ViewController.m */,
30DCEF1A1E86CA5700442696 /* TTSpeechController.h */,
30DCEF1B1E86CA5700442696 /* TTSpeechController.m */,
30DCEEED1E86B68A00442696 /* Main.storyboard */,
30DCEEF01E86B68A00442696 /* Assets.xcassets */,
30DCEEF21E86B68A00442696 /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -276,6 +281,7 @@
30DCEEEC1E86B68A00442696 /* ViewController.m in Sources */,
30DCEEE91E86B68A00442696 /* AppDelegate.m in Sources */,
30DCEEE61E86B68A00442696 /* main.m in Sources */,
30DCEF1C1E86CA5700442696 /* TTSpeechController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -499,6 +505,7 @@
30DCEF101E86B68A00442696 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
30DCEF111E86B68A00442696 /* Build configuration list for PBXNativeTarget "文字转语音Tests" */ = {
isa = XCConfigurationList;
Expand All @@ -507,6 +514,7 @@
30DCEF131E86B68A00442696 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
30DCEF141E86B68A00442696 /* Build configuration list for PBXNativeTarget "文字转语音UITests" */ = {
isa = XCConfigurationList;
Expand All @@ -515,6 +523,7 @@
30DCEF161E86B68A00442696 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Binary file not shown.
19 changes: 19 additions & 0 deletions 文字转语音/TTSpeechController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// TTSpeechController.h
// 文字转语音
//
// Created by 王琪 on 17/3/25.
// Copyright © 2017年 王琪. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface TTSpeechController : NSObject
// 创建一个制度的语音合成器
@property (nonatomic,strong,readonly) AVSpeechSynthesizer *synthesizer;

// 实例化语音合成控制器
+ (instancetype)shareSpeechController;
//
- (void)beginConversation;
@end
61 changes: 61 additions & 0 deletions 文字转语音/TTSpeechController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// TTSpeechController.m
// 文字转语音
//
// Created by 王琪 on 17/3/25.
// Copyright © 2017年 王琪. All rights reserved.
//

#import "TTSpeechController.h"

@interface TTSpeechController ()
//语音合成对象
@property (nonatomic,strong) AVSpeechSynthesizer *synthesizer;
//语音设置器
@property (nonatomic,strong) NSArray *voice;
//语音转化的文本
@property (nonatomic,strong) NSArray *speechString;

@end

@implementation TTSpeechController

+ (instancetype)shareSpeechController {
return [[self alloc]init];
}
- (instancetype)init {
self = [super init];
if (self) {
// 初始化语音合成器
_synthesizer = [[AVSpeechSynthesizer alloc]init];
// 设置需要发出的声音
_voice = @[[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"],[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]];
// 文本
_speechString = [self buildString];
}
return self;
}
// 语音转文本核心方法
- (void)beginConversation {
for (int i = 0; i < self.speechString.count; i++) {
NSString *speechStr = self.speechString[i];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:speechStr];
utterance.volume = 1;
utterance.pitchMultiplier = 0.8;//音调
utterance.voice = self.voice[i%2];//语言
utterance.rate = 0.5;//说话速率
utterance.postUtteranceDelay = 0.1;//延迟播放声音

[self.synthesizer speakUtterance:utterance];
}
}

- (NSArray *)buildString {
return @[@"hello",
@"你好",
@"miki",
@"王胖胖同学",
@"miki",
@"王胖胖同学"];
}
@end

0 comments on commit cf66558

Please sign in to comment.