none
MediaElement not working with Multiple Monitors

    Question

  • Does anyone know of a way of getting the WPF MediaElement to work on the secondary monitor of a 2 monitor PC?

    On most (2 out of 3) computers I have tested this on, the MediaElement stops working when a window containing the MediaElement is dragged from on monitor to another.  Strangely, it's not until the entire window is moved to the alternate monitor that the MediaElement stops playing.

    I can't find other reports of this problem so I'm not sure whether:

    1. Hardly anyone uses the MediaElement on multiple monitor PC

    2. It works on most PCs and I'm just unlucky to have 2 that it doesn't work on.

     

    BTW, Windows Media Player work fine on both monitors.

    Monday, January 24, 2011 4:21 AM

All replies

  • Hi ghd,

    Based on your descriptions the MediaElement stops playing video when moving to the second monitor. If I am off base please feel free to let me know

    There are some information you may let us know in order to get a better view of your problem.

    1. Which version of WPF you are using, 4.0 or 3.5?
    2. Is there anything abnormal on the 2nd monitor?
    3. Please kindly elaborate your monitor/video card setup and video format.

    Best regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 25, 2011 9:36 AM
    Moderator
  • I have the same issue in WPF 4.0 MediaElement stops playing video when moved to secondary monitor.  There is nothing abnormal with the 2nd monitor, Windows Media Player works fine when dragged to secondary monitor.  Normally my secondary monitor is in Portrait mode versus typical landscape but I tested with it in Landscape and the issue still occurs.  I'm using an ATI Radeon HD 5700.  My primary monitor (left side) is running at 1920x1200, my secondary monitor (right side) is running at 1200x1600 (or when doing landscape test 1600x1200).  I've had this problem for many months through various updates of the ATI Catalyst drivers.
    Monday, January 31, 2011 7:12 PM
  • Actually, the problem is worse.  In addition to dragging a window containing a MediaElement to another display, even in a single display configuration, it seems like any change to display settings will cause the video to stop playing, including:

    • changing screen resolution
    • screen rotation (portrait vs. landscape)
    • simply attaching a secondary monitor (don't even have to move the window)

    I have tried the following workarounds, none of which are successful:

    1. Recompile app for .NET 4.0 (I am currently targeting .NET 3.5 SP1)
    2. In response to SystemEvents.DisplaySettingsChanged event, call Stop and then Start 
    3. In response to SystemEvents.DisplaySettingsChanged event, remove failing MediaElement control from my window's layout, dynamically create a new MediaElement control, add it to layout, set Source property to Uri for my video, call Play
    4. In response to SystemEvents.DisplaySettingsChanged event, set Source property to null and then to a new Uri for my video, call Play

    For what it's worth, my video is a .MP4 file and the Windows Media Player application does not demonstrate these problems when playing it.

    Monday, February 14, 2011 4:47 PM
  • I can confirm this bug, whenever I move my playing MediaElement video to another screen the video stops. So if I start it on screen 1 it stops when moving to screen 2, and when I start it on screen 2 it wil stop when moving to screen 1. If I keep the video on one screen it will play fine. I'm using WPF .Net 4.0
    Sunday, February 27, 2011 5:27 PM
  • Same issues here. Windows 7 machine, WPF .Net 3.5 App. Avi and WMV files seem to work just fine, but mp4 and mov files experience difficulties when dragged or started on the secondary screen. I initially thought it may be related to my secondary screen being on the left and thus having negative screen coordinates in x. But according to this post and the reconfiguration and tests I did, it's not the problem. My secondary screen is my laptop - thought that might be it. Reconfigure & Test = no solution. My coworker tried it out on a regular desktop with the same make (Dell) and resolution dual monitors, but no dice. Just thought I'd thrown in my two bits and hope that one of us here will find a solution.
    Friday, April 15, 2011 3:18 PM
  • Here's a solution that 1) won't work for some (maybe most) of you, and 2) is not all that pretty, but 3) it's better than nothing. It's quite simple so maybe some of you have already thought of and dismissed it:

    The idea is to create your window such that it is either on the primary screen, or overlaps the primary by 1 pixel, and then, make sure it stays on the primary screen by at least 1 pixel no matter where/how it is moved. In our application, we wanted it to take over the secondary screen and stay there (no resize controls are provided), and so we position the window with the overlapping edge and we're careful about what we draw to that 1 pixel wide edge of the window.

    Hope the idea can come in handy for some of you as a reasonable work around until WPF/MediaElement is fixed.

    • Proposed as answer by rfont Thursday, October 06, 2011 7:50 PM
    Friday, April 15, 2011 6:30 PM
  • I too have this issue.  It does not happen with .avi or .mpg files, only .mov files.  SOME .mov files work fine, but some do not, and those that don't work, never work on the second monitor.  It doesn't matter whether the window is dragged to the second monitor, or if the window is initially placed in the second monitor.

    I am also able to reproduce the problem on multiple computers with ATI and nVidia graphics cards.  WPF 3.5 and 4.0 behave the same way.

    I have read suggestions that WPFMediaKit (on CodePlex) might offer a solution.  But so far I haven't gotten the sample app to play .mov files at all, on ANY screen, even the ones that MediaElement will play.

    This is a show-stopper for me.  Because my application is designed to be used with a projector, it MUST display video output on the secondary monitor.  NOT GOOD!


    Monday, August 01, 2011 3:10 AM
  • I also have this problem.  Have not found a work around as of yet.

    Monday, August 01, 2011 9:47 PM
  • I HAVE FOUND a workaround that might be helpful to some of you.  I got the idea from the post on this thread by kelvcutler.

    The basic idea is to create a parent window that is the size you want, say, 300x300.  Contained within the parent window, create Grid with three columns, where column 0 and 2 are each 1600 pixels wide (never intended to be visible), and column 1 has a width that matches the parent window.  This center column then contains the MediaElement, and is all that is visible in the parent window.  The rest of the grid extends beyond the bonds of the window, but is clipped from view.  BUT even though you can't see the hidden area, the rendering engine still sees it as being partly in the primary monitor space, and the bug is avoided.

    This technique worked beautifully!  No change in appearance, but the formerly non-functional videos now work on any monitor.

    Following is the XAML (Note that some of the above, such as changing the Left and Width properties, wore done in code rather than XAML):

     

    <UserControl x:Class="Application.MediaViewerCtrl"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

        Height="300" Width="300">

        <Grid Cursor="None">

            <Grid.ColumnDefinitions>

                <ColumnDefinition

                    Width="1600" />

                <ColumnDefinition />

                <ColumnDefinition

                    Width="1600" />

            </Grid.ColumnDefinitions>

            <MediaElement Name="_BackgroundMovie1" LoadedBehavior="Manual" Grid.Column="1" />

            <MediaElement Name="_BackgroundMovie2" LoadedBehavior="Manual" Grid.Column="1" />

        </Grid>

    </UserControl>

     




    Tuesday, August 02, 2011 4:52 AM
  • Tony,

    I know it has been awhile since you posted your workaround.  I tried something similar to get around the issue, but it did not seem to work. While the video is playing, if I move it to the second monitor, it freezes the video. I have been trying to toggle the visiblity while the window is moving, then just stop video if it crosses over to the other monitor. I have had some success with doing this, but is is still a little flaky given the asyn way that the mediaelement works.

     

    Do you have any more inisght on your workaround.  You mention that you modify the Left and Width in code. Maybe you could share exactly what your are doing. 

    Thanks

    Wednesday, November 30, 2011 3:47 PM
  • I have exactly the same problem. .Net 4.0 when draging a window that contains a mediaelement from primary monitor to secondary the mediaelement stops playing and I cant find a way to make it start again without restarting my application.

    Thursday, January 19, 2012 10:51 PM
  • We opened a formal SR with MS last year, and here's their response:

    • The issue occurs when a synchronization between WPF and the underlying WMP control have to resynchronize when the display changes occur
    • This is a known problem of WPF and some media playback works better than others
    • Re-encoding the content as WMV does not exhibit the immediate problem as H.264
    • The recommended workaround would be to use another rendering mechanism for the backgrounds. This includes using a custom presenter allocation for DirectShow and hosting that video to a D3DImage.

    We went with the third bullet.

    • Edited by nrcaliendo Friday, January 20, 2012 1:26 PM
    • Proposed as answer by jippers Tuesday, February 07, 2012 8:46 PM
    Friday, January 20, 2012 1:24 PM
  • On Windows 7 the workaround is to disable the Media Foundation codec for MP4 playback (and possibly others if your application uses them). An easy way of turning this off is to use Win7DSFilterTweaker (http://www.codecguide.com/windows7_preferred_filter_tweaker.htm).

    1. Download Win7DSFilterTweaker
    2. Start it
    3. Click Media Foundation
    4. Disable "MP4" (depending on your app being 32-bit or 64-bit, you may need to check both options)
    5. Now you will need another MP4 decoder (ffdshow perhaps?)
    6. MediaElement will now play on a secondary monitor using the installed decoder (if you use ffdshow you will see an icon in the systray)

    I do hope this works for you as well

    Saturday, February 18, 2012 8:52 PM
  • An easier option is to turn off hardware acceleration for WPF on the Window that is displaying the video.

    HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
    HwndTarget hwndTarget = hwndSource.CompositionTarget;
    hwndTarget.RenderMode = RenderMode.SoftwareOnly;

    • Proposed as answer by Martijn Hols Sunday, June 10, 2012 1:30 PM
    Friday, March 02, 2012 5:34 PM
  • Thanks a lot MattyBoy4444, that seems to have solved my problems. I did add a check to see if multiple screens are connected to the computer to prevent disabling hardware acceleration unnecessarily. I run this check in the LocationChanged event handler so that if a new monitor is connected while the application is running, it will still change when necessary.

    private void SurfaceWindow_LocationChanged(object sender, EventArgs e)
    {
        if (System.Windows.Forms.Screen.AllScreens.Length > 1)
        {
            this.LocationChanged -= SurfaceWindow_LocationChanged;
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            HwndTarget hwndTarget = hwndSource.CompositionTarget;
            hwndTarget.RenderMode = RenderMode.SoftwareOnly;
        }
    }
    Sunday, June 10, 2012 2:12 PM
  • An easier option is to turn off hardware acceleration for WPF on the Window that is displaying the video.

    HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
    HwndTarget hwndTarget = hwndSource.CompositionTarget;
    hwndTarget.RenderMode = RenderMode.SoftwareOnly;

    Thank for this quick solution. Amazing this bug is still active and I was unfortunate enough to run in to it. This solution is quick and effective.
    Tuesday, June 19, 2012 11:12 AM
  • OK, day after situation: with this solution my clients where unable to play the media at full screen settings, probably due to hardware they have. So this solution what not efficient  as I thought it might be, not for this real world situation.

    What I did next helped resolve the issue and still managed to keep the hardware support on rendering media. I am forcing the media windows on the primary screen. This might not be the most user friendly solution but keep my clients do their job. So the trick here is to force the media window on primary screen once it's loaded and again if the user tries to move it to secondary screen.

    This method does the job of returning the window to primary screen.

    Private Sub returnToPrimaryScreen()
            'second solution, try and open the screen on default (primary) monitor
            If Not IsNothing(System.Windows.Forms.Screen.AllScreens.First) Then
                Me.WindowStartupLocation = Windows.WindowStartupLocation.Manual
                Me.Left = System.Windows.Forms.Screen.AllScreens.First.Bounds.X
                Me.Top = System.Windows.Forms.Screen.AllScreens.First.Bounds.Y
            End If
        End Su

    This method open the video window you have on the primary screen after it's loaded.

    Private Sub Main_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            Try
                returnToPrimaryScreen()
    Catch ex As Exception
                Throw ex
            End Try

    This method returns the video window you have on the primary screen after it's been moved.

    Private Sub Main_LocationChanged(sender As Object, e As System.EventArgs) Handles Me.LocationChanged
         Try
               returnToPrimaryScreen()
         Catch ex As Exception
               Throw ex
          End Try

    End Sub


    • Edited by gadnaSarma Wednesday, June 20, 2012 9:14 AM -- code styling
    Wednesday, June 20, 2012 9:10 AM
  • 18 months later... what should be basic functionality is still broken.  Transcoding media isn't an option for me, and I have serious reservations about the performance impact of disabling hardware acceleration or making my controls (10s possibly 100s) 3200 pixels wide.  

    Does anyone know if this works in 4.5?

    Monday, July 16, 2012 6:06 PM
  • I'm on .NET 4.5 and encountered the problem recently, although so far i've only noticed it to occur on PC's with integrated Intel HD Graphics (my dev pc has discrete Nvidia GTX550 graphics and its ok).

    I added a setting to my app to allow the user to disable hardware acceleration (like mentioned above). This is the only feasible workaround I've found for my scenario so far, although its far from ideal.

    Friday, October 19, 2012 5:24 AM
  • I'm on .NET 4.5 and encountered the problem recently, although so far i've only noticed it to occur on PC's with integrated Intel HD Graphics (my dev pc has discrete Nvidia GTX550 graphics and its ok).

    I added a setting to my app to allow the user to disable hardware acceleration (like mentioned above). This is the only feasible workaround I've found for my scenario so far, although its far from ideal.

    What I ended up doing is embedding a Silverlight control that has a Silverlight media element which does not have this problem... its a hack but the best solution I could find.
    Monday, December 03, 2012 8:56 PM
  • This is very heavy on processing, if you use this method the CPU usage is extremely high and the video lags

    Friday, January 24, 2014 9:47 AM