音声読み上げ関数。
[self speech:string]
#include <AVFoundation/AVFoundation.h>
..
@implementation ViewController{
AVSpeechSynthesizer *speechSynth;
}
- (IBAction)pressButton:(id)sender {
[self speech:self.myText.text];
}
- (void) speech : (id) str{
speechSynth = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utter = [AVSpeechUtterance speechUtteranceWithString:str];
utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"ja-JP"];
[speechSynth speakUtterance:utter];
}
音声ファイルを再生する方法。
[self playSound:filename]
sounds の配列に複数のファイル名を入れてランダムにどちらかの音を鳴らす。
#include <AVFoundation/AVFoundation.h>
...
@interface ViewController (){
@public AVAudioPlayer *audio;
}
...
- (IBAction)HogePressed:(id)sender {
NSArray *sounds = @[@"soundFile1",@"soundFile2"];
int i = arc4random() % sounds.count;
[self playSound:sounds[i]];
}
- (void) playSound : (id)file{
NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
audio.volume = 0.1;
[audio play];
}