使用MediaStreamSource解码音频

WJM*_*WJM 5 audio mediastreamsource win-universal-app

我有一个UWP项目,我想使用Windows.Media.Audio API来播放文件.我想要流式传输文件,而不是使用FileInputNode,因此我可以精确地确定各种时序属性.

我找到了MediaStreamSource API并制作了以下代码,试图解码16位PCM 2通道.wav文件

 public async Task<Windows.Storage.Streams.Buffer> GetBuffer()
    {
        // check if the sample requested byte offset is within the file size 

        if (byteOffset + BufferSize <= mssStream.Size)
        {
            inputStream = mssStream.GetInputStreamAt(byteOffset);

            // create the MediaStreamSample and assign to the request object.  
            // You could also create the MediaStreamSample using createFromBuffer(...) 

            MediaStreamSample sample = await MediaStreamSample.CreateFromStreamAsync(inputStream, BufferSize, timeOffset);
            sample.Duration = sampleDuration;
            sample.KeyFrame = true;
            // increment the time and byte offset 

            byteOffset += BufferSize;
            timeOffset = timeOffset.Add(sampleDuration);


            return sample.Buffer;
        }
        else
        {

            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我没有使用Event系统,只要我的AudioFrameInputNode需要一个新的AudioFrame,我就会触发一个方法.

现在看来,MediaStreamSample中生成的字节数组与我使用DataReader读取StorageFile时完全相同.

MediaStreamSample.CreateFromStreamAsync实际上是否将audiofile解码为浮点字节数组?或者在播放样本时是否在MediaElement中完成?

如果是这样,我怎么能解码一个audiofile所以我可以将生成的AudioBuffer重新提供给我的FrameInputNode?

The*_*ord 1

完成后MediaStreamSample.CreateFromStreamAsync,它将新文件作为 MediaStreamSample 返回。

如果我明白你想要什么,这里就是你可以做些什么来实现它的例子。

function sampleRequestedHandler(e) {
    var request = e.request;
    if (!MyCustomFormatParser.IsAtEndOfStream(randomAccessStream)) { 
        var deferral = request.getDeferral();
        var inputStream = MyCustomFormatParser.getInputStream(randomAccessStream); 
        MediaStreamSample.createFromStreamAsync(inputStream, sampleSize, timeOffset).
            then(function(sample) {
                sample.duration = sampleDuration;  
                sample.keyFrame = true;  
                request.sample = sample;           
                deferral.complete();  
            });
        }
}
Run Code Online (Sandbox Code Playgroud)

当请求样本时,您的自定义解析器从 RandomAccessStream 中提取音频数据并返回仅包含音频样本的 InputStream