locked
MediaElement_MediaEnded不触发,求救 RRS feed

  • 问题

  • 我报告了音视频结束流,之后GetSampleAsync也不会再取了,但是我的播放器有些视频结束后可以触发MediaElement_MediaEnded,有些视频不触发,怎么回事呀?弄了一周,没弄出来,请哪位指点

    private void PositionTimerTick(object sender, EventArgs e)
            {
                TimeSpan span = this.mediaElement.Position;
                long totalSeconds = (long)span.TotalSeconds;
                this.progressBar.Value = span.TotalSeconds;

           
            }
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                IDictionary<string, string> queryString = this.NavigationContext.QueryString;

                if (queryString.ContainsKey("SourceURI"))
                {
                    mSourceURI = new Uri(queryString["SourceURI"], UriKind.RelativeOrAbsolute);
                }
                else
                {
                    System.Diagnostics.Debug.Assert(false, "FullScreenPlayer.OnNavigatedTo: SourceURI is not set.");
                    NavigationService.GoBack();
                }
                String url = "http的url链接";
              
               mDataBuffered = new DataBuffered(url);
                mMediaStream = new ReadPacket_Demux();
              
                try
                {
                   mediaElement.SetSource(mMediaStream);
                
                }
                catch (System.Exception ex)
                {
                    Debug.WriteLine("{0} exception caught here.", ex.GetType().ToString());
                    Debug.WriteLine(ex.Message);
               
                }
                mPositionTimer = new DispatcherTimer();

                mPositionTimer.Interval = TimeSpan.FromSeconds(1);
                mPositionTimer.Tick += new EventHandler(this.PositionTimerTick);
                mPositionTimer.Start();
                mediaElement.Play();
                //mediaElement.MediaEnded += new RoutedEventHandler(MediaElement_MediaEnded);
                base.OnNavigatedTo(e);
            }

            private void button_pause_Click(object sender, RoutedEventArgs e)
            {
                //读包线程挂起
                ReadPacket_Demux.mPlayer.pause();
                //silverlight挂起
                mediaElement.Pause();
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                ReadPacket_Demux.mPlayer.start();
                mediaElement.Play();
            }
            //当媒体流已被验证和打开且已读取文件头时发生,获取媒体时长
            private void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
            {
                TimeSpan span = this.mediaElement.NaturalDuration.TimeSpan;
               
                 progressBar.Maximum = span.TotalSeconds;
                 SampleBuffer.mDuration = (int)span.TotalSeconds;
            }
            //当mediaElement播放完毕后触发
            private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
            {
                 mediaElement.Stop();
                          
                //释放buffer
                mMediaStream.DoCloseMedie();
                //释放api
                ReadPacket_Demux.mPlayer.release();
                ReadPacket_Demux.mPlayer = null;
                 
            }

    这是界面的简单代码,下面几行代码是最后一帧报告后,报告结束流

    //最后一帧,报告流结束信息
                    if (DataBuffered.mIsEndOfStream)
                    {                   
                        if (mSamples.Count == 0 && !mIsSeeking.isSeeking())                 
                        {
                            mEmptyAttributes[MediaSampleAttributeKeys.KeyFrameFlag] = Boolean.FalseString;

                            if (sampleAttributes == null)
                                sampleAttributes = mEmptyAttributes;
                            Debug.WriteLine("{0}silverlight read over", mMsd.Type.ToString());
                            mIsReportEnd = true;
                            return new MediaStreamSample(mMsd, null, 0, 0,0, sampleAttributes);
                        }
                    }

    2013年9月13日 7:13

答案

全部回复

  • 流读完后,触发了MediaElement_MediaEnded事件,但是CloseMedia()始终不被调用,请问CloseMedia函数在什么情况下调用
    2013年9月16日 5:58
  • tingter 您好,

    个人认为那些不触发MediaEnded事件的视频应该相比触发的视频有一些编码上的错误或不一致。

    同时该问题也在英文论坛上多次被提及,如下:

    http://playerframework.codeplex.com/discussions/409014

    http://social.msdn.microsoft.com/Forums/windowsapps/en-US/4af53ba0-906d-4f99-9d70-a91ed7a27c0b/mediaelementmediaended-does-not-fire-when-audiocategory-audiocategorybackgroundcapablemedia

    a solution might be to set a MediaMarker at the MediaOpened event (can't set one before that event) and use the MediaElement.NaturalDuration there to specify that the marker is at the end. When you get the MarkerReached event for that specific marker you can stop the mediaelement by code

    目前的解决方法是通过在MediaOpen时间中设置MediaMarker,然后使用MediaElement.NaturalDuration来辨别是否marker视频结束。我推荐你尝试下这种方法。

    谢谢!


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月18日 3:02