Leider erfordert das Einrichten ein paar Aufgaben, also hier sind sie.
Erstellen Sie eine .Net Maui-Anwendung, ich habe meine „AudioManagerTest“ genannt.
Zuerst musste ich eine IoS-Bundle-Signierung einrichten.
Zweitens musste ich die info.plist anpassen
Code: Select all
NSMicrophoneUsageDescription
This app needs microphone access to allow for voice recognition and AI request processing.
LSRequiresIPhoneOS
UIDeviceFamily
1
2
UIRequiredDeviceCapabilities
arm64
UISupportedInterfaceOrientations
UIInterfaceOrientationPortrait
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
UISupportedInterfaceOrientations~ipad
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
XSAppIconAssets
Assets.xcassets/appicon.appiconset
Code: Select all
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
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;
}
}
Code: Select all
using Microsoft.Maui.Controls;
using Plugin.Maui.Audio;
namespace AudioManagerTest
{
public partial class MainPage : ContentPage
{
int count = 0;
private IAudioManager _audioManager;
private IAudioStreamer? _audioStreamer;
private IAudioPlayer playerIsSleeping;
private List _audioBuffer;
public MainPage()
{
InitializeComponent();
}
public async Task StartIt()
{
try
{
_audioManager = new AudioManager();
_audioBuffer = new List();
_audioStreamer = _audioManager.CreateStreamer();
}
catch (Exception ex)
{
await SayIt("Error initializing audio manager: " + ex.Message);
return;
}
try
{
// Configure streaming options for optimal PCM16 format
_audioStreamer.Options.SampleRate = 16000;// 44100; // Standard sample rate
_audioStreamer.Options.Channels = ChannelType.Mono; // Mono for efficiency
_audioStreamer.Options.BitDepth = BitDepth.Pcm16bit; // PCM16 as requested
//await _audioStreamer.StartAsync();
}
catch (Exception ex)
{
await SayIt("Error starting audio streamer: " + ex.Message);
return;
}
playerIsSleeping = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("isAsleep.wav"));
playerIsSleeping.Play();
await SayIt("I am ready.");
}
private async Task SayIt(string text)
{
CancellationToken token = new CancellationToken();
//onTextOut(text);
await TextToSpeech.Default.SpeakAsync(text, cancelToken: token).ConfigureAwait(false);
}
private async void OnCounterClicked(object? sender, EventArgs e)
{
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
//SemanticScreenReader.Announce(CounterBtn.Text);
await StartIt();
}
}
}
Wenn dies bereitgestellt und zum ersten Mal ausgeführt wird, werden die Sounds platziert, nachdem die Schaltfläche berührt wird. Ich glaube, es hat mit einer Verzögerung zu tun, da Sie als Benutzer die Schaltfläche berühren müssen.
Wenn Sie die Zeile „await _audioStreamer.StartAsync();“ auskommentieren, werden die Sounds abgespielt.
Auch dies funktioniert auf Android. Und wenn ich die Ausgabe unterbreche und neu starte, während die obige Zeile nicht auskommentiert ist, kann ich es gelegentlich zum Laufen bringen.
Ich habe damit herumexperimentiert und kann nicht herausfinden, warum IoS die Wiedergabe nicht zulässt.
Hat jemand einen Einblick, was los ist?
Mobile version