Skip to main content

Microsoft Silverlight

Answered Question weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers RSS Feed

(0)

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

 Hello,

 I want to add a collection of TimelineMarker objects to MediaElement.Markes collection on the MediaOpened event:

 

        void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Markers.Clear();
            foreach(TimelineMarker marker in this.markers)
            {
                this.mediaElement.Markers.Add(marker);      // generates exception
            }

          }

where markers is a TimelineMarkerCollection object which is created before the event is generated (in fact before setting MediaElement.Source).

This however results in an exception generated by Add method:

 System.ArgumentException: Value does not fall within the expected range.
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value)
   at System.Windows.Media.TimelineMarkerCollection.Add(TimelineMarker value)
   at TestProject.MediaPlayer.mediaElement_MediaOpened(Object sender, RoutedEventArgs e)

But if I create a new TimelineMarker object everything is fine:

        void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Markers.Clear();
            foreach(TimelineMarker marker in this.markers)
            {
                TimelineMarker tlm = new TimelineMarker();
                tlm.Text = marker.Text;
                tlm.Time = marker.Time;
                tlm.Type = marker.Type;

                this.mediaElement.Markers.Add(tlm);
            }

         }

I don't understand why a copy of the TimelineMarker created locally works but not an item from a TimelineMarkerCollection.

Please click on 'Mark as answer' near my comment if you feel I answered your question.

robhouweling
robhouwe...

Contributor

Contributor

3158 points

540 Posts

Silverlight MVP

Re: weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

Is the markers collection already added to a mediaelement?

An element can only have 1 parent, that's probably why the error occurs.

 

(If this has answered your question, please click on mark as answer on this post)

Cheers!
Rob Houweling



My blog

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

Re: Re: weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

Hi,

thanks for your comment. 

markers collection is a member type of my movie player class:

TimelineMarkerCollection markers = new TimelineMarkerCollection();

The clients of my class can add new markers and I thought to save them in a TimelineMarkerCollection. Then, I receive the event that movie is opened so I can add each TimelineMarker in this markers collection to the MediaElement.Markers

 

Please click on 'Mark as answer' near my comment if you feel I answered your question.

robhouweling
robhouwe...

Contributor

Contributor

3158 points

540 Posts

Silverlight MVP

Re: Re: weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

Can you post your project?

(If this has answered your question, please click on mark as answer on this post)

Cheers!
Rob Houweling



My blog

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

Re: Re: weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

 Here it is: https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=nitescua&ReleaseId=1545

I only have a MediaElement for which I add a marker in MediaOpened event.

As you can see it fails on adding a TimelineMarker object from a TimelineMarkerCollection.

Please click on 'Mark as answer' near my comment if you feel I answered your question.

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

Re: Re: Re: weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

 anybody knows why it crashes when using an object from TimlineMarkerCollection

Please click on 'Mark as answer' near my comment if you feel I answered your question.

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

Hello, the problem is: When you add the TimelineMarker to a TimelineMarkerCollection, the TimelineMarkerCollection will become the logical parent of the TimelineMarker. A FrameworkElement can only have a single parent, so later when you add the TimelineMarker to the MediaElement, an exception is thrown. You can use a List<TimelineMarker> instead of a TimelineMarkerCollection. A List<TimelineMarker> will not be considered in the logical tree, and thus won't have this problem.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

Re: Re: weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

Ok, thanks for explanation.

But it's a pitty that you need to add one by one a TimelineMarker to MediaElement.Markers instead of adding a collection

Please click on 'Mark as answer' near my comment if you feel I answered your question.

BigDubb
BigDubb

Member

Member

65 points

77 Posts

Re: Re: weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

 I too am getting an odd error.

This is the message information that is being thrown, with no inner exception at all.

System.ExecutionEngineException was unhandled
  Message="Exception of type 'System.ExecutionEngineException' was thrown."
  InnerException:

 

The code that generates the error.

 

public void AddMarker(TimeSpan Time)
        {
            //If the media already has a marker at that time, throw an error
            var _x = (from _m in _media.Markers where _m.Time == Time select _m);
            if(_x.Count() > 0)
                throw new MediaMarkerException("Marker already exists for this time");
            else
            {
                TimelineMarker _tl = new TimelineMarker();
                _tl.Time = Time;
                _media.Markers.Add(_tl);
            } 

I just want to allow for users to create a new marker item for a piece of media.

 

 

 

BigDubb
BigDubb

Member

Member

65 points

77 Posts

Re: Re: weird exception adding an item from a TimelineMarkerCollection to MediaElement.Markers

I found the problem, and thought I'd share.

When adding TimelineMarkers to a piece of media the media must be loaded, not just the source defined.  What I ended up doing was creating a keypair list on the control that housed the media item, and on the MediaLoaded event I go through the list and add the markers.  

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities