MediaOpened event not fired when LoadBehavior is set to Manual

Hello,

I create a MediaElement as so to load a music file:

Code Snippet

MediaElement me =newMediaElement();

canvas.Children.Add(me);

me.LoadedBehavior =MediaState.Manual;

me.UnloadedBehavior =MediaState.Manual;

me.MediaFailed +=newEventHandler<ExceptionRoutedEventArgs>(me_MediaFailed);

me.MediaOpened +=newRoutedEventHandler(me_MediaOpened);

me.Source =newUri(location,UriKind.Relative);

When LoadedBehavior is set toMediaState.Play, the MediaOpened event is fired and the music plays, but when it is set toMediaState.Manual, the event is not fired. (the MediaFailed event is not fired either).

Also, when I run the XBAP outside of Visual Studio, the MediaFailed event is raised if I set LoadedBehavior toMediaState.Play. WithMediaState.Manual, nothing happens (and yes, the media file is in the right location).

Any ideas on what could be wrong?

Thanks.

[1603 byte] By [Terminator] at [2008-1-4]
# 1

Since you set the LoadedBehavior to Manual, you need to call Play() method on MediaElement somewhere in your program to start the media. Give it a try and let me know if it doesn't help. Thanks.

EddieLi-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

Thanks for your response.

If I add me.Play(); after I set the source uri, it will work when i debug in Visual Studio. But when I run the xbap outside of Visual Studio, I will get the MediaFailed exception. I also will need to stream media files online, so it may take some time to download. That's why I thought I needed to call Play in the MediaOpened event.

Terminator at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

What error was reported by the MediaFailed exception? Do you have the stack track?

thanks.

EddieLi-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. > System.Runtime.InteropServices.COMException (0xC00D11B1): Exception from HRESULT: 0xC00D11B1
at Test.Audio.me_MediaFailed(Object sender, ExceptionRoutedEventArgs e)
End of inner exception stack trace
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.RoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.AVElementHelper.OnMediaFailed(Object sender, ExceptionEventArgs args)
at System.Windows.Media.UniqueEventHelper`1.InvokeEvents(Object sender, TEventArgs args)
at System.Windows.Media.MediaEventsHelper.OnMediaFailed(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.StartDispatcherInBrowser(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
Terminator at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

Looks like it is failing due to a specific media player error, you can search online for error "C00D11B1", it will return a lot of results on how people have been solving this problem. It's not related to WPF.

I also suggest you to try play the media in Windows Media Player first, in case it's a DRM content, Media Player will automatically download the license for it, WPF, unfortunately does not do that for you, but it will play the media if the license is pre-downloaded. Media Player will also download any required codec for the media type.

Hope this helps.

EddieLi-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 6
I tested it with a wav, mp3, and mid file (two of which even come with Windows!) and get the same behavior and same error when running the xbap outside of Visual Studio. All three files play correctly in Windows Media Player and work under F5 debugging. So I'm not sure how this would be Media Player related.
Terminator at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 7
can you send me your app to [eddieli at microsoft dot] com? I will be glad to take a look at it for you. thanks.
EddieLi-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 8

This is the solution Eddie Li sent to me:

I looked at your app, and I found two things caused the error.

(1) MediaElement source property should be referenced via SiteofOrigin syntax when hosted in the browser. You can find more about this at http://msdn2.microsoft.com/en-us/library/aa970069.aspx

(2) UriKind will need to be absolute, to make it easier, you can just set it to be UriKind.RelativeOrAbsolute

This is how the MediaElement.Source will be set in the browser.

MediaElement.Source = new Uri("pack://SiteOfOrigin:,,,/town.mid", UriKind.RelativeOrAbsolute);

Of course, you need to make sure the media file (town.mid) to be in the same directory as the xbap file, I believe you are already doing that. As to why it worked in VS, I'm not 100% sure, but I believe it has to do with the app being hosted inside a VShost.exe process during debugging, which might handle resource loading differently.

Terminator at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified