NAudio 和 Xamarin Android

NAudio and Xamarin Android

我想使用 Naudio 库来录制音频,添加一些效果并使用 android 播放音频。我有一个 xamarin android 应用程序,我想添加此功能。我看到 WaveOut(和 WaveIn)仅适用于 Windows,对吗?那么,我是否需要使用 android 音频播放器实现 IWavePlayer?这是将 naudio 与 xamarin android 一起使用的另一种方式吗? 谢谢。

NAudio 的很大一部分是 Windows 音频 API 的包装器。有些东西可以跨平台使用,但您需要创建 IWavePlayer 的本机实现,并且只能使用完全托管的编解码器。

可以录制回放:

https://github.com/NateRickard/Plugin.AudioRecorder

页数:

<StackLayout Orientation="Horizontal">
    <Switch x:Name="TimeoutSwitch" IsToggled="True" />
    <Label Text="Timeout after silence?" />
</StackLayout>
<Button x:Name="RecordButton"
        Clicked="Record_Clicked" FontSize="24" HorizontalOptions="FillAndExpand"
        Text="Record" />
<Button x:Name="PlayButton"
        Clicked="Play_Clicked" FontSize="24" HorizontalOptions="FillAndExpand"
        IsEnabled="False" Text="Play" />

CS:

AudioRecorderService recorder;
AudioPlayer player;

public AudioPlayPage()
{
    InitializeComponent();

    recorder = new AudioRecorderService
    {
        StopRecordingAfterTimeout = true,
        TotalAudioTimeout = TimeSpan.FromSeconds(15),
        AudioSilenceTimeout = TimeSpan.FromSeconds(2)
    };

    player = new AudioPlayer();
    player.FinishedPlaying += Player_FinishedPlaying;
}

private void Player_FinishedPlaying(object sender, EventArgs e)
{
    PlayButton.IsEnabled = true;
    RecordButton.IsEnabled = true;
}

private async void Record_Clicked(object sender, EventArgs e)
{
    try
    {
        if (!recorder.IsRecording)
        {
            recorder.StopRecordingOnSilence = TimeoutSwitch.IsToggled;

            RecordButton.IsEnabled = false;
            PlayButton.IsEnabled = false;

            var audioRecordTask = await recorder.StartRecording();

            RecordButton.Text = "Stop Recording";
            RecordButton.IsEnabled = true;

            await audioRecordTask;

            RecordButton.Text = "Record";
            PlayButton.IsEnabled = true;
        }
        else 
        {
            RecordButton.IsEnabled = false;
            await recorder.StopRecording();
            RecordButton.IsEnabled = true;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void Play_Clicked(object sender, EventArgs e)
{
    try
    {
        var filePath = recorder.GetAudioFilePath();

        if (filePath != null)
        {
            PlayButton.IsEnabled = false;
            RecordButton.IsEnabled = false;

            player.Play(filePath);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}