【问题标题】:Decode Audio using MediaStreamSource使用 MediaStreamSource 解码音频
【发布时间】:2016-03-18 12:18:11
【问题描述】:

我有一个 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;
        }
    }

我没有使用事件系统,而是创建了一个方法,当我的 AudioFrameInputNode 需要一个新的 AudioFrame 时触发该方法。

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

MediaStreamSample.CreateFromStreamAsync 是否真的将音频文件解码为浮点字节数组?还是在播放样本时在 MediaElement 中完成?

如果是这样,我如何解码音频文件,以便将生成的 AudioBuffer 提供回我的 FrameInputNode?​​p>

【问题讨论】:

    标签: audio win-universal-app mediastreamsource


    【解决方案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();  
                });
            }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2013-04-25
      相关资源
      最近更新 更多