سؤال

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.

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

use this one for pause

- (IBAction)pausePlayButton:(id)sender
    {
        if([synthesize isSpeaking]) {
            [synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
            AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
            [synthesize speakUtterance:utterance];
            [synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        }

    }

For future help, when I clicked pause and resume the annotation worked however it wouldn't let me play another annotation as technically the first one is still running. I added a stop button with [_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; to sort it out.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top