我有MediaStreamSource实现,可以打开具有多个音频流的文件。在OpenMediaAsync方法中,我为所有视频和音频流传递MediaStreamDescription,但MediaElement仅检测到1个音频流。我也测试了下一个有效的逻辑:

  • 检测到2个流
  • 仅向ReportOpenMediaCompleted
  • 报告音频流的第一或第二MediaStream描述

    但是,当然我想向ReportOpenMediaCompleted报告第一和第二音频流,结果是MediaElement具有2音频流。我也发现了MediaStreamSource类中的StreamId字段,但是它没有设置访问器,并且当使用ReportOpenMediaCompleted报告流时,所有MediaStreamDescription的StreamId == 0。

    OpenMediaAsync代码:
    protected override void OpenMediaAsync()
    {
        this.streamDesc = new Dictionary<int, MediaStreamDescription>();
        List<MediaStreamDescription> availableStreams = new List<MediaStreamDescription>();
    
        for (int i = 0; i < this.parser.StreamCount; i++)
        {
            Dictionary<MediaStreamAttributeKeys, string> streamAttributes = new Dictionary<MediaStreamAttributeKeys, string>();
            MediaStreamDescription msd = null;
    
            var type = this.parser.GetStreamType(i);
            streamAttributes[MediaStreamAttributeKeys.CodecPrivateData] = this.parser.GetCodecPrivateData(i);
    
            if (type == ParserComponent.StreamType.Video)
            {
                streamAttributes[MediaStreamAttributeKeys.VideoFourCC] = this.parser.GetCodecID(i);
                streamAttributes[MediaStreamAttributeKeys.Width] = this.parser.GetWidth(i).ToString();
                streamAttributes[MediaStreamAttributeKeys.Height] = this.parser.GetHeight(i).ToString();
    
                msd = new MediaStreamDescription(MediaStreamType.Video, streamAttributes);
            }
            else if (type == ParserComponent.StreamType.Audio)
            {
                msd = new MediaStreamDescription(MediaStreamType.Audio, streamAttributes);
            }
    
            if (msd != null)
            {
                if (i == this.parser.CurrentAudioStreamIndex || i == this.parser.CurrentVideoStreamIndex)
                {
                    this.parser.SetStreamActive(i, true);
    
                    // quick fix for multilanguage videos to submit only 1 audio stream
                    // availableStreams.Add(msd);
                }
    
                this.streamDesc.Add(i, msd);
                availableStreams.Add(msd);
                }
            }
    
            Dictionary<MediaSourceAttributesKeys, string> sourceAttributes = new Dictionary<MediaSourceAttributesKeys, string>();
    
            sourceAttributes[MediaSourceAttributesKeys.CanSeek] = this.parser.Seekable.ToString();
            sourceAttributes[MediaSourceAttributesKeys.Duration] = this.parser.Duration.Ticks.ToString();
    
            ReportOpenMediaCompleted(sourceAttributes, availableStreams);
    }
    

    最佳答案

    使用Player framework可能是一个好主意。它支持多种音频流。

    09-26 19:27