NAudio playback stream, byte array (not wav, mp3, aiff format)

NAudio playback stream, byte array (not wav, mp3, aiff format)

1. Questions

The audio data stream returned by http or websocket communication is exported to an audio file, and then AudioFileReaderthe file stream can be read to play more audio formats (for example: webm). But how can I play the stream directly without creating a temporary fileMemoryStream ?

2. Examples

  • 1. When the returned audio stream is webm, the temporary file method can be played normally. It is guessed that the method of reading the file stream can be analyzed to cancel the redundant steps;
var filename = "./tmp.webm";
using (var audioFile = new AudioFileReader(filename))
	using (var waveOut = new WaveOutEvent())
	{
    
    
	    waveOut.Init(audioFile);
	    waveOut.Play();
	    while (waveOut.PlaybackState == PlaybackState.Playing)
	        Thread.Sleep(100);
	}
  • 2. Check AudioFileReaderthe constructor, the CreateReaderStreammethod is the key to successfully play the three types of audio: wav, mp3, and aiff, so you need to find the associated MediaFoundationReaderclasses;
// 摘要:
//     Creates the reader stream, supporting all filetypes in the core NAudio library,
//     and ensuring we are in PCM format
//
// 参数:
//   fileName:
//     File Name
private void CreateReaderStream(string fileName)
{
    
    
    if (fileName.EndsWith(".wav", StringComparison.OrdinalIgnoreCase))
    {
    
    
        readerStream = new WaveFileReader(fileName);
        if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm && readerStream.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat)
        {
    
    
            readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
            readerStream = new BlockAlignReductionStream(readerStream);
        }
    }
    else if (fileName.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase))
    {
    
    
        if (Environment.OSVersion.Version.Major < 6)
        {
    
    
            readerStream = new Mp3FileReader(fileName);
        }
        else
        {
    
    
            readerStream = new MediaFoundationReader(fileName);
        }
    }
    else if (fileName.EndsWith(".aiff", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith(".aif", StringComparison.OrdinalIgnoreCase))
    {
    
    
        readerStream = new AiffFileReader(fileName);
    }
    else
    {
    
    
    	// 一切其他格式音频IO流读取方式
        readerStream = new MediaFoundationReader(fileName);
    }
}
  • 3. With the help of vs code intelligent prompts, find MediaFoundationReaderthe subclass ofStreamMediaFoundationReader
public class StreamMediaFoundationReader : MediaFoundationReader
{
    
    
    private readonly Stream stream;
    public StreamMediaFoundationReader(Stream stream, MediaFoundationReaderSettings settings = null)
    {
    
    
        this.stream = stream;
        Init(settings);
    }
    // 省略
}

3. Code modification

byte[] buffer;
using (var ms = new MemoryStream(buffer))
	using (var media = new StreamMediaFoundationReader(ms))
	    using (var waveOut = new WaveOutEvent())
	    {
    
    
	        waveOut.Init(media);
	        waveOut.Play();
	        while (waveOut.PlaybackState == PlaybackState.Playing)
	            Thread.Sleep(100);
	    }

Guess you like

Origin blog.csdn.net/qq_41755979/article/details/125734860