At the moment I have a map with annotations and when a user clicks on the annotation an audio plays. I wanted to add a Play/Pause button but it's not working and I'm unsure as to why.
The AVSpeechSynthesizer
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)anView
{
//Get a reference to the annotation this view is for...
id<MKAnnotation> annSelected = anView.annotation;
//Before casting, make sure this annotation is our custom type
//(and not some other type like MKUserLocation)...
if ([annSelected isKindOfClass:[MapViewAnnotation class]])
{
MapViewAnnotation *mva = (MapViewAnnotation *)annSelected;
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
AVSpeechUtterance *utterance =
[AVSpeechUtterance speechUtteranceWithString:mva.desc];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-gb"];
[utterance setRate:0.35];
[synthesizer speakUtterance:utterance];
}
The Button
- (IBAction)pauseButtonPressed:(UIButton *)sender
{
[_synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
Right now nothing happens when I click it.
I don't think you're initializing _synthesizer
. Try doing self.synthesizer = [[AVSpeechSynthesizer alloc] init];
instead of assigning the synth to a local variable.
I noticed that AVSpeechSynthesizer
had a hard time shutting up during the 7.0 beta, but I find it hard to believe that such an egregious bug would last this long.
NB: you should probably shouldn't recreate the AVSpeechSynthesizer
every time an annotation is tapped.
NB2: Once you've paused, I think you have to call continueSpeaking
to restart.