将音频字节流式传输到MediaPlayer

时间:2018-04-01 17:52:26

标签: c# xamarin stream android-mediaplayer

有没有办法直接将字节流式传输到Android.Media.MediaPlayer?所以当我收到一堆字节时,我可以将它们扔到这个类中并播放它们并重复一次?我无法找到任何用字节喂MediaPlayer的例子,这似乎是最合理的方法。

目前我正在将每个数据包存储为临时文件,因此我可以播放一小部分音乐并立即处理它,但我还没有让它工作但感觉不错像一个可怕的方法。

这是我迄今为止所尝试过的。我收到了一小部分声音(bArray),我向其添加了.wav标题,以便我可以播放它。我对收到的每个数据包执行此操作。此标头与我收到的数据匹配(我使用NAudio库来录制声音):

public void PlayAudio(byte[] bArray)
{
    var player = new MediaPlayer();
    player.Prepared += (sender, args) =>
    {
        player.Start();
    };

    var header = new byte[]
    {
        0x52, 0x49, 0x46, 0x46, // b Chunk ID (RIFF)
        //0x24, 0xDC, 0x05, 0x00, // l Chunk size
        0x32, 0x4B, 0x00, 0x00,
        0x57, 0x41, 0x56, 0x45, // b Format WAVE
        0x66, 0x6d, 0x74, 0x20, // b Subchunk 1 id
        0x12, 0x00, 0x00, 0x00, // l Subchunk 1 size (size of the rest of the header) = 16
        0x03, 0x00,             // l Audio format, 1 = Linear Quantization, others = compression
        0x02, 0x00,             // l Number of channels, 1 = mono, 2 = stereo
        0x80, 0xBB, 0x00, 0x00, // l Sample rate
        0x00, 0xDC, 0x05, 0x00, // l Byte rate (SampleRate * NumChannels * BitsPerSample / 8)
        0x08, 0x00,             // l Block align (NumChannels * BitsPerSample / 8)
        0x20, 0x00,             // l Bits per sample 
        0x00, 0x00, 0x66, 0x61, // compression data
        0x63, 0x74, 0x04, 0x00, // compression data
        0x00, 0x00, 0x60, 0x09, // compression data
        0x00, 0x00,             // compression data
        0x64, 0x61, 0x74, 0x61, // b Subchunk 2 id (Contains the letters "data")
        0x00, 0x4B, 0x00, 0x00, // l Subchunk 2 Size
    };

        var file = File.CreateTempFile("example", ".wav");
        var fos = new FileOutputStream(file);
        fos.Write(header);
        fos.Write(bArray);
        fos.Close();

        player.Reset();

        var fis = new FileInputStream(file);
        player.SetDataSource(fis.FD);
        player.Prepare();
}

显然这段代码没有经过优化,但我甚至无法让它工作,而且我花了很多时间在上面,所以希望这个问题有不同的解决方案。

1 个答案:

答案 0 :(得分:1)

据我所知,MediaPlayer无法播放连续的流(不是为此而设计的)。但是,有更多的低级类AudioTrack,它能够做到这一点。

以下是我的一个项目的小样本:

private int _bufferSize;
private AudioTrack _output;

// in constructor
_bufferSize = AudioTrack.GetMinBufferSize(8000, ChannelOut.Mono, Encoding.Pcm16bit);

// when starting to play audio
_output = new AudioTrack(Stream.Music, 8000, ChannelOut.Mono, Encoding.Pcm16bit, _bufferSize, AudioTrackMode.Stream);
_output.Play();

// when data arrives via UDP socket
byte[] decoded = _codec.Decode(decrypted, 0, decrypted.Length);                
// just write to AudioTrack
_output.Write(decoded, 0, decoded.Length);

当然,您需要了解所有这些参数的含义(如Pcm16bit或采样率)才能正确实现。

相关问题