我尝试使用以下代码使用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”?我在这一行上有编译器错误。
答案 0 :(得分:0)
AudioTrack没有WriteAsync。对于dot42,请使用以下行:
await Task.Factory.StartNew( () => audioTrack.Write(buffer, 0, buffer.Length) );
我认为这也适用于Xamarin。
答案 1 :(得分: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) { } }