ISimpleAudioPlayer Issues on iOS and with SpeechRecognizer

Matt Drouillard 186 Reputation points
2021-03-18T14:19:32.953+00:00

I am using a simple audio player implementation to play audio, but having issues only on a physical iOS device:

1) In silent mode, the audio does not play
2) I always want the audio to play through the devices speakers, but after using SpeechRecognizer - the audio starts playing through the phone earpiece.

How can I make sure the audio always plays through the standard loud speakers, and also make sure that when I am using SpeechRecognizer the mode doesn't switch to playing sounds through the earpiece. No issues at all with Android.

Sample Code I am using for simple audio playback.

        public ISimpleAudioPlayer AudioPlayer => _audioPlayer;

        private void StopAudio()
        {
            if(_audioPlayer.IsPlaying)
                _audioPlayer.Stop();
        }

        private void PlayAudio(string audioFile)
        {
            if(_audioPlayer.IsPlaying)
                _audioPlayer.Stop();

            _audioPlayer = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();

            _audioPlayer.Load(audio);
            _audioPlayer.Play();
        }

Speech Recognizer Usage (Azure Speech to Text)

_speechRecognizer = new SpeechRecognizer(config);
await _speechRecognizer.StartContinuousRecognitionAsync();

...
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,408 questions
{count} votes

Accepted answer
  1. Matt Drouillard 186 Reputation points
    2021-03-25T13:51:55.28+00:00

    I had to resolve this issue by writing a custom AudioService for both iOS and Android:

    I followed these articles:

    //Implementing Audio service using depdency injection for both Android/IOS
    https://visualstudiomagazine.com/articles/2018/03/29/xamarin-forms-audio.aspx

    //Details on the sound play output mode in iOS
    https://alexdunn.org/2017/07/27/xamarin-tip-playing-audio-through-the-earpiece-in-ios/

    Essentially I had to allow the audio to be set to a specific mode in iOS to work, in addition to this; prior to each listening session I had to re-instantiate t he SpeechRecognizer object:

    //Before each time you want to begin speech to text
    var config = SpeechConfig.FromSubscription(...,...);
    _speechRecognizer = new SpeechRecognizer(config);

    0 comments No comments

0 additional answers

Sort by: Most helpful