0

我尝试使用以下代码使用 Xamarin 音频记录/播放示例: Example_WorkingWithAudio

在以下Task我有async错误:

protected async Task PlayAudioTrackAsync ()
{
    audioTrack = new AudioTrack (
        // Stream type
        Android.Media.Stream.Music,
        // Frequency
        11025,
        // Mono or stereo
        ChannelConfiguration.Mono,
        // Audio encoding
        Android.Media.Encoding.Pcm16bit,
        // Length of the audio clip.
        buffer.Length,
        // Mode. Stream or static.
        AudioTrackMode.Stream);

    audioTrack.Play ();

    await audioTrack.WriteAsync (buffer, 0, buffer.Length);
}

更改为 dot42:

protected async Task PlayAudioTrackAsync()
{
    audioTrack = new AudioTrack (AudioManager.STREAM_MUSIC,11025,
                                 AudioFormat.CHANNEL_OUT_MONO,
                                 AudioFormat.ENCODING_PCM_16BIT,
                                 buffer.Length,
                                 AudioTrack.MODE_STREAM
                                 );
    audioTrack.Play ();
    await audioTrack.WriteAsync(buffer, 0, buffer.Length);
}

如何更改“audioTrack.WriteAsync”?我在这一行有编译器错误。

4

2 回答 2

0

AudioTrack 没有 WriteAsync。对于 dot42,使用这一行:

await Task.Factory.StartNew( () => audioTrack.Write(buffer, 0, buffer.Length) );

我认为这也适用于 Xamarin。

于 2013-11-11T11:52:28.857 回答
0

除了那个答案,我还使用 audioTrack.WriteAsync() 方法来播放音频异步。我认为不需要创建异步方法。这是异步播放音频的方法。

public void audioTrackPlay(byte[] audioBuffer){try {

     audioTrack.Play();
   //  audioTrack.Write(audioBuffer, 0, audioBuffer.Length);
    audioTrack.WriteAsync(audioBuffer, 0, audioBuffer.Length);

     Toast.MakeText(this,"Server Started",ToastLength.Short).Show();
   } catch (Exception e) { }   }
于 2016-01-29T09:50:15.370 回答