AVSpeechSynthesizer works on simulator but not on device

江枫思渺然 提交于 2019-12-01 18:00:54

问题


I've been trying to learn AVSpeechSynthesis. The code works fine in the iOS Simulator (both on iPad and iOS) but the text to speech feature doesn't work at all on my iPad. Here's my code.

    - (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.



//Load Model
    self.string = @"Hello World";
    self.utterance = [[AVSpeechUtterance alloc] initWithString:self.string];
    self.utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
    self.utterance.rate = (float) 0.25;

    //Load UI
    self.textLabel.text = self.string;


}

- (IBAction)startSpeaking:(id)sender {

    self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init];

    if ([self.speechSynthesizer isSpeaking]) {

        [self.speechSynthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryWord];


    } else {

        [self.speechSynthesizer speakUtterance:self.utterance];

    }    
}

UPDATE: AVSpeechSyntheSizer is working but no sound So I have a UILabel with the same text as the AVSpeechUtterance on the UI. I implemented AVSpeechSynthesizerDelegate so that when I hit startSpeaking: the text label turns the current word red. So I know that AVSpeechSynthesizer must be working because it's making delegate calls but I can't figure out why I can't hear it. The volume is up and no other apps are playing audio.


回答1:


Do you have the phone in silent mode? That silences the synthesizer as well.




回答2:


It's not working because of the Mute mode of your phone. To turn 'Mute On' in iOS 7, you have to follow the following steps:

  • Go to your Setting option
  • Go to Accessibility
  • Go to AssistiveTouch
  • Turn on AssistiveTouch option from the switch button
  • Then a Moving Assistive icon, will appear on you device screen.
  • Touch on it to expand with more options
  • From there select Device and Touch Un mute option.

Now run the project and hope it will work.

P.S: (In iOS 8.0 this code wouldn't work, because for an unexpected bug, from iOS8.1 they remove it and it is now working perfectly) Hope it works. :)



来源:https://stackoverflow.com/questions/24093434/avspeechsynthesizer-works-on-simulator-but-not-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!