예제 #1
0
        public void Speak(string text, string voiceTypeIdentifier, FinishEvent audioFinishHandler = null)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException("text");
            }

            if (IsPlaying)
            {
                return;
            }

            this.audioFinishHandler = audioFinishHandler;

            IsPlaying = true;

            AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.PlayAndRecord,
                                                        AVAudioSessionCategoryOptions.AllowBluetooth | AVAudioSessionCategoryOptions.AllowBluetoothA2DP);

            AVAudioSession.SharedInstance().OverrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, out NSError error);

            AVSpeechUtterance speechUtterance = new AVSpeechUtterance(text);

            if (!string.IsNullOrEmpty(voiceTypeIdentifier))
            {
                speechUtterance.Voice = AVSpeechSynthesisVoice.FromIdentifier(voiceTypeIdentifier);
            }

            speechSynthesizer.SpeakUtterance(speechUtterance);
        }
예제 #2
0
        public void PlayAudio(string text)
        {
            if (_speechSynthesizer == null)
            {
                _speechSynthesizer = new AVSpeechSynthesizer();
                _voice             = AVSpeechSynthesisVoice.FromIdentifier("com.apple.ttsbundle.siri_female_de-DE_compact");
                if (_voice == null)
                {
                    _voice = AVSpeechSynthesisVoice.FromLanguage("de-DE");
                }
            }
            var speechUtterance = new AVSpeechUtterance(text)
            {
                Rate            = AVSpeechUtterance.DefaultSpeechRate,
                Voice           = _voice,
                Volume          = 1.0f,
                PitchMultiplier = 1.0f
            };

            _speechSynthesizer.SpeakUtterance(speechUtterance);
        }