-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// PCSpeechSynthesizer.h | ||
// Pica | ||
// | ||
// Created by Fancy on 2022/4/13. | ||
// Copyright © 2022 fancy. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface PCSpeechSynthesizer : NSObject | ||
|
||
+ (instancetype)sharedInstance; | ||
|
||
- (void)speakText:(NSString *)string; | ||
- (void)stopSpeak; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// PCSpeechSynthesizer.m | ||
// Pica | ||
// | ||
// Created by Fancy on 2022/4/13. | ||
// Copyright © 2022 fancy. All rights reserved. | ||
// | ||
|
||
#import "PCSpeechSynthesizer.h" | ||
#import <AVFoundation/AVFoundation.h> | ||
|
||
@interface PCSpeechSynthesizer () <AVSpeechSynthesizerDelegate> | ||
|
||
@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer; | ||
|
||
@end | ||
|
||
@implementation PCSpeechSynthesizer | ||
|
||
+ (instancetype)sharedInstance { | ||
static dispatch_once_t onceToken; | ||
static PCSpeechSynthesizer *instance = nil; | ||
dispatch_once(&onceToken, ^{ | ||
instance = [[super allocWithZone:NULL] init]; | ||
}); | ||
return instance; | ||
} | ||
|
||
+ (id)allocWithZone:(struct _NSZone *)zone { | ||
return [self sharedInstance]; | ||
} | ||
|
||
- (void)speakText:(NSString *)string { | ||
// AVSpeechUtteranceMinimumSpeechRate 0 | ||
// AVSpeechUtteranceMaximumSpeechRate 1 | ||
// AVSpeechUtteranceDefaultSpeechRate 0.5 | ||
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:string]; | ||
utterance.rate = 0.6; | ||
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]; | ||
[self.synthesizer speakUtterance:utterance]; | ||
} | ||
|
||
- (void)stopSpeak { | ||
[self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; | ||
} | ||
|
||
#pragma mark - AVSpeechSynthesizerDelegate | ||
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;{ | ||
|
||
} | ||
|
||
#pragma mark - Get | ||
- (AVSpeechSynthesizer *)synthesizer{ | ||
if (!_synthesizer) { | ||
_synthesizer = [[AVSpeechSynthesizer alloc] init]; | ||
_synthesizer.delegate = self; | ||
} | ||
return _synthesizer; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters