Playing Audio through bluetooth to iPad - Updated

40 Views Asked by At

I asked this question yesterday on here, but since then I've gotten more information on exactly what is causing the issue.

I'm using communitytoolkit speechtotext.

The app I am writing interfaces with a bluetooth headset on the tablet. Currently, I'm working on an iPad 10 (current hardware).

I have an app delegate to enable the bluetooth:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{


    SetAudioSession();

    return base.FinishedLaunching(application, launchOptions);
}

public bool SetAudioSession()
{

    var audioSession = AVAudioSession.SharedInstance();
    var err = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord, AVAudioSessionCategoryOptions.AllowBluetooth |
                                        AVAudioSessionCategoryOptions.AllowAirPlay | AVAudioSessionCategoryOptions.DefaultToSpeaker);

    if (err != null)
        return false;

    err = audioSession.SetActive(true);

    if (err != null)
        return false;

    return true;


}

And this has been working.

But due to issues with the SpeechToText class I switched over from the interface method to the direct:

Interface:

public interface ISpeechToText
{
    Task<bool> RequestPermissions();

    Task<string> Listen(CultureInfo culture,
        IProgress<string> recognitionResult,
        CancellationToken cancellationToken);
}

public async Task GetPhraseAsync(CancellationToken cancellationToken)
{  
            var recognitionResult = await speechToText.Listen(CultureInfo.GetCultureInfo("en-us"),
            new Progress<string>(partialText =>
            {

                Debug.WriteLine("--- " + partialText + "---");
                if (previousText != partialText)
                {
                    RecognitionText = partialText + " ";
                    previousText = partialText;
                }

            }), cancellationToken);
...
}

The above code works fine with the AppDelegate which enabled the bluetooth headset, but the interface version of this method has issues of duplicating some text on occasion.

I then moved over to calling the class directly:

public async Task GetPhraseAsync(CancellationToken cancellationToken)
{
             var recognitionResult = await SpeechToText.Default.ListenAsync(CultureInfo.GetCultureInfo("en-us"),
            new Progress<string>(partialText =>
            {

                Debug.WriteLine("--- " + partialText + "---");
                if (previousText != partialText)
                {
                    RecognitionText = partialText + " ";
                    previousText = partialText;
                }

            }), cancellationToken);
...
}

This code is what caused the headphone icon on the iPad to disappear / headset go inactive. But the headset icon will re-appear on the ipad once the app is exited. If I go back to the interface version of the method, the bluetooth works great, but I've got the other issues that renders it useless for what I need it for.

If I comment out the instantiation of this class (disable SpeechToText), then the sounds and TextToSpeech (audio out) continues through the bluetooth headset as expected.

What am I running into here? Is this something in my control or is it in the library?

0

There are 0 best solutions below