Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 异步播放音频曲目_Android_Dot42 - Fatal编程技术网

Android 异步播放音频曲目

Android 异步播放音频曲目,android,dot42,Android,Dot42,我尝试使用Xamarin的音频录制/播放示例,代码如下: 在以下任务中,我出现异步错误: protected async Task PlayAudioTrackAsync () { audioTrack = new AudioTrack ( // Stream type Android.Media.Stream.Music, // Frequency 11025, // Mono or stereo

我尝试使用Xamarin的音频录制/播放示例,代码如下:

在以下
任务
中,我出现
异步
错误:

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”?我发现这行有编译器错误。

AudioTrack没有WriteAsync。对于dot42,请使用此行:

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

我想这也适用于Xamarin。

除了这个答案,我还使用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) { }   }